Hi guys!
I'm trying to make a forecastmachine in r but got stuck.
I try to predict a dynamic regression model using the temperature per month.
To predict the temperature of '21 I want to use the snaive () function. But how do I apply this in the forecast () with regression.
This may seem a bit messy, that is because it is part of an (almost) automated code.
START<- c(2014,1)
Fq<- 12
liters<-data[,"Liters"]
temp<- data[,"Tempratuur"]
tsl<- ts(liters, frequency=Fq, start=START)
X1<- 6 #length test set
X2<- length(tsl)-(X1-1) #Length training set
Y<- 12 #Length forecast
dput(temp) and dput(liters) are at the bottom
Now the code
reg1<- ts(temp, frequency=Fq, start=START)
reg1training<-subset(reg1, end=length(tsl)-X1)
reg1test<- subset(reg1, start=X2)
lamb<- BoxCox.lambda(training)
txt<- auto.arima(training, xreg=reg1training,
lambda=lamb, stepwise=FALSE)
#> Series: training
#> Regression with ARIMA(0,1,1)(0,1,1)[12] errors
#> Box Cox transformation: lambda= -0.1176994
#>
#> Coefficients:
#> ma1 sma1 xreg
#> -0.6507 -0.4964 0.1057
#> s.e. 0.0897 0.1514 0.0188
#>
#> sigma^2 estimated as 0.09377: log likelihood=-15.75
#> AIC=39.49 AICc=40.16 BIC=48.19
ntxt<- Arima(tsl,
order=arimaorder(txt)[1:3],
seasonal=arimaorder(txt)[4:6],
lambda=lamb)
#> Series: tsl
#> Regression with ARIMA(0,1,1)(0,1,1)[12] errors
#> Box Cox transformation: lambda= -0.1176994
#>
#> Coefficients:
#> ma1 sma1 tempratuur
#> -0.6870 -0.4226 0.0939
#> s.e. 0.0779 0.1587 0.0188
#>
#> sigma^2 estimated as 0.1023: log likelihood=-19.79
#> AIC=47.58 AICc=48.18 BIC=56.63
So what i would like now is something like this:
reg<-snaive(ts(temp, frequency=Fq, start=START),h=Y)
forecast(ntxt,xreg=reg)
But that gives
#>Error in forecast.forecast_ARIMA(ntxt, xreg = reg) :
#> xreg should be a numeric matrix or a numeric vector
Could someone please help me?
Thank you in advance!
.
.
.
.
.
.
.
.
> dput(liters)
structure(list(test = c(7.21032258064516, 17.2285714285714, 76.3225806451613,
84.37, 215.516129032258, 431.293333333333, 745.207741935484,
539.334193548387, 655.362, 367.974193548387, 236.646666666667,
450.588387096774, 43.5716129032258, 66.04, 276.847741935484,
282.619333333333, 566.998709677419, 1095.84266666667, 1187.29161290323,
1726.45161290323, 1228.92, 655.529032258065, 746.288666666667,
2058.73548387097, 51.2283870967742, 215.889655172414, 440.936129032258,
546.156, 1347.63935483871, 1996.08933333333, 1990.58709677419,
2641.42451612903, 2540.032, 1096.66451612903, 422.928, 1732.26838709677,
71.0477419354839, 182.357142857143, 396.425806451613, 1027.65,
1599.73935483871, 3255.12866666667, 2865.75483870968, 3925.91612903226,
2232.9, 2148.5264516129, 873.73, 1782.78580645161, 354.518709677419,
15.81, 397.767096774194, 1355.744, 2438.12903225806, 3957.012,
5153.51612903226, 4435.99483870968, 3508.008, 1976.56516129032,
986.362, 3491.30322580645, 333.44, 359.325, 1176.71806451613,
1387.1, 1956.2064516129, 4006.08, 5792.94967741935, 6212.12903225807,
4270.84, 2047.86903225806, 714.922666666667, 3568.59870967742,
442.003225806452, 378.063448275862, 706.285161290323, 307.008,
712.175483870968, 2856.568, 2208.59612903226, 3378.91806451613,
2642.112, 1065.01161290323, 141.542666666667, 2720.94967741935
)), row.names = c(NA, -84L), class = c("tbl_df", "tbl", "data.frame"
))
> dput(temp)
structure(list(tempratuur = c(4.50645161290323, 5.74285714285714,
7.33870967741935, 10.8166666666667, 12.6774193548387, 15.4033333333333,
19.4064516129032, 16.0516129032258, 15.83, 13.141935483871, 7.68333333333333,
4.65483870967742, 3.89032258064516, 3.175, 5.81612903225806,
7.55666666666667, 11.0741935483871, 14.0133333333333, 17.358064516129,
17.9838709677419, 13.3, 9.93225806451613, 9.54333333333333, 8.93548387096774,
3.28387096774194, 4.15172413793103, 4.80322580645161, 7.78, 13.6677419354839,
15.6433333333333, 17.4612903225806, 17.1967741935484, 17.4933333333333,
9.7741935483871, 5.34, 4.98064516129032, 1.55806451612903, 4.10714285714286,
7.45161290322581, 7.75, 13.6032258064516, 16.4566666666667, 17.058064516129,
16.7774193548387, 13.7833333333333, 12.9741935483871, 7.15, 4.6258064516129,
4.61612903225806, 0.221428571428571, 3.54516129032258, 10.8633333333333,
15.6290322580645, 15.93, 19.0870967741935, 18.0032258064516,
14.6533333333333, 12.1709677419355, 6.43, 6.06129032258064, 3.36129032258064,
5.70357142857143, 7.18387096774194, 9.7, 10.7483870967742, 17.12,
18.0129032258065, 18.2709677419355, 14.5266666666667, 11.0935483870968,
6.29333333333333, 5.63225806451613, 6.05483870967742, 6.38620689655172,
6.06774193548387, 9.36, 11.5612903225806, 16.6466666666667, 15.958064516129,
19.6677419354839, 14.8266666666667, 11.1870967741935, 8.52666666666667,
5.06129032258064)), row.names = c(NA, -84L), class = c("tbl_df",
"tbl", "data.frame"))