R具有滞后因变量的预测

时间:2015-02-12 13:39:23

标签: r loops regression lag dummy-data

使用lm函数拟合(Pt =每个四分之一的 Pt-1 + b Xt + Dummy变量)以拟合样本数据。如何创建n.ahead = 12预测?无法弄清楚如何设置虚拟和Pt-1前迭代。感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

也许这可以帮助

#store your model
model<-your_model

#get the last pt observation
last<-dato[nrows(dato$pt), c('pt', 'age')]

years<-12/4

#create dummy
t1<-rep(c(1,0,0,0) , years)
t2<-rep(c(0,1,0,0) , years)
t3<-rep(c(0,0,1,0) , years)
t4<-rep(c(0,0,0,1) , years)

#create pt observation
pt<-c(last$pt, rep(NA, length(t1)-1 ))

df<-data.frame(t1=t1,t2=t2,t3=t3,t4=t4,lag_pt=pt, age=last$age)

df$predict<-NA

for (i in 1:nrow(df) )
{
df$predict[i]<-predict(model, data=df[i,])

if (i!=nrow(df))
{df$lag_pt[i+1]<-df$predict[i] }

}