Re: [R] how to predict dynamic model in R

2009-07-23 Thread Gabor Grothendieck
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 wrote: > Hi, Gabor, it seems ARIMA model does not have that problem. For ex

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Hongwei Dong
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

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Gabor Grothendieck
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 p

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Hongwei Dong
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. Than

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Gabor Grothendieck
You can't remove Y since its in the rhs of your model. On Thu, Jul 23, 2009 at 8:25 PM, Hongwei Dong 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 thi

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Hongwei Dong
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:2

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Gabor Grothendieck
Best thing is to test it out on simulated data for which you already know the answer. library(dyn) set.seed(123) DF <- data.frame(x = rnorm(10), z = rnorm(10)) DF$Y <- 0 for(i in 2:10) { DF$Y[i] <- with(DF, 2*Y[i-1] + 3*z[i] +4* x[i] + 5*x[i-1] + rnorm(1)) } DF.zoo <- do.call(merge, lapply(DF, z

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Hongwei Dong
Hi, Gabor, got it. Thanks a lot. I have one more question about how the "predict" function works here, especially for the lag(Y,-1) part. In my model, I assume I know predictors x and z in the next two years, and use them to predict Y. For each forecast step at time t, the lag(Y,-1) in the model s

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Gabor Grothendieck
Please provide your code in a reproducible form (as requested previously). Here we fit with first 9 points and then add a point for prediction. (Of course your model can only predict the current value of Y so you may have to rethink your model even aside from the implementation if you really want

Re: [R] how to predict dynamic model in R

2009-07-23 Thread Hongwei Dong
Hi, Gabor and Other R users, I'm re-posting my script and the results I got. here is the dynamic model I used to estimate in-sample model (1996-2006) and it works: fit<-dyn$lm(Y~lag(Y,-1)+z+x+lag(x,-1)+lag(x,-2)+lag(x,-3)+lag(x,-4)) Then I used this model to do out sample forecast with t

Re: [R] how to predict dynamic model in R

2009-07-22 Thread Gabor Grothendieck
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:

Re: [R] how to predict dynamic model in R

2009-07-22 Thread Gabor Grothendieck
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 wrote: > I have a dynamic time series model like this: > dyn$lm( y ~ lag(y,-1) + x + lag

[R] how to predict dynamic model in R

2009-07-22 Thread Hongwei Dong
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