[R] how to predict dynamic model in R

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 23 07:53:44 CEST 2009


Here is an example closer to yours.

> library(dyn)
> set.seed(123)
> x <- zooreg(rnorm(10))
> y <- zooreg(rnorm(10))
> L <- function(x, k = 1) lag(x, k = -k)
> mod <- dyn$lm(y ~ L(y) + L(x, 0:2))
> mod

Call:
lm(formula = dyn(y ~ L(y) + L(x, 0:2)))

Coefficients:
(Intercept)         L(y)   L(x, 0:2)1   L(x, 0:2)2   L(x, 0:2)3
    0.06355     -0.74540      0.63649      0.44957     -0.41360

> newdata <- cbind(x = c(coredata(x), rnorm(1)), y = c(coredata(y), rnorm(1)))
> newdata <- zooreg(newdata)
> predict(mod, newdata)
         1          2          3          4          5          6          7
        NA         NA  0.9157808  0.6056333 -0.5496422  1.5984615 -0.2574875
         8          9         10         11         12         13
-1.6148859  0.3329285 -0.5284646 -0.1799693         NA         NA


On Thu, Jul 23, 2009 at 1:04 AM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote:
> Use dyn.predict like this:
>
>> library(dyn)
>> x <- y <- zoo(1:5)
>> mod <- dyn$lm(y ~ lag(x, -1))
>> predict(mod, list(x = zoo(6:10, 6:10)))
>  7  8  9 10
>  7  8  9 10
>
>
> On Thu, Jul 23, 2009 at 12:54 AM, Hongwei Dong<pdxdong at gmail.com> wrote:
>> I have a dynamic time series model like this:
>> dyn$lm( y ~ lag(y,-1) + x + lag(x,-1)+lag(x,-2) )
>>
>> I need to do an out of sample forecast with this model. Is there any way I
>> can do this with R?
>> It would be greatly appreciated if some one can give me an example. Thanks.
>>
>>
>> Harry
>>
>>        [[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.
>>
>




More information about the R-help mailing list