本文介绍了迭代预测动力模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个函数来迭代地预测使用包dyn构建的模型,我想要一些反馈。有没有更好的方法来做到这一点?有人为dyn类(或dynlm类)编写了规范的预测方法,还是在这里冒险进入未知领域?

  ipredict< -function(model,newdata,interval =none,
level = 0.95,na.action = na.pass,weights = 1){
P< -predict(model,newdata = newdata,interval = interval,
level = level,na.action = na.action,weights = weight)
for(i in seq(1,dim(newdata)[1])){
if(is.na(newdata [i])){
if(interval ==none){
P [i]< -predict(model,newdata = newdata,interval = interval,
level = level,na.action = na.action,weights =权重)[i]
newdata [i]< ;-P [i]
}
else {
P [i,]< -predict(model,newdata = newdata,interval = interval,
level = level,na.action = na.action,weights =
newdata [i]< ;-P [i,1]
}
}
}
P_end< -end(P)[1] *频率)+(end(P)[2] -1)#Convert(time,period)to decimal time
P return(P)
}

使用示例:

 库(dyn)
y A< -rnorm(10)#创建独立变量
B< -rnorm(10)
C< -rnorm(10)
错误< -rnorm(10)
y< -y + .5 * A + .2 * B-.3 * C + .1 *错误#Add关系独立变量
data = cbind(y,A,B,C)

#Fit线性模型
model.dyn< -dyn $ lm(y〜A + B + C + lag(y,-1),data = data)
summary(model.dyn)

#Forecast线性模型
A B< -c(B,rnorm(5))
C y = window(y,end = end(y)+ c (5,0),extend = TRUE)
newdata< -cbind(y,A,B,C)
P1 P2< -ipredict (model.dyn,newdata,interval =predict)

#Plot
plot(y)
lines(P1,col = 2)


解决方案

R中心的rima n.ahead 参数来预测 n 并且似乎这是您正在寻找与dyn结合,但 predict.dyn 目前不支持该功能。为了获得这个效果,你必须迭代地调用 dyn $ any


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?

ipredict <-function(model, newdata, interval = "none",
        level = 0.95, na.action = na.pass, weights = 1) {
 P<-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]<-predict(model,newdata=newdata,interval=interval,
     level=level,na.action=na.action,weights=weights)[i]
    newdata[i]<-P[i]
   }
   else{
    P[i,]<-predict(model,newdata=newdata,interval=interval,
     level=level,na.action=na.action,weights=weights)[i,]
    newdata[i]<-P[i,1]
   }
  }
 }
 P_end<-end(P)[1]*frequency(P)+(end(P)[2]-1) #Convert (time,period) to decimal time
 P<-window(P,end=P_end-1*frequency(P)) #Drop last observation, which is NA
 return(P)
}

Example usage:

library(dyn)
y<-arima.sim(model=list(ar=c(.9)),n=10) #Create AR(1) dependant variable
A<-rnorm(10) #Create independant variables
B<-rnorm(10)
C<-rnorm(10)
Error<-rnorm(10)
y<-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<-dyn$lm(y~A+B+C+lag(y,-1),data=data)
summary(model.dyn)

#Forecast linear model
A<-c(A,rnorm(5))
B<-c(B,rnorm(5))
C<-c(C,rnorm(5))
y=window(y,end=end(y)+c(5,0),extend=TRUE)
newdata<-cbind(y,A,B,C)
P1<-ipredict(model.dyn,newdata)
P2<-ipredict(model.dyn,newdata,interval="prediction")

#Plot
plot(y)
lines(P1,col=2)
解决方案

predict.Arima in the core of R has the n.ahead argument to forecast n steps ahead and it seems that that is what you are looking for in conjunction with dyn but predict.dyn does not currently support that functionality. To get that effect one must iteratively call dyn$whatever as you are doing.

这篇关于迭代预测动力模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:17