I want to do a bootstrap prediction of the future values of my two time series pibusa and popfra from my dataset expli.bv, using a VAR model with a lag p = 2. I can't seem to make it work as I get non numeric variables at each replication... Here is my code :
library(vars)
p <- 2
Model_expli <- VAR(expli.bv, p = p, type = "const", season = NULL, exogen = NULL)
m <- 1000 # Number of bootstrap replications
n <- nrow(expli.bv) # Number of observations in my dataset
nvybb <- matrix(0, m, p)
zb <- matrix(0, m, p)
for (b in 1:m) {
indboot <- sample(1:n, n, replace = TRUE)
ub <- Model_expli$residuals[indboot, , drop = FALSE]
# Prediction bootstrap of Y (future)
yb <- predict(Model_expli, n.ahead = p, ci = 0.95, dumvar = NULL, dumvar.forecast =
NULL, dumvar.match = FALSE, lag.max = NULL, nahead = NULL,
ci.method = "ET", ortho = FALSE, VAR.extra = NULL, dumvar.include =
TRUE)
# Construction of Z*(i)
for (i in 1:p) {
yb_values <- as.vector(yb[[i]])
fitted_values <- as.vector(Model_expli$fitted.values[, i])
if (all(is.numeric(yb_values)) && all(is.numeric(fitted_values))) {
z <- (yb_values - fitted_values) / sqrt(diag(var(ub[, i])))
nvybb[b, i] <- yb_values
zb[b, i] <- z
} else {
# case when variable is non numeric
cat("Variable non numérique détectée dans la réplication", b, "de la variable",
i, "\n")
}
}
}
And here is the head of my dataset for reproducibility :
structure(c(1023.984, 1059.458, 1101.609, 1140.716, 1158.725,
1171.112, 218035000, 220239000, 222585000, 225055000, 227225000,
229466000), dim = c(6L, 2L), dimnames = list(NULL, structure(c("pibfra",
"popusa"), dim = 1:2)), tsp = c(1976, 1981, 1), class = c("mts",
"ts", "matrix", "array"))
Can anyone help me ?