[R] Semi-Regular Time Series with Missing Values

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 20 00:36:21 CET 2011


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



More information about the R-help mailing list