[R] how to predict dynamic model in R

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 24 04:56:32 CEST 2009


If by "problem" you mean the problem of determining how, in general,
you should proceed perhaps you need an introductory guide on R
and time series such as Cowpertwait's book.

On Thu, Jul 23, 2009 at 10:37 PM, Hongwei Dong<pdxdong at gmail.com> wrote:
> Hi, Gabor, it seems ARIMA model does not have that problem. For example:
> set.seed(123)
> y<-ts(c(1:20))
> x = ts(rnorm(20))
> z = ts(rnorm(20))
> tt<-ts(cbind(x, lag(x,-1),lag(x,-2),z))
> fit <- arima(y[1:15],order=c(1,0,0),xreg=tt[(1:15),])
> fit
> pred <- predict(fit, n.ahead=5,tt[(16:20),])
> pred
> What do you make of this? Thanks.
> Harry
>
> On Thu, Jul 23, 2009 at 6:36 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>>
>> Try this:
>>
>> library(dyn)
>> set.seed(123)
>> tz <- zoo(cbind(Y = 0, x = rnorm(10), z = rnorm(10)))
>>
>> # simulate values
>> for(i in 2:10) {
>>  tz$Y[i] <- with(tz, 2*Y[i-1] + 3*z[i] +4* x[i] + 5*x[i-1] + rnorm(1))
>> }
>>
>> # keep copy of tz to compare later to simulated Y's
>> tz.orig <- tz
>>
>> # NA out Y's that are to be predicted
>> tz[7:10, "Y"] <- NA
>>
>> L <- function(x, k = 1) lag(x, -k)
>>
>> # predict 1 ahead each iteration
>> for(i in 7:10) {
>>    # fit based on first i-1 values
>>    fit <- dyn$lm(Y ~ L(Y) + z + L(x, 0:1), tz, subset = seq_len(i-1))
>>    # get prediction for ith value
>>    tz[i, "Y"] <- tail(predict(fit, tz[1:i,]), 1)
>> }
>> cbind(pred = tz[7:10, "Y"], act = tz.orig[7:10, "Y"])
>>
>>
>> On Thu, Jul 23, 2009 at 9:02 PM, Hongwei Dong<pdxdong at gmail.com> wrote:
>> > What I want R to do is to use the estimated Y at t-1 to be the lag(Y,-1)
>> > in
>> > the forecast equation for time t. Is there anyway I can realize this
>> > with R?
>> > For example, when the Y value for year 18 is forecast, the estimated Y
>> > for
>> > year 17 is used, not the actual Y for year 17 already in the data.
>> > Thanks for you patience. I appreciate it.
>> > Harry
>> >
>> >
>> >
>> > On Thu, Jul 23, 2009 at 5:44 PM, Gabor Grothendieck
>> > <ggrothendieck at gmail.com> wrote:
>> >>
>> >> 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