# Re: leap years (Was: [R] lm and time series)

From: Kjetil Brinchmann Halvorsen <kjetil_at_acelerate.com>
Date: Sat 05 Mar 2005 - 12:49:20 EST

A followup:

For the moment I just left out the 29 of February's, but that dsoes'nt look really good. Is the only way to include them to use irregular time series?

Kjetil

Gabor Grothendieck wrote:

>
>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.
>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
>
>
>
>
>

```--

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
--  Mahdi Elmandjra

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help