简体   繁体   English

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

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

I am trying to build a dynamic regression model and so far I did it with the dynlm package. 我正在尝试建立一个动态回归模型,到目前为止,我是使用dynlm软件包完成的。 Basically the model looks like this 基本上模型看起来像这样

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

y_t shall be predicted, x1_t and x2_t will be given and so is y_(t-1) . y_t应预测, x1_tx2_t将给出,所以是y_(t-1)

Building the model with the dynlm package worked fine, but when it came to predict y_t I got confused... 使用dynlm软件包构建模型可以正常工作,但是当要预测y_t我很困惑...

I found this , which seems to be a very similar problem, but it did not help me to handle my own problem. 我发现了这个问题 ,这似乎是一个非常相似的问题,但是它并没有帮助我解决自己的问题。

Here is the problem I am facing (basically what predict() does, seems to be weird. See comments!): 这是我面临的问题(基本上是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 

Of course I could just use my own prediction function, but the problem is that my real model will have way more variables (which can even vary as I use the the step function to optimize the model according to AIC) and that I is why I want to use the predict() function. 当然,我可以只使用自己的预测函数,但是问题是我的真实模型将具有更多的变量(随着我使用步进函数根据AIC优化模型,这些变量甚至可能会有所不同),这就是为什么我想使用predict()函数。

Any ideas, how to solve this? 有什么想法,如何解决呢?

Unfortunately, the dynlm package does not provide a predict() method. 不幸的是, dynlm包不提供predict()方法。 At the moment the package completely separates the data pre-processing (which knows about functions like d() , L() , trend() , season() etc.) and the model fitting (which itself is not aware of the functions). 目前,程序包将数据预处理(了解d()L()trend()season()等功能)与模型拟合(本身不了解功能)完全分开了。 A predict() method has been on my wishlist but so far I did not get round to write one because the flexibility of the interface allows so many models where it is not quite straightforward what to do. 我的心愿单上有一个predict()方法,但是到目前为止,我还没有写完一个方法,因为接口的灵活性允许这么多的模型,而这些模型并不是很简单。 In the meantime, I should probably add a method that throws a warning before the lm method is found by inheritance. 同时,我可能应该添加一个在继承找到lm方法之前引发警告的方法。

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

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