I have tried to perform a dynamic harmonic regression to obtain an arima model with exogenous variables and for that I used the code presented in https://otexts.com/fpp2/dhr.html. However, when running the code an error message is displayed. Error in ...fourier(x, K, 1:NROW(x)) : Number of periods does not match number of orders
To build the model I used three monthly series with 43 data, where two of them are the exogenous variables. The dataframe I used looks like this:
{> modeloajuste
T_matrix V_matrix M Month
1 14.17802 0.8604688 1 1
2 15.79006 0.7736985 2 2
3 17.62116 0.8357023 3 3
4 22.05094 0.8797615 4 4
5 21.08723 0.2305717 5 5
6 20.03432 0.3423527 6 6
7 19.36241 0.3935543 7 7
8 19.55144 0.4312422 8 8
9 18.34461 0.2839403 9 9
10 17.88718 0.9743811 10 10
11 18.24095 0.5178554 11 11
12 16.43083 0.7020082 12 12
13 13.28446 0.9563793 1 13
14 15.71997 0.7737180 2 14
15 19.11637 1.0874646 3 15
16 22.06385 0.9277122 4 16
17 24.44772 0.3887426 5 17
18 20.27052 0.3137290 6 18
19 19.56379 0.3456929 7 19
20 19.33246 0.3433963 8 20
21 18.55662 0.3047316 9 21
22 17.25356 0.7940866 10 22
23 15.45711 0.6400765 11 23
24 15.87066 0.7007103 12 24
25 14.76583 0.6811891 1 25
26 16.48400 0.8756550 2 26
27 19.05038 0.6741369 3 27
28 21.45588 0.7876674 4 28
29 23.84894 0.3214968 5 29
30 21.70421 0.2775853 6 30
31 19.18367 0.1340960 7 31
32 19.76713 0.3197615 8 32
33 18.28757 0.5319219 9 33
34 16.93758 0.9814153 10 34
35 15.12334 0.6050203 11 35
36 14.12159 0.5645782 12 36
37 13.01696 0.8034560 1 37
38 17.56584 0.5185198 2 38
39 20.77130 0.6376224 3 39
40 21.75943 0.6357125 4 40
41 22.54398 0.3640080 5 41
42 20.19191 0.5674044 6 42
43 20.04843 0.1816656 7 43
>}
Where T_matrix and V_matrix are the exogenous variables and with these I build the xreg for the regression.
xreg <- cbind(Mes = model.matrix(~as.factor(modeloajuste$M)),
mes = modeloajuste$Month, modeloajuste$T_matrix,
modeloajuste$V_matrix)
And this is how the xreg array looks like with the colnames change
> xreg
Ene feb mar abr may jun jul ago sep oct nov dic mes temp Vel
1 1 0 0 0 0 0 0 0 0 0 0 0 1 14.17802 0.8604688
2 1 1 0 0 0 0 0 0 0 0 0 0 2 15.79006 0.7736985
3 1 0 1 0 0 0 0 0 0 0 0 0 3 17.62116 0.8357023
4 1 0 0 1 0 0 0 0 0 0 0 0 4 22.05094 0.8797615
5 1 0 0 0 1 0 0 0 0 0 0 0 5 21.08723 0.2305717
6 1 0 0 0 0 1 0 0 0 0 0 0 6 20.03432 0.3423527
7 1 0 0 0 0 0 1 0 0 0 0 0 7 19.36241 0.3935543
8 1 0 0 0 0 0 0 1 0 0 0 0 8 19.55144 0.4312422
9 1 0 0 0 0 0 0 0 1 0 0 0 9 18.34461 0.2839403
10 1 0 0 0 0 0 0 0 0 1 0 0 10 17.88718 0.9743811
11 1 0 0 0 0 0 0 0 0 0 1 0 11 18.24095 0.5178554
12 1 0 0 0 0 0 0 0 0 0 0 1 12 16.43083 0.7020082
13 1 0 0 0 0 0 0 0 0 0 0 0 13 13.28446 0.9563793
14 1 1 0 0 0 0 0 0 0 0 0 0 14 15.71997 0.7737180
15 1 0 1 0 0 0 0 0 0 0 0 0 15 19.11637 1.0874646
16 1 0 0 1 0 0 0 0 0 0 0 0 16 22.06385 0.9277122
17 1 0 0 0 1 0 0 0 0 0 0 0 17 24.44772 0.3887426
18 1 0 0 0 0 1 0 0 0 0 0 0 18 20.27052 0.3137290
19 1 0 0 0 0 0 1 0 0 0 0 0 19 19.56379 0.3456929
20 1 0 0 0 0 0 0 1 0 0 0 0 20 19.33246 0.3433963
21 1 0 0 0 0 0 0 0 1 0 0 0 21 18.55662 0.3047316
22 1 0 0 0 0 0 0 0 0 1 0 0 22 17.25356 0.7940866
23 1 0 0 0 0 0 0 0 0 0 1 0 23 15.45711 0.6400765
24 1 0 0 0 0 0 0 0 0 0 0 1 24 15.87066 0.7007103
25 1 0 0 0 0 0 0 0 0 0 0 0 25 14.76583 0.6811891
26 1 1 0 0 0 0 0 0 0 0 0 0 26 16.48400 0.8756550
27 1 0 1 0 0 0 0 0 0 0 0 0 27 19.05038 0.6741369
28 1 0 0 1 0 0 0 0 0 0 0 0 28 21.45588 0.7876674
29 1 0 0 0 1 0 0 0 0 0 0 0 29 23.84894 0.3214968
30 1 0 0 0 0 1 0 0 0 0 0 0 30 21.70421 0.2775853
31 1 0 0 0 0 0 1 0 0 0 0 0 31 19.18367 0.1340960
32 1 0 0 0 0 0 0 1 0 0 0 0 32 19.76713 0.3197615
33 1 0 0 0 0 0 0 0 1 0 0 0 33 18.28757 0.5319219
34 1 0 0 0 0 0 0 0 0 1 0 0 34 16.93758 0.9814153
35 1 0 0 0 0 0 0 0 0 0 1 0 35 15.12334 0.6050203
36 1 0 0 0 0 0 0 0 0 0 0 1 36 14.12159 0.5645782
37 1 0 0 0 0 0 0 0 0 0 0 0 37 13.01696 0.8034560
38 1 1 0 0 0 0 0 0 0 0 0 0 38 17.56584 0.5185198
39 1 0 1 0 0 0 0 0 0 0 0 0 39 20.77130 0.6376224
40 1 0 0 1 0 0 0 0 0 0 0 0 40 21.75943 0.6357125
41 1 0 0 0 1 0 0 0 0 0 0 0 41 22.54398 0.3640080
42 1 0 0 0 0 1 0 0 0 0 0 0 42 20.19191 0.5674044
43 1 0 0 0 0 0 1 0 0 0 0 0 43 20.04843 0.1816656
>
However, when inserting the xreg array into the auto.arima of the code, the above error is displayed
plots <- list()
for (i in seq(6)) {
fit_arimax <- auto.arima(consumo, xreg = fourier(xreg, K = c(i,i)), seasonal = F, lambda = NULL)
plots[[i]] <- autoplot(main = "",forecast::forecast(fit_arimax, xreg=fourier(xreg, K=c(i,i)), h = 12)) +
xlab(paste("K =", i, "AICC =", round(fit[["aicc"]],2))) +
ylab("Consumo, MWh") }
gridExtra::grid.arrange(plots[[1]],plots[[2]],plots[[3]],plots[[4]], plots[[5]], plots[[6]], nrow=3)
Error in ...fourier(x, K, 1:NROW(x)) :
Number of periods does not match number of orders
So far, I have not been able to find how to fix this error, could someone help me?