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.
# Create an example data set with 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))
I tried to put the exogenous training and test sets in the same order by adding this code,
# Reorder columns of exogenous test sets to match training order
test_EX1 <- test_EX1[, colnames(train_EX1)]
test_EX2 <- test_EX2[, colnames(train_EX2)]
but now this message appears:
test_EX2 <- test_EX2[, colnames(train_EX2)]
Error in [.default(test_EX2, , colnames(train_EX2)) :
wrong number of dimensions
I have tried to solve the column selection in different ways like the following:
colnames_test_EX1 <- colnames(train_EX1)
colnames_test_EX2 <- colnames(train_EX2)
test_EX1 <- test_EX1[, colnames_test_EX1]
test_EX2 <- test_EX2[, colnames_test_EX2]
colnames_test_EX1 <- colnames(train_EX1)
colnames_test_EX2 <- colnames(train_EX2)
test_EX1 <- test_EX1[, colnames_test_EX1, drop = FALSE]
test_EX2 <- test_EX2[, colnames_test_EX2, drop = FALSE]
colnames_test_EX1 <- colnames(train_EX1)
colnames_test_EX2 <- colnames(train_EX2)
test_EX1 <- test_EX1[, which(colnames(test_EX1) %in% colnames_test_EX1)]
test_EX2 <- test_EX2[, which(colnames(test_EX2) %in% colnames_test_EX2)]
But it still shows the same error. Could someone help me with this please?