简体   繁体   中英

r auto.arima results mismatch if runned with apply from a data.frame

summary : I need to forecast 25 variables of time-series, but result doesn't match between running one by one vs apply :

cpi_fit <- auto.arima(cpi_ts[,1])

vs

cpi_fit_ply <- apply(cpi_ts, 2, function(x) auto.arima(x) )

sample data set and my script : my original data is some FMCG consumption trends but for data privacy issues I can't share, so I prepare 3 components of Turkey Consumer Prices Index trend. Food, Clothes and Shoes

cpi <- data.frame( food = c(93.43 , 96.25 , 101.29 , 102.78 , 103.73 , 101.11 , 98.91 , 97.38 , 98.55 , 100.03 , 102.76 , 103.78 , 104.88 , 106.29 , 108 , 108.28 , 106.43 , 103.46 , 103.72 , 104.58 , 105.1 , 108.32 , 109.81 , 110.3 , 111.72 , 112.75 , 112.85 , 111.65 , 111.63 , 109.66 , 108.24 , 108.11 , 109.6 , 112.21 , 116.11 , 115.7 , 117.28 , 120.25 , 121.91 , 122.69 , 123.61 , 119.97 , 121.6 , 119.91 , 123.52 , 124.5 , 128.07 , 128.83 , 134.85 , 137.07 , 137.92 , 139.28 , 136.85 , 133.97 , 132.91 , 135.21 , 138.41 , 143.41 , 144.2 , 144.69 , 147.62 , 155.47 , 157.05 , 158.63 , 158.99 , 153.07 , 153.67 , 153.56 , 153.89 , 160.24 , 161.4 , 162.16 , 164.64 , 164.59 , 171.44 , 170.75 , 170.99 , 168.06 , 166.87 , 164.06 , 164.4 , 169.54 , 173.52 , 177.59 , 180.56 , 190.24 , 191.5 , 191.72 , 182.72 , 177.71 , 176.19 , 181.67 , 190.8 , 199.96 , 195.91 , 190.36 , 193.58 , 198.74 , 197.84 , 196.83 , 206.72 , 192.48 , 189.91 , 192.64 , 194.55 , 202.83 , 209.76 , 214.03 , 216.51 , 221.34 , 220.36 , 219.95 , 214.22 , 206.24 , 207 , 210.2 , 215.02 , 218.76 , 218.29 , 221.92 , 231.4 , 233.57 , 238.59 , 235.25 , 229.64 , 234.09 , 234.64 , 232.65 , 234.37 , 244.15 , 240.52 , 244.14 , 257.57 , 257.79 , 263.46)
                   ,clothes = c(95.41 , 93.4 , 92.89 , 96.69 , 100.51 , 101.34 , 99.49 , 97.07 , 100.3 , 105.25 , 109.01 , 108.65 , 105.01 , 100.67 , 99.11 , 101.8 , 107.45 , 108.46 , 103.83 , 100.9 , 102.64 , 111.09 , 116.34 , 116.11 , 109.61 , 101.85 , 97.89 , 105.61 , 117 , 118.21 , 108.62 , 102.94 , 103.08 , 111.17 , 114.87 , 113.65 , 106.36 , 96.99 , 94.05 , 103.51 , 117.46 , 118.84 , 109.99 , 102.1 , 102.6 , 112.98 , 118.89 , 116.12 , 105.82 , 98.62 , 97.56 , 108.56 , 123.53 , 125.32 , 115.14 , 106.83 , 106.86 , 115.66 , 120.89 , 119.06 , 107.13 , 99.43 , 100.53 , 112.1 , 128.6 , 128.23 , 117.98 , 110.59 , 109.4 , 118.68 , 121.98 , 116.98 , 107.1 , 101.11 , 100.87 , 111.61 , 126.64 , 126.24 , 119.23 , 112.57 , 109.36 , 118.82 , 124.76 , 121.25 , 111.21 , 105.21 , 105.44 , 117.18 , 132.78 , 133.73 , 126.87 , 120.4 , 116.19 , 125.01 , 130.13 , 127.58 , 116.96 , 111.05 , 111.67 , 124.44 , 141.86 , 143.16 , 136.14 , 129.68 , 123.99 , 133.72 , 141.77 , 138.06 , 127.07 , 121.74 , 122.4 , 139.15 , 153.43 , 151.74 , 145.23 , 138.92 , 133.66 , 142.77 , 152.47 , 149.51 , 138.63 , 130.09 , 130.55 , 148.12 , 162.69 , 159.35 , 151.72 , 146.12 , 140.89 , 155.66 , 161.03 , 156.01 , 144.03 , 136.96 , 139.27)
                   , shoes = c(94.75 , 94.41 , 94.4 , 97.83 , 99.97 , 99.95 , 99.35 , 98.95 , 101.37 , 104.39 , 107.39 , 107.24 , 105.92 , 103.39 , 101.08 , 104.01 , 108.32 , 109.1 , 106.68 , 103.7 , 106.79 , 112.8 , 117.17 , 118.23 , 114.09 , 106.78 , 104.85 , 115.87 , 120.16 , 120.38 , 113.66 , 108.45 , 112.66 , 119.04 , 123.94 , 124.78 , 117.56 , 109.86 , 106.95 , 114.28 , 118.35 , 118.12 , 111.81 , 104.94 , 113.6 , 123.13 , 127.53 , 126.2 , 117.25 , 110.79 , 113.08 , 126.01 , 131.58 , 132.65 , 124.11 , 115.59 , 123.1 , 133.12 , 137.98 , 137.32 , 122.48 , 114.93 , 119.37 , 133.97 , 139.48 , 138.33 , 127.25 , 120.43 , 127.12 , 137.8 , 140.81 , 136.09 , 125.79 , 118.84 , 120.03 , 134.26 , 141.53 , 138.21 , 128.17 , 123.04 , 129.29 , 139.06 , 143.49 , 139.58 , 128.47 , 122.37 , 125.05 , 136.11 , 141.95 , 140.64 , 133.03 , 128.14 , 131.11 , 141.25 , 145.7 , 144 , 135.46 , 128.06 , 129.92 , 141.91 , 147.96 , 147.46 , 140.66 , 136.49 , 137.52 , 149.17 , 155.41 , 154.36 , 142.61 , 137.35 , 141.06 , 157.62 , 163.14 , 163.31 , 155.66 , 147.19 , 148.98 , 159.12 , 166.53 , 166.42 , 156.63 , 145.67 , 149.24 , 164.96 , 174.38 , 173.16 , 165.61 , 160.04 , 163.12 , 174.13 , 178.82 , 177.29 , 164.32 , 155.23 , 163.05)
                   )

