I am trying to forecast using covariates in a state-space model. However, I keep getting the error "Model contains time varying system matrices, cannot use argument 'n.ahead'. Use 'newdata' instead." However, when I input newdata instead of n.ahead, I get the error: "System matrices (excluding Z) contain NA or infinite values, covariance matrices contain values larger than 1e+07". Here is what I am trying to achieve: I have training data on my y and x. I would like to use the data on my covariate x, to forecast the sales y. I can then use my test set to evaluate the performance. I also have some scenarios of my covariate, and I would like to predict the sales y with these scenarios. For example: what happens to the sales y if inflation (my covariate) drops in the upcoming 6 months.
I am really lost and would appreciate any help.
Here is the code I used:
Parameters to be estimated are left as NA
model_2 <- SSModel(y_tr ~ SSMtrend(2, Q = list(matrix(NA), matrix(NA)))+SSMregression(~energy_tr, Q = diag(NA,1))
+ SSMseasonal(period = 12, sea.type = "dummy", Q = NA), H = matrix(NA))
# Diffuse prior as initialization
fit_2 <- fitSSM(model_2, inits = c(0, 0, 0, 0, 0), method = "BFGS")
exp(fit_2$optim.out$par)
kf <- KFS(fit_2$model, filtering = c("state"), smoothing = c("state"))
kf
model_ssm <- fit_2$model
# Confidence and prediction interval
conf_ssm <- predict(model_ssm, interval = "confidence", level = 0.9)
pred_ssm <- predict(model_ssm, interval = "prediction", level = 0.9)
# As we did not estimate initial states, we discard the first observations
plot(window(kf$att[, "level"], start = 17), type = "l", ylim = c(10, 20),
ylab = "log monthly sales", main = "Smoothed states of level")
plot(window(kf$a[,'slope'], start = 17), col = 2, lwd = 2, main = "Smoothed states of slope")
plot(window(kf$a[, 'energy_tr'], start = 17), col = 3, lwd = 2, main = "Smoothed states of log energy")
ts.plot(cbind(y_tr, pred_ssm, conf_ssm[, -1]), col = c(1, 2, 3, 3, 4, 4),lty = c(1,1,2,2,2,2), ylab = "log monthly total sales",
main = "Sales")
legend("topleft", c("Observed", "Predictions", "Prediction interval", "Confidence interval"),
lty = c(1,1,2,2,2,2), lwd = 2, col = 1:4, bty = "n")
#Predictions ahead
pred_fit <- predict(model_ssm, interval = "prediction", level = 0.90, n.ahead = length(y_test), filtered = TRUE)
Error in predict.SSModel(model_ssm, interval = "prediction", level = 0.9, : Model contains time varying system matrices, cannot use argument 'n.ahead'. Use 'newdata' instead. So then I tried:
scenario <- read_excel("path")
y_new <- log(scenario$gut)
# New data of regressor to forecast y with
energy_new <- log(scenario$energy_basecase)
newdata <- SSModel(rep(NA,length(energy_new)) ~ SSMtrend(2, Q = list(matrix(NA), matrix(NA)))+SSMregression(~energy_new, Q = diag(NA,1))
+ SSMseasonal(period = 12, sea.type = "dummy", Q = NA), H = matrix(NA))
pred_ssm <- predict(model_ssm, interval = "prediction", level = 0.90, newdata = newdata, filtered = TRUE)
Error in is.SSModel(newdata, na.check = TRUE, return.logical = FALSE) : System matrices (excluding Z) contain NA or infinite values, covariance matrices contain values larger than 1e+07