[R] lm and time series

Gabor Grothendieck ggrothendieck at myway.com
Fri Mar 4 16:53:45 CET 2005


 
From:   Matthieu Cornec <matthieu.cornec at 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)))




More information about the R-help mailing list