RE: [R] lm and time series

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Sat 05 Mar 2005 - 02:53:45 EST

 

From: Matthieu Cornec <matthieu.cornec@gmail.com>  

> I create a multivariate time series containing NA values (that could
> come directly from an imported file,)
> I want to compute a linear regression and obtain a time serie for both
> residuals and fitted values. I have tried the trick ts.intersect,
> without success.
>
> Could you help me out of this?
> ####
> Example:
>
> y<-ts(1:10+rnorm(10))
> x<-ts(1:10)
> datats<-cbind(y,lagx=lag(x))
>
> Notice the datats could come directly from an imported file, that is
> why I did not use ts.intersect(y,lagx=lag(x))
>
> fit<-lm(y~lagx,data=datats,na.action=na.omit)
>
> but how do I get a time serie of residuals instead of a vector residuals(fit)?
> ######
>
> Matthieu Cornec
>

ts is used for regular time series. Removing NAs, other than at the beginning or end, means its probably best to model it as an irregular time series and so to use an irregular time series package. Below it is done in zoo. Also review the comments in my post to your previous question along these lines and, in particular, be sure you read the zoo vignette referenced there which has 15 pages of examples of time series manipulations.

library(zoo)

# set up test data with NAs
set.seed(1)
x <- zoo(1:10)
y <- x + rnorm(10)
y[5] <- x[2] <- NA

# create multivariate zoo series without NAs # Note: if you want to fill in NAs rather than omit them see ?na.locf z <- na.omit(merge(y, lagx = lag(x, -1)))

# run lm
# (This also works:   z.lm <- lm(I(y ~ lagx), z)
# but the syntax is experimental.)

z.lm <- lm(y ~ lagx, as.data.frame(z))

# get fitted and resid using fact that their time base is that of z

z.fit <- z.resid <- z[,1]
z.fit[] <- fitted(z.lm)
z.resid[] <- resid(z.lm)

# We can just use the zoo series already created.  Its not really
# necessary to convert it to ts but if for some reason we want a 
# ts series the following creates one.

# (This uses facts that we know y starts at 1 and is regularly spaced # and other series have a subset of the time base of y.) ts(coredata(merge(y, x, z.fit, z.resid)))


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Mar 05 04:08:20 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:40 EST