繁体   English   中英

R:带有dynlm包的动态线性回归,如何预测()?

[英]R: Dynamic linear regression with dynlm package, how to predict()?

我正在尝试建立一个动态回归模型,到目前为止,我是使用dynlm软件包完成的。 基本上模型看起来像这样

y_t = a*x1_t + b*x2_t + ... + c*y_(t-1).

y_t应预测, x1_tx2_t将给出,所以是y_(t-1)

使用dynlm软件包构建模型可以正常工作,但是当要预测y_t我很困惑...

我发现了这个问题 ,这似乎是一个非常相似的问题,但是它并没有帮助我解决自己的问题。

这是我面临的问题(基本上是predict()所做的事情,似乎很奇怪。请参阅注释!):

library(dynlm)

# Create Data
set.seed(1)
y <- arima.sim(model = list(ar = c(.9)), n = 11) #Create AR(1) dependant variable
A <- rnorm(11) #Create independent variables
B <- rnorm(11)
y <- y + .5 * A + .2 * B #Add relationship to independent variables 
data = cbind(y, A, B)

# subset used for the fitting of the model
reg <- data[1:10, ]


# Fit dynamic linear model
model <- dynlm(y ~ A + B + L(y, k = 1), data = reg)  # dynlm
model

# Time series regression with "zooreg" data:
# Start = 2, End = 11
#
# Call:
# dynlm(formula = y ~ A + B + L(y, k = 1), data = reg)

# Coefficients:
# (Intercept)            A            B  L(y, k = 1)  
#      0.8930      -0.2175       0.2892       0.5176  


# subset last two rows.
# the last row (r11) for which y_t shall be predicted, where from the same time A and B are input for the prediction
# and the second last row (r10), so y_(t-1) can be input for the model as well
pred <- as.data.frame(data[10:11, ])

# prediction using predict()
predict(model, newdata = pred)

#    1        2 
# 1.833134 1.483809 

# manual calculation of prediction of y in r11 (how I thought it should be...), taking y_(t-1) as input
predicted_value <- model$coefficients[1] + model$coefficients[2] * pred[2, 2] + model$coefficients[3] * pred[2, 3] + model$coefficients[4] * pred[1, 1]
predicted_value
# (Intercept) 
#    1.743334 

# and then what gives the value from predict() above taking y_t into the model (which is the value that should be predicted and not y_(t-1))
predicted_value <- model$coefficients[1] + model$coefficients[2] * pred[2, 2] + model$coefficients[3] * pred[2, 3] + model$coefficients[4] * pred[2, 1]
predicted_value
# (Intercept) 
#    1.483809 

当然,我可以只使用自己的预测函数,但是问题是我的真实模型将具有更多的变量(随着我使用步进函数根据AIC优化模型,这些变量甚至可能会有所不同),这就是为什么我想使用predict()函数。

有什么想法,如何解决呢?

不幸的是, dynlm包不提供predict()方法。 目前,程序包将数据预处理(了解d()L()trend()season()等功能)与模型拟合(本身不了解功能)完全分开了。 我的心愿单上有一个predict()方法,但是到目前为止,我还没有写完一个方法,因为接口的灵活性允许这么多的模型,而这些模型并不是很简单。 同时,我可能应该添加一个在继承找到lm方法之前引发警告的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM