An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110118/285311b8/attachment.pl>
Semi-Regular Time Series with Missing Values
14 messages · Adam Oliner, Gabor Grothendieck
On Tue, Jan 18, 2011 at 6:33 PM, Adam Oliner <oliner at gmail.com> wrote:
Hi, I'm trying to make a ts object that has both NA values and a frequency other than 1 (so I can use stl). I've tried all permutations I can think of, but cannot get the desired (expected?) results. The values live in x and the corresponding semi-regular time stamps are in t:
library('zoo')
z = zoo(x, order.by=t, frequency=24)
zzr = as.zooreg(z, start=0)
zr = zooreg(x, order.by=t, start=0, frequency=24)
zrz = as.zoo(zr)
The objects z, zr, and zzr all have frequency 24, as desired, and plot correctly. Object zrz plots correctly but has frequency 1. Now I try to make it a ts object:
zrt = ts(zzr)
... omits the missing values.
zrt = ts(zzr, frequency=24)
... omits the missing values and changes the timestamps.
plot(ts(zr))
... omits the missing values.
zrt = ts(zr, frequency=24)
... omits the missing values and changes the timestamps.
zrt = as.ts(zr)
... inserts 23 NA values between each legitimate value.
zrt = as.ts(zr, frequency=24)
... inserts 23 NA values between each legitimate value.
zrzt = ts(zrz)
... omits the missing values.
zrzt = ts(zrz, frequency=24)
... omits the missing values and changes the timestamps.
Note that there is an as.ts.zoo method.
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/e8e21077/attachment.pl>
On Wed, Jan 19, 2011 at 1:10 PM, Adam Oliner <oliner at gmail.com> wrote:
I don't seem to have that method:
zts = as.ts.zoo(z)
Error: could not find function "as.ts.zoo" I'm finding forum posts from people trying to use stl directly with zoo[reg] functions (despite the documentation specifying ts) but discovering that stl gives errors with NA entries. I encountered similar errors. Can anyone on this list confirm whether stl works with NA values? If so, could someone please point me to a working example?
Normally S3 methods such as as.ts.zoo are not exported unless there is particular reason to access them directly. Rather one accesses them via their generic which dispatches the appropriate method based on the class of its first argument. That is if z is a zoo object then as.ts(z) dispatches as.ts.zoo. If you really want to see it you can do this zoo:::as.ts.zoo
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/80753df1/attachment.pl>
On Wed, Jan 19, 2011 at 3:07 PM, Adam Oliner <oliner at gmail.com> wrote:
This still does not give me the desired behavior:
z = zoo(x, order.by=t, frequency=24) t = as.ts(z)
... inserts 23 NA values between every actual value. This is not correct; the original data has a frequency of 24 and doesn't need one forced upon it during coercion.
z = zoo(x, order.by=t) t = as.ts(z)
... obviously doesn't give me a ts with frequency 24. Keep in mind that I'm trying to get a ts with frequency 24 so I can feed it to stl. I haven't yet found a sequence of operations that lets me do this. Any ideas?
Its not clear what you are expecting but here is an example:
library(zoo) z <- zooreg(1001:1012, frequency = 4); z
1(1) 1(2) 1(3) 1(4) 2(1) 2(2) 2(3) 2(4) 3(1) 3(2) 3(3) 3(4) 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
frequency(z)
[1] 4
tt <- as.ts(z); tt
Qtr1 Qtr2 Qtr3 Qtr4 1 1001 1002 1003 1004 2 1005 1006 1007 1008 3 1009 1010 1011 1012
frequency(tt)
[1] 4
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/15deea2c/attachment.pl>
On Wed, Jan 19, 2011 at 4:17 PM, Adam Oliner <oliner at gmail.com> wrote:
Gabor, If you follow those steps on a series with missing values, the resulting tt will contain tons of new NAs. (See my original email and the call to "zrt =
zoo and zooreg classes handle series which may not be regularly spaced whereas ts only handles regularly spaced series so the series necessarily must be filled with NAs if you convert it from an irregularly spaced one to a regularly spaced one.
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/76cef7bf/attachment.pl>
On Wed, Jan 19, 2011 at 4:27 PM, Adam Oliner <oliner at gmail.com> wrote:
My series was regularly spaced, it simply contained missing values. I don't see why the new values were added. Besides, my previous email showed exactly how to convert a zoo object with
Please provide a minimal self-contained reproducible example (as per last line on every r-help message) and explain what you were expecting that is different than what you got and we may be able to say more.
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/d26588ed/attachment.pl>
On Wed, Jan 19, 2011 at 5:21 PM, Adam Oliner <oliner at gmail.com> wrote:
The objective is to get the stl calls to work without making up values for
the NAs:
library("zoo")
data = c(50, 32, 22, 13, 14, 6, 12, 3, 6, 21, NA, NA, 15, 13, 17, 9, 11, 34,
63)
dataz = zoo(data, 1:length(data))
t = as.ts(dataz)
datat = as.ts(dataz)
datatt = ts(t, frequency=2)
frequency(datatt)
length(datatt)
# these all give errors
stl(datatt, s.window="per")
# this next error is especially bizarre to me, because datatt is periodic
with more than two periods
stl(datatt, s.window="per", na.action=F)
stl(datatt, s.window="per", na.action=na.omit)
stl(datatt, s.window="per", na.action=na.pass)
stl(datatt, s.window="per", na.action=na.exclude)
stl(dataz, s.window="per", na.action=F)
stl(datat, s.window="per", na.action=F)
I was expecting at least one of the stl calls to work, because stl claims to
require a ts object with a frequency greater than one and at least two
periods (which I gave it) and it claims to handle NA values.
I think what you want is: library(zoo) library(zoo) data <- c(50, 32, 22, 13, 14, 6, 12, 3, 6, 21, NA, NA, 15, 13, 17, 9, 11, 34, 63) z <- zooreg(data, frequency = 2) stl(z) or equivalently tt <- ts(data, frequency = 2) stl(tt) however stl does not support series with NAs so that can't work with your data and specifying na.action won't help since that is equivalent to stl(na.action(as.ts(z))). Also note that na.action's value is a function and na.action = F is illegal unless you have a function called F in your workspace. These work in so far as they give an answer but whether they are acceptable is up to you: stl(z[1:01], "per") stl(z[13:19], "per") stl(na.approx(z), "per")
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/5c4f72d6/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110119/f6e4cd1e/attachment.pl>