简体   繁体   中英

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. 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) .

Building the model with the dynlm package worked fine, but when it came to predict y_t I got confused...

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!):

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.

Any ideas, how to solve this?

Unfortunately, the dynlm package does not provide a predict() method. 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). 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. In the meantime, I should probably add a method that throws a warning before the lm method is found by inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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