I am trying to model a time series with two hexogenous series, however when I try to test the model on the test partition the result is this message.
Warning message:
In forecast.forecast_ARIMA(model, xreg = cbind(test_EX1, test_EX2, :
xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
# Crear un conjunto de datos de ejemplo con series A, EX1 y EX2
set.seed(123)
n <- 100
A <- ts(sin(1:n) + rnorm(n, sd = 0.1), start = 1)
EX1 <- ts(rnorm(n), start = 1)
EX2 <- ts(rnorm(n), start = 1)
# Split data into training and test set (70% - 30%)
train_len <- round(length(A) * 0.7)
train_A <- window(A, start = 1, end = train_len)
test_A <- window(A, start = train_len + 1)
train_EX1 <- window(EX1, start = 1, end = train_len)
test_EX1 <- window(EX1, start = train_len + 1)
train_EX2 <- window(EX2, start = 1, end = train_len)
test_EX2 <- window(EX2, start = train_len + 1)
# Initialize the harmonic matrix
K <- 6 # Número máximo de armónicos
harmonics <- matrix(0, nrow = length(train_A), ncol = 2 * K)
# Calculate harmonics
for (k in 1:K) {
harmonics[, 2 * k - 1] <- sin(2 * pi * k * (1:length(train_A)) / length(train_A))
harmonics[, 2 * k] <- cos(2 * pi * k * (1:length(train_A)) / length(train_A))
}
# Model series A with dynamic harmonic regression with ARIMA errors and exogenous series EX1 and EX2
model <- Arima(train_A, xreg = cbind(train_EX1, train_EX2, harmonics), order = c(1, 0, 1))
# Make predictions
# Initialize harmonic matrices for the test set
harmonics_test <- matrix(0, nrow = length(test_A), ncol = 2 * K)
for (k in 1:K) {
harmonics_test[, 2 * k - 1] <- sin(2 * pi * k * (1:length(test_A)) / length(test_A))
harmonics_test[, 2 * k] <- cos(2 * pi * k * (1:length(test_A)) / length(test_A))
}
# Make predictions
predictions <- forecast(model, xreg = cbind(test_EX1, test_EX2, harmonics_test))