# data modelM=c("diff",1,"diff",12)r =Resid(chicken,M)test(r)
Null hypothesis: Residuals are iid noise.
Test Distribution Statistic p-value
Ljung-Box Q Q ~ chisq(20) 236.56 0 *
McLeod-Li Q Q ~ chisq(20) 69.81 0 *
Turning points T (T-110)/5.4 ~ N(0,1) 85 0 *
Diff signs S (S-83)/3.7 ~ N(0,1) 89 0.1088
Rank P (P-6930.5)/361.3 ~ N(0,1) 6323 0.0927
Test stationary
# Augmented Dickey-Fuller Test# H0: X_t is a non-stationary time series# H1: X_t is a stationary time serieslibrary(aTSA)adf.test(r)
Augmented Dickey-Fuller Test
alternative: stationary
Type 1: no drift no trend
lag ADF p.value
[1,] 0 -5.00 0.01
[2,] 1 -5.54 0.01
[3,] 2 -5.42 0.01
[4,] 3 -4.45 0.01
[5,] 4 -4.15 0.01
Type 2: with drift no trend
lag ADF p.value
[1,] 0 -4.98 0.01
[2,] 1 -5.53 0.01
[3,] 2 -5.41 0.01
[4,] 3 -4.43 0.01
[5,] 4 -4.14 0.01
Type 3: with drift and trend
lag ADF p.value
[1,] 0 -5.03 0.01
[2,] 1 -5.58 0.01
[3,] 2 -5.46 0.01
[4,] 3 -4.47 0.01
[5,] 4 -4.17 0.01
----
Note: in fact, p.value = 0.01 means p.value <= 0.01
# Results in stationary data. No differencing is needed.
SARIMA models
# Potential Values# based on the PACF# p = 0, 1, 2# P = 0, 1, 2# based on the ACF# q = 0, 1, 2, 3, 9, 10, 11# Q = 0, 1, 2,# Eliminate Trend and Season# d = 1# D = 1# SARIMA (1,1,1)x(1,1,1)12fit1 =arima(chicken,order =c(1,1,1),seasonal =list(order=c(1,1,1),period=12))test(fit1$residuals)
Null hypothesis: Residuals are iid noise.
Test Distribution Statistic p-value
Ljung-Box Q Q ~ chisq(20) 26.41 0.1526
McLeod-Li Q Q ~ chisq(20) 26.03 0.165
Turning points T (T-118.7)/5.6 ~ N(0,1) 113 0.314
Diff signs S (S-89.5)/3.9 ~ N(0,1) 90 0.8976
Rank P (P-8055)/404.2 ~ N(0,1) 7885 0.674
# Auto fitbestmodel =auto.arima(chicken, trace=TRUE)
Fitting models using approximations to speed things up...
ARIMA(2,1,2)(1,0,1)[12] with drift : Inf
ARIMA(0,1,0) with drift : 512.1327
ARIMA(1,1,0)(1,0,0)[12] with drift : 357.8553
ARIMA(0,1,1)(0,0,1)[12] with drift : 399.0111
ARIMA(0,1,0) : 521.4674
ARIMA(1,1,0) with drift : 380.5252
ARIMA(1,1,0)(2,0,0)[12] with drift : 361.1736
ARIMA(1,1,0)(1,0,1)[12] with drift : Inf
ARIMA(1,1,0)(0,0,1)[12] with drift : 361.7262
ARIMA(1,1,0)(2,0,1)[12] with drift : Inf
ARIMA(0,1,0)(1,0,0)[12] with drift : 496.251
ARIMA(2,1,0)(1,0,0)[12] with drift : 349.6098
ARIMA(2,1,0) with drift : 361.2204
ARIMA(2,1,0)(2,0,0)[12] with drift : 352.9452
ARIMA(2,1,0)(1,0,1)[12] with drift : Inf
ARIMA(2,1,0)(0,0,1)[12] with drift : 347.5637
ARIMA(2,1,0)(0,0,2)[12] with drift : 348.8669
ARIMA(2,1,0)(1,0,2)[12] with drift : Inf
ARIMA(3,1,0)(0,0,1)[12] with drift : 346.8076
ARIMA(3,1,0) with drift : 359.6763
ARIMA(3,1,0)(1,0,1)[12] with drift : Inf
ARIMA(3,1,0)(0,0,2)[12] with drift : 348.5706
ARIMA(3,1,0)(1,0,0)[12] with drift : 350.3946
ARIMA(3,1,0)(1,0,2)[12] with drift : Inf
ARIMA(4,1,0)(0,0,1)[12] with drift : 349.2894
ARIMA(3,1,1)(0,0,1)[12] with drift : 347.6645
ARIMA(2,1,1)(0,0,1)[12] with drift : 346.754
ARIMA(2,1,1) with drift : 359.2506
ARIMA(2,1,1)(1,0,1)[12] with drift : Inf
ARIMA(2,1,1)(0,0,2)[12] with drift : 348.4092
ARIMA(2,1,1)(1,0,0)[12] with drift : 350.2436
ARIMA(2,1,1)(1,0,2)[12] with drift : Inf
ARIMA(1,1,1)(0,0,1)[12] with drift : 354.7731
ARIMA(2,1,2)(0,0,1)[12] with drift : 348.0198
ARIMA(1,1,2)(0,0,1)[12] with drift : 348.7836
ARIMA(3,1,2)(0,0,1)[12] with drift : 349.5839
ARIMA(2,1,1)(0,0,1)[12] : 347.2775
Now re-fitting the best model(s) without approximations...
ARIMA(2,1,1)(0,0,1)[12] with drift : 351.5017
Best model: ARIMA(2,1,1)(0,0,1)[12] with drift
test(bestmodel$residuals)
Null hypothesis: Residuals are iid noise.
Test Distribution Statistic p-value
Ljung-Box Q Q ~ chisq(20) 20.39 0.4339
McLeod-Li Q Q ~ chisq(20) 25.62 0.1789
Turning points T (T-118.7)/5.6 ~ N(0,1) 126 0.1926
Diff signs S (S-89.5)/3.9 ~ N(0,1) 96 0.0942
Rank P (P-8055)/404.2 ~ N(0,1) 8124 0.8644
Fitting models using approximations to speed things up...
ARIMA(2,1,2) with drift : 347.0167
ARIMA(0,1,0) with drift : 488.3928
ARIMA(1,1,0) with drift : 366.5475
ARIMA(0,1,1) with drift : 397.7907
ARIMA(0,1,0) : 499.836
ARIMA(1,1,2) with drift : 351.2362
ARIMA(2,1,1) with drift : 344.8664
ARIMA(1,1,1) with drift : 357.355
ARIMA(2,1,0) with drift : 347.3834
ARIMA(3,1,1) with drift : 344.7761
ARIMA(3,1,0) with drift : 345.4701
ARIMA(4,1,1) with drift : Inf
ARIMA(3,1,2) with drift : Inf
ARIMA(4,1,0) with drift : 348.4639
ARIMA(4,1,2) with drift : Inf
ARIMA(3,1,1) : Inf
Now re-fitting the best model(s) without approximations...
ARIMA(3,1,1) with drift : Inf
ARIMA(2,1,1) with drift : 349.2716
Best model: ARIMA(2,1,1) with drift
M =c("diff",1,"diff",12)r =Resid(empl_data,M)test(r)
Null hypothesis: Residuals are iid noise.
Test Distribution Statistic p-value
Ljung-Box Q Q ~ chisq(20) 36.26 0.0143 *
McLeod-Li Q Q ~ chisq(20) 16.13 0.7087
Turning points T (T-68)/4.3 ~ N(0,1) 73 0.2408
Diff signs S (S-51.5)/3 ~ N(0,1) 54 0.398
Rank P (P-2678)/178 ~ N(0,1) 2339 0.0569
p =c(0,1,6,11)P =c(0,1)q =c(0,1,11)Q =c(0,1)all =expand_grid(p,P,q,Q)# this function fit a SARIM model, forecast, and calculate the mapefit =function(x,p,d,q,P,D,Q,s,h){# x trainx_train = x[1:I(length(x)-h)] # fit the modelft =arima(x_train, order =c(p,d,q) ,seasonal =list(order=c(P,D,Q),period=s))# predict the h ahead point using SARIMAprd =predict(ft,h)$pred[1:h]# fit and predict using Holt-WintersHWmodel =HoltWinters(ts(x_train,start=1,frequency = s),seasonal ="mult")prdhw = forecast::forecast(HWmodel,h)# fit and predict using ARARprdarar =arar(x_train,h,opt=0)# actual dataactual = x[I(length(x)-h+1):length(x)]# return the mapereturn(c(sarima=100*mape(actual,prd),arar =100*mape(actual,prdarar$pred),hw =100*mape(actual,prdhw$mean)))}fit(chicken,1,1,1,1,1,1,12,10)