[R] how to predict dynamic model in R

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 24 02:44:22 CEST 2009


You can't remove Y since its in the rhs of your model.

On Thu, Jul 23, 2009 at 8:25 PM, Hongwei Dong<pdxdong at gmail.com> wrote:
> Thanks, Gabor. Here are the problems I'm trying to solve.
> FIRST, I run this to simulate a 20 years time series process. The data from
> 1-15 years are used to estimate the model, and this model is used to predict
> the year from 16-20. The following script works.
> set.seed(123)
> tt <- ts(cbind(Y = 1:20, x = rnorm(20), z = rnorm(20)))
> L <- function(x, k = 1) lag(x, -k)
> tt.zoo <- as.zoo(tt)
> fit <- dyn$lm(Y ~ L(Y) + z + L(x, 0:1), tt.zoo[(1:15), ])
> fit
> pred <- predict(fit, tt.zoo[(16:20),])
> pred
> SECONDLY, I use similar script, but pretend that we do not know the Y data
> from year 16-20. We know x and z for year 16-20, and use them predict Y
> based on the model estimated from 1-15 years. So, in the "newdata" part, I
> use tt.zoo[(16:20), (2:3)] to remove the Y out. here is the script.
> Unfortunately, it does not work in that way.
> pred1 <- predict(fit, tt.zoo[(16:20),(2:3)])
> pred1
> It will be greatly appreciated if you can give me some guide on this.
> Thanks.
> Harry
>
>
>
>
>
>
> On Wed, Jul 22, 2009 at 10:04 PM, 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