Note that there are some explanatory texts on larger screens.

plurals
  1. POIteratively forecasting dyn models
    primarykey
    data
    text
    <p>I've written a function to iteratively forecast models built using the package dyn, and I'd like some feedback on it. Is there a better way to do this? Has someone written canonical "forecast" methods for the dyn class (or dynlm class), or am I venturing into uncharted territory here?</p> <pre><code>ipredict &lt;-function(model, newdata, interval = "none", level = 0.95, na.action = na.pass, weights = 1) { P&lt;-predict(model,newdata=newdata,interval=interval, level=level,na.action=na.action,weights=weights) for (i in seq(1,dim(newdata)[1])) { if (is.na(newdata[i])) { if (interval=="none") { P[i]&lt;-predict(model,newdata=newdata,interval=interval, level=level,na.action=na.action,weights=weights)[i] newdata[i]&lt;-P[i] } else{ P[i,]&lt;-predict(model,newdata=newdata,interval=interval, level=level,na.action=na.action,weights=weights)[i,] newdata[i]&lt;-P[i,1] } } } P_end&lt;-end(P)[1]*frequency(P)+(end(P)[2]-1) #Convert (time,period) to decimal time P&lt;-window(P,end=P_end-1*frequency(P)) #Drop last observation, which is NA return(P) } </code></pre> <p>Example usage:</p> <pre><code>library(dyn) y&lt;-arima.sim(model=list(ar=c(.9)),n=10) #Create AR(1) dependant variable A&lt;-rnorm(10) #Create independant variables B&lt;-rnorm(10) C&lt;-rnorm(10) Error&lt;-rnorm(10) y&lt;-y+.5*A+.2*B-.3*C+.1*Error #Add relationship to independant variables data=cbind(y,A,B,C) #Fit linear model model.dyn&lt;-dyn$lm(y~A+B+C+lag(y,-1),data=data) summary(model.dyn) #Forecast linear model A&lt;-c(A,rnorm(5)) B&lt;-c(B,rnorm(5)) C&lt;-c(C,rnorm(5)) y=window(y,end=end(y)+c(5,0),extend=TRUE) newdata&lt;-cbind(y,A,B,C) P1&lt;-ipredict(model.dyn,newdata) P2&lt;-ipredict(model.dyn,newdata,interval="prediction") #Plot plot(y) lines(P1,col=2) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload