Я пытаюсь построить модель динамической регрессии, и до сих пор я делал это с помощью пакета dynlm. В основном модель выглядит так
y_t = a*x1_t + b*x2_t + ... + c*y_(t-1).
y_t
должно быть предсказано, x1_t
и x2_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()
.
Любые идеи, как это решить?
predict
. - person salhin   schedule 09.05.2015