Here is an example using ets model and arima model from forecast package in R. I am using the 2000th series of the M3 competiton, which already is divided into the training series- M3[[2000]]$x and the test data- M3[[2000]]$x. This monthly data.
#Load libraries
library(forecast)
library(Mcomp)
#View 2000th series of the M3 competition
M3[[2000]]
Series: M599
Type of series: INDUSTRY
Period of series: MONTHLY
Series description: Superphosphate & other phosphatic fertilizers, stocks
HISTORICAL data
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1979 2440 2175 1800 1795 2075 1985 1775 1610 1525 1440 1885 1830
1980 2055 2355 2475 2935 3160 2550 2305 2420 2575 2155 2410 1860
1981 5565 6740 7120 6915 6750 7130 7860 6660 6115 5885 6880 5340
1982 5985 6530 6585 6000 4645 4585 4990 4630 4545 4405 4415 4460
1983 4090 3865 3440 4100 5345 5115 5085 4060 3290 2910 3205 4220
1984 4335 4820 4925 4735 4225 4765 4060 4105 4200 4570 5935 5895
1985 5380 5395 4955 3520 3865 4805 4635 4165 3990 3995 5420 5645
1986 5000 4170 3890 2795 3355 4065 3815 3240 2545 3300 4075 3875
1987 3355 2555 2005 2100 3160 2760 3080 3075 2695 3355 4160 3520
1988 3750 3810 2905 3100 3900 3450 3630 3640 2850 3010 3745 4395
1989 4300 4945 4850 4565 4370 5055
FUTURE data
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1989 4810 4130 3845 4260 4715 4710
1990 4510 4255 3660 3220 3615 3210 3015 2820 3020 3315 3920 3690
#The training series
M3[[2000]]$x
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1979 2440 2175 1800 1795 2075 1985 1775 1610 1525 1440 1885 1830
1980 2055 2355 2475 2935 3160 2550 2305 2420 2575 2155 2410 1860
1981 5565 6740 7120 6915 6750 7130 7860 6660 6115 5885 6880 5340
1982 5985 6530 6585 6000 4645 4585 4990 4630 4545 4405 4415 4460
1983 4090 3865 3440 4100 5345 5115 5085 4060 3290 2910 3205 4220
1984 4335 4820 4925 4735 4225 4765 4060 4105 4200 4570 5935 5895
1985 5380 5395 4955 3520 3865 4805 4635 4165 3990 3995 5420 5645
1986 5000 4170 3890 2795 3355 4065 3815 3240 2545 3300 4075 3875
1987 3355 2555 2005 2100 3160 2760 3080 3075 2695 3355 4160 3520
1988 3750 3810 2905 3100 3900 3450 3630 3640 2850 3010 3745 4395
1989 4300 4945 4850 4565 4370 5055
#The test data (the test series)
M3[[2000]]$xx
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1989 4810 4130 3845 4260 4715 4710
1990 4510 4255 3660 3220 3615 3210 3015 2820 3020 3315 3920 3690
#ets and auto.arima
ets.model <- ets(M3[[2000]]$x)
arima.model <- auto.arima(M3[[2000]]$x)
#View ets model
ets.model
ETS(A,N,N)
Call:
ets(y = M3[[2000]]$x)
Smoothing parameters:
alpha = 0.9992
Initial states:
l = 2424.2309
sigma: 660.0962
AIC AICc BIC
2251.453 2251.649 2259.962
#View auto.arima model
arima.model
Series: M3[[2000]]$x
ARIMA(2,0,0)(1,0,0)[12] with non-zero mean
Coefficients:
ar1 ar2 sar1 mean
1.0375 -0.1631 0.2632 3956.6301
s.e. 0.0882 0.0884 0.0843 547.6591
sigma^2 estimated as 386813: log likelihood=-988.58
AIC=1987.16 AICc=1987.66 BIC=2001.34
#Forecasted values from ETS
ets.forecast <- forecast(ets.model,M3[[2000]]$h)
ets.forecast
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jul 1989 5054.439 4208.491 5900.386 3760.67401 6348.204
Aug 1989 5054.439 3858.579 6250.299 3225.52866 6883.349
Sep 1989 5054.439 3590.015 6518.862 2814.79659 7294.081
Oct 1989 5054.439 3363.584 6745.294 2468.49948 7640.378
Nov 1989 5054.439 3164.083 6944.794 2163.38932 7945.488
Dec 1989 5054.439 2983.715 7125.163 1887.53936 8221.338
Jan 1990 5054.439 2817.845 7291.033 1633.86327 8475.014
Feb 1990 5054.439 2663.454 7445.423 1397.74331 8711.134
Mar 1990 5054.439 2518.445 7590.432 1175.97172 8932.906
Apr 1990 5054.439 2381.291 7727.586 966.21281 9142.665
May 1990 5054.439 2250.839 7858.038 766.70318 9342.174
Jun 1990 5054.439 2126.193 7982.685 576.07284 9532.805
Jul 1990 5054.439 2006.640 8102.238 393.23224 9715.645
Aug 1990 5054.439 1891.602 8217.275 217.29796 9891.580
Sep 1990 5054.439 1780.605 8328.273 47.54191 10061.336
Oct 1990 5054.439 1673.249 8435.628 -116.64439 10225.522
Nov 1990 5054.439 1569.199 8539.679 -275.77567 10384.653
Dec 1990 5054.439 1468.166 8640.711 -430.29193 10539.169
class(ets.forecast)
[1] "forecast"
#Forecasted values from auto.arima
arima.forecast <- forecast(arima.model,M3[[2000]]$h)
arima.forecast
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jul 1989 5078.727 4281.675 5875.779 3859.741 6297.713
Aug 1989 4925.779 3777.235 6074.323 3169.233 6682.326
Sep 1989 4560.321 3200.504 5920.139 2480.660 6639.983
Oct 1989 4464.332 2969.673 5958.990 2178.449 6750.215
Nov 1989 4540.178 2956.018 6124.338 2117.414 6962.942
Dec 1989 4611.770 2966.717 6256.823 2095.878 7127.661
Jan 1990 4502.753 2815.622 6189.885 1922.508 7082.998
Feb 1990 4601.547 2885.046 6318.048 1976.385 7226.709
Mar 1990 4516.644 2779.509 6253.780 1859.924 7173.364
Apr 1990 4391.065 2639.368 6142.763 1712.075 7070.056
May 1990 4297.044 2535.039 6059.050 1602.289 6991.799
Jun 1990 4441.252 2671.935 6210.570 1735.315 7147.190
Jul 1990 4417.052 2614.331 6219.773 1660.028 7174.076
Aug 1990 4351.097 2518.027 6184.167 1547.659 7154.535
Sep 1990 4233.219 2377.617 6088.821 1395.321 7071.118
Oct 1990 4189.633 2317.936 6061.331 1327.119 7052.147
Nov 1990 4194.121 2311.001 6077.240 1314.138 7074.103
Dec 1990 4199.897 2308.671 6091.122 1307.517 7092.277
class(arima.forecast)
[1] "forecast"
Class of ets.forecast and arima.forecast-“forecast”. Поэтому пока не надо использовать dygraphs package. Если мы хотим ипользовать dygraphs package то сначала надо преоброзовать ets.forecast and arima.forecast в xts object.
#Plot ets.forecast
plot(ets.forecast)
#Plot arima.forecast
plot(arima.forecast)
#Plot test data using dygraphs pacakge
library(dygraphs)
dygraph(M3[[2000]]$xx) %>%
dyRangeSelector()