The dyn package has a predict method that can typically be used to
predict one step ahead (and you can use a loop to get multiple steps).
To use just preface lm (or glm or any model fitting function in R
that uses model.frame in the same way as lm). It works with zoo,
zooreg, ts, its and irts class series.
library(dyn) # this also pulls in zoo
# generate test data
set.seed(123)
x <- zooreg(rep(1, 10))
for(i in 2:10) x[i] <- x[i-1] + rnorm(1)
# fit model
Lag <- function(x, k = 1) lag(x, -k)
mod <- dyn$lm(x ~ Lag(x))
# perform prediction and plot
x. <- predict(mod, list(x = x))
plot(cbind(x, x.), col = 1:2, screen = 1)
# get more info
package?dyn
?dyn
On Sun, Nov 22, 2009 at 9:43 PM, zubin <binabina at bellsouth.net>
wrote:> Hello, can one use predict, as you can with other model objects like lm,
> with dynlm to predict a new data set that is identical in field names,
> just a different time period.
>
> Be nice if you could, I don't really want to create a new data set with
> all the lags, hoping it would generate dynamically. ?Does not seem to
> work, get a # of column error. ?Any suggestions?
>
>
> R> str(dfz)
> An 'xts' object from 2009-09-25 09:45:06 to 2009-10-19 15:00:57
containing:
> ?Data: num [1:28232, 1:8] 0.54771 -0.00825 1.27406 0.69705 1.08107 ...
> ?- attr(*, "dimnames")=List of 2
> ?..$ : NULL
> ?..$ : chr [1:8] "PC1" "PC2" "PC3"
"PC4" ...
> ?Indexed by objects of class: [POSIXt,POSIXct] TZ: GMT
> ?xts Attributes:
> ?NULL
>
> R> str(z)
> An 'xts' object from 2009-10-21 09:45:04 to 2009-10-21 15:00:56
containing:
> ?Data: num [1:2304, 1:8] -0.5044 1.237 -0.7764 0.3931 0.0629 ...
> ?- attr(*, "dimnames")=List of 2
> ?..$ : NULL
> ?..$ : chr [1:8] "PC1" "PC2" "PC3"
"PC4" ...
> ?Indexed by objects of class: [POSIXt,POSIXct] TZ: GMT
> ?xts Attributes:
> ?NULL
>
>
> dols = dynlm(FAS0 ~ L(FAS0,1:10) + L(PC1,0:10) + L(PC2,0:10) +
> L(PC3,0:10) + L(PC4,0:10) + L(PC5,0:10) + L(PC6,0:10) + L(PC7,0:10),
> data=dfz)
>
> R> predict(dols,newdata=z)
>
> /*Error in fix.by(by.x, x) : 'by' must match numbers of columns*/
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>