cpi_ts <- ts(cpi, start=c(2003, 1), end=c(2014, 3), frequency=12) 

cpi_fit <- auto.arima(cpi_ts[,1])
cpi_forecast <- forecast(cpi_fit, level=c(99), h=6)

cpi_fit
cpi_forecast

cpi_fit_ply <- apply(cpi_ts, 2, function(x) auto.arima(x) )
cpi_forecast_ply <- apply(cpi_ts, 2, function(x) forecast(auto.arima(x), level=c(99), h=6) )

cpi_fit_ply$food
cpi_forecast_ply$food

and here is my results for only one:

> cpi_fit
Series: cpi_ts[, 1] 
ARIMA(2,1,2)(1,0,1)[12] with drift         

Coefficients:
         ar1      ar2      ma1   ma2    sar1     sma1   drift
      1.3425  -0.6305  -1.4266  0.57  0.9430  -0.7661  1.1721
s.e.     NaN      NaN      NaN   NaN  0.0473   0.1061  0.3132

sigma^2 estimated as 10.38:  log likelihood=-347.13
AIC=710.26   AICc=711.41   BIC=733.44
> cpi_forecast
         Point Forecast    Lo 99    Hi 99
Apr 2014       261.3114 253.0115 269.6112
May 2014       258.2686 247.0133 269.5239
Jun 2014       254.2368 241.4043 267.0692
Jul 2014       253.6520 239.9956 267.3085
Aug 2014       254.6313 240.5144 268.7482
Sep 2014       257.5707 243.1288 272.0125

and this is the result of food, which I produce with apply:

> cpi_fit_ply$food
Series: x 
ARIMA(4,1,3) with drift         

Coefficients:
         ar1     ar2      ar3      ar4      ma1      ma2     ma3   drift
      0.8093  0.4999  -0.7247  -0.0669  -0.8446  -0.7004  0.8292  1.1936
s.e.  0.1217  0.1617   0.1398   0.1015   0.0892   0.1221  0.0831  0.1706

sigma^2 estimated as 11.13:  log likelihood=-349.82
AIC=717.64   AICc=719.1   BIC=743.72
> cpi_forecast_ply$food
    Point Forecast    Lo 99    Hi 99
136       263.1505 254.5564 271.7446
137       260.3486 248.4072 272.2900
138       258.1259 244.6140 271.6377
139       255.3471 240.7681 269.9260
140       254.6141 239.6324 269.5957
141       255.0057 239.7582 270.2532

Any help will be appreciated, thanks in advance, Selcuk

You lose all the time series attributes when you use apply . In this case, you lose the information about seasonality. So auto.arima does not know that the data are seasonal and therefore cannot consider seasonal models with period 12.

You can fix the problem as follows:

cpi_fit_ply <- apply(cpi_ts, 2, 
         function(x) auto.arima(ts(x,start=c(2003, 1), 
                  end=c(2014, 3), frequency=12) ) )
cpi_forecast_ply <- apply(cpi_ts, 2, 
         function(x) forecast(auto.arima(ts(x,start=c(2003, 1), 
                  end=c(2014, 3), frequency=12)), level=c(99), h=6) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM