7.8 13
first time series:ausbeer
library(fpp)
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.4.4
## Loading required package: fma
## Loading required package: expsmooth
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 3.4.4
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 3.4.4
ausbeer
## Qtr1 Qtr2 Qtr3 Qtr4
## 1956 284 213 227 308
## 1957 262 228 236 320
## 1958 272 233 237 313
## 1959 261 227 250 314
## 1960 286 227 260 311
## 1961 295 233 257 339
## 1962 279 250 270 346
## 1963 294 255 278 363
## 1964 313 273 300 370
## 1965 331 288 306 386
## 1966 335 288 308 402
## 1967 353 316 325 405
## 1968 393 319 327 442
## 1969 383 332 361 446
## 1970 387 357 374 466
## 1971 410 370 379 487
## 1972 419 378 393 506
## 1973 458 387 427 565
## 1974 465 445 450 556
## 1975 500 452 435 554
## 1976 510 433 453 548
## 1977 486 453 457 566
## 1978 515 464 431 588
## 1979 503 443 448 555
## 1980 513 427 473 526
## 1981 548 440 469 575
## 1982 493 433 480 576
## 1983 475 405 435 535
## 1984 453 430 417 552
## 1985 464 417 423 554
## 1986 459 428 429 534
## 1987 481 416 440 538
## 1988 474 440 447 598
## 1989 467 439 446 567
## 1990 485 441 429 599
## 1991 464 424 436 574
## 1992 443 410 420 532
## 1993 433 421 410 512
## 1994 449 381 423 531
## 1995 426 408 416 520
## 1996 409 398 398 507
## 1997 432 398 406 526
## 1998 428 397 403 517
## 1999 435 383 424 521
## 2000 421 402 414 500
## 2001 451 380 416 492
## 2002 428 408 406 506
## 2003 435 380 421 490
## 2004 435 390 412 454
## 2005 416 403 408 482
## 2006 438 386 405 491
## 2007 427 383 394 473
## 2008 420 390 410
tsausbeer=ts(ausbeer,frequency=4)#here, the data in quaters, so frequency=4
plot(tsausbeer)

#it has seaonality in the data
Seprate train and test(3years) data
trainaus=ts(tsausbeer[1:200],frequency=4)
testaus=ts(tsausbeer[201:211], frequency = 4)
Build forecasting ets model
fit1=forecast(ets(trainaus,model="ZZZ"),h=12)
plot(fit1)

accu1=accuracy(fit1,testaus[1:4])
Build forecasting snaive model
fit2=snaive(trainaus,h=12)
plot(fit2)

accu2=accuracy(fit2,testaus[1:4])
Build forecasting stlf model
fit3=stlf(trainaus,lambda=TRUE, h=12)
plot(fit3)

accu3=accuracy(fit3,testaus[1:4])
Compare the three models accuracy
compare=(rbind(accu1,accu2,accu3))
compare
## ME RMSE MAE MPE MAPE MASE
## Training set -0.4071783 15.95589 12.161081 -0.07590221 2.908499 0.2114693
## Test set 7.4311204 10.07427 7.770158 1.63441897 1.722253 0.1351155
## Training set 3.4540816 19.84159 15.984694 0.94049719 3.808452 0.2779582
## Test set 2.7500000 14.68843 12.750000 0.42773478 3.000178 0.2217101
## Training set 0.5804392 13.95378 10.848234 0.14903329 2.604410 0.1886402
## Test set 9.5194182 11.24329 9.519418 2.17594884 2.175949 0.1655334
## ACF1
## Training set -0.192444183
## Test set NA
## Training set -0.001505113
## Test set NA
## Training set -0.145835045
## Test set NA
#1st time series, by comparing RMSE, MAE, ME, MAPE,MASE, snaive model has the lowest value, and it will get higher accuracy
#for ausbeer, choose snaive model
Second time series: bricksq
str(bricksq)
## Time-Series [1:155] from 1956 to 1994: 189 204 208 197 187 214 227 223 199 229 ...
tsbricksq=ts(bricksq,frequency=4)#here, the data is in quaters, so frequency =4
plot(tsbricksq)

#seprate train and test(3years) data
trainbri=ts(tsbricksq[1:144],frequency=4)
testbri=ts(tsbricksq[145:155], frequency = 4)
#build forecasting ets model
fit1bri=forecast(ets(trainbri,model="ZZZ"),h=12)#forecast in next three years
plot(fit1bri)

accu1bri=accuracy(fit1bri,testbri[1:4])
#build forecasting snaive model
fit2bri=snaive(trainbri,h=12)
plot(fit2bri)

accu2bri=accuracy(fit2bri,testbri[1:4])
#build forecasting stlf model
fit3bri=stlf(trainbri,lambda=TRUE, h=12)
plot(fit3bri)

accu3bri=accuracy(fit3bri,testbri[1:4])
#compare the three models accuracy
comparebri=(rbind(accu1bri,accu2bri,accu3bri))
comparebri
## ME RMSE MAE MPE MAPE MASE
## Training set 1.371550 21.87030 15.76868 0.2948698 3.893113 0.4804861
## Test set -10.212718 14.42046 10.21272 -2.4507343 2.450734 0.3111909
## Training set 6.178571 50.13916 36.73571 1.3701875 8.985498 1.1193708
## Test set 2.000000 19.71040 18.50000 0.4135769 4.388557 0.5637119
## Training set 1.521243 20.02799 14.42477 0.3815756 3.552404 0.4395361
## Test set -11.970118 19.92711 14.48331 -2.8423816 3.491785 0.4413197
## ACF1
## Training set 0.1471234
## Test set NA
## Training set 0.8169686
## Test set NA
## Training set 0.2322316
## Test set NA
#For second time series, by comparing RMSE, MAE, ME, MAPE, MASE, the ets model has the lowest error, and it will get higher accuracy
#For bricksq: choose ets model
Third time series :dole
dole
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct
## 1956 4742 6128 6494 5379 6011 7003 9164 10333 9614 9545
## 1957 15711 13135 13077 15453 15995 18071 20291 20175 18975 17928
## 1958 29856 26879 24485 27745 27282 29418 29908 29278 26002 23826
## 1959 31486 28207 27669 27559 27924 27528 27410 24887 21904 19598
## 1960 23781 20020 18177 17732 16765 16310 14897 12940 11465 10364
## 1961 19257 20941 29718 35025 45110 57154 61499 62090 59561 48531
## 1962 56755 49740 45870 49136 47256 46324 45453 42333 36851 33952
## 1963 46178 40482 36394 37142 36424 38188 37174 31869 26575 21758
## 1964 28649 24226 21955 19937 18287 18129 17072 14924 12491 11160
## 1965 15831 13698 12111 12690 12585 12855 12137 10977 9993 9614
## 1966 19490 17611 16206 17560 18082 19482 19200 18918 17375 16122
## 1967 24911 21969 21956 20944 22200 24002 22951 20143 17187 15287
## 1968 26943 23735 20744 21090 21502 21275 19426 16798 14209 13357
## 1969 23460 19551 15898 16012 16054 15910 13873 11854 10138 9942
## 1970 17778 13854 12681 11328 11946 13043 12785 11937 11383 10282
## 1971 18337 16779 15504 17258 18264 19184 19453 18741 19087 18171
## 1972 37486 37303 37639 36536 35850 41581 42979 42490 37992 32454
## 1973 48622 39868 34511 37234 36675 37945 36593 31669 28682 25944
## 1974 46847 38315 32600 33349 30598 32009 37599 45999 54945 68394
## 1975 182260 184177 157547 168471 159020 160748 169631 170927 179898 176471
## 1976 248619 215342 192024 178765 182397 188423 197159 198648 195864 194125
## 1977 229415 245395 236383 226807 239984 250309 253809 254863 249551 254085
## 1978 269896 298455 290356 283308 272384 286091 290718 285424 284642 279874
## 1979 341877 357463 334400 332572 318905 311232 310000 303800 299566 286241
## 1980 334495 334265 316776 309300 308989 311232 313943 303555 290386 283822
## 1981 339700 347400 325500 315200 314900 314500 313700 318500 306000 299500
## 1982 351425 372288 358536 356004 375626 390664 404840 421856 446341 465959
## 1983 601931 632837 622819 622162 633272 635002 634020 622103 610379 599100
## 1984 674424 667059 626653 602100 600344 584506 580347 570553 565348 555279
## 1985 636841 636342 599092 580700 568574 561400 553644 541022 534700 522587
## 1986 609987 603156 578700 568400 569966 569761 573989 573735 566245 556055
## 1987 623079 619978 592892 582102 561698 550850 536522 525650 515893 492248
## 1988 517127 511023 493993 483400 481469 475070 472806 458767 441201 428578
## 1989 448572 441100 409708 393323 391918 390001 383839 377968 368060 360246
## 1990 385727 398961 390149 391108 411171 427931 441335 450824 452304 457658
## 1991 567249 580777 596890 616326 647415 676706 701677 709801 718748 720754
## 1992 779868 816124 818102 826297 838390 851831 856505
## Nov Dec
## 1956 10096 13277
## 1957 19782 26055
## 1958 22302 27565
## 1959 19037 22469
## 1960 11738 17633
## 1961 47541 56756
## 1962 33392 43153
## 1963 20978 29555
## 1964 10658 15451
## 1965 10459 20509
## 1966 17269 26261
## 1967 17394 26321
## 1968 14766 25092
## 1969 11262 19601
## 1970 11316 19652
## 1971 20825 36441
## 1972 34049 47598
## 1973 28443 40232
## 1974 85735 140772
## 1975 184528 231311
## 1976 192866 215655
## 1977 238332 256223
## 1978 278829 311279
## 1979 282902 297782
## 1980 284842 299500
## 1981 289800 304700
## 1982 493446 562592
## 1983 591442 629214
## 1984 552893 576900
## 1985 520756 552300
## 1986 540527 560166
## 1987 481236 495772
## 1988 419052 420900
## 1989 357443 374530
## 1990 480083 523798
## 1991 730105 751348
## 1992
str(dole)
## Time-Series [1:439] from 1956 to 1992: 4742 6128 6494 5379 6011 ...
tsdole=ts(dole,frequency=12)#here, the data in months, so frequency=12
plot(tsdole)

#do not have seaonality in the data
#seprate train and test(3years) data
traindole=ts(tsdole[1:408],frequency=12)
testdole=ts(tsdole[409:439], frequency = 12)
#build forecasting ets model
fit1dole=forecast(ets(traindole,model="ZZZ"),h=36)
plot(fit1dole)

accu1dole=accuracy(fit1dole,testdole[1:12])
#build forecasting snaive model
fit2dole=snaive(traindole,h=36)
plot(fit2dole)

accu2dole=accuracy(fit2dole,testdole[1:12])
#build forecasting stlf model
fit3dole=stlf(traindole,lambda=TRUE, h=36)
plot(fit3dole)

accu3dole=accuracy(fit3dole,testdole[1:12])
#compare the three models accuracy
comparedole=(rbind(accu1dole,accu2dole,accu3dole))
comparedole
## ME RMSE MAE MPE MAPE
## Training set 79.64032 16353.340 9496.106 0.5242479 6.153383
## Test set 23299.09221 65196.903 53494.898 4.2092139 11.824597
## Training set 11613.43939 56531.602 31909.737 3.0811779 27.613157
## Test set 42861.75000 76617.804 63988.083 8.6877774 14.093502
## Training set 262.57717 6369.339 3885.957 0.5528408 4.920648
## Test set 63534.12988 87482.709 71409.954 13.5480563 15.566115
## MASE ACF1
## Training set 1.2464083 0.52284216
## Test set 7.0214553 NA
## Training set 4.1883021 0.98136400
## Test set 8.3987349 NA
## Training set 0.5100501 0.01877909
## Test set 9.3728901 NA
#For 3rd time series, by comparing RMSE, MAE, ME, MAPE,MASE, ETS model has the lowest value, and it will get better result due to higher accuracy
Forth time series model: a10
a10
## Jan Feb Mar Apr May Jun Jul
## 1991 3.526591
## 1992 5.088335 2.814520 2.985811 3.204780 3.127578 3.270523 3.737851
## 1993 6.192068 3.450857 3.772307 3.734303 3.905399 4.049687 4.315566
## 1994 6.731473 3.841278 4.394076 4.075341 4.540645 4.645615 4.752607
## 1995 6.749484 4.216067 4.949349 4.823045 5.194754 5.170787 5.256742
## 1996 8.329452 5.069796 5.262557 5.597126 6.110296 5.689161 6.486849
## 1997 8.524471 5.277918 5.714303 6.214529 6.411929 6.667716 7.050831
## 1998 8.798513 5.918261 6.534493 6.675736 7.064201 7.383381 7.813496
## 1999 10.391416 6.421535 8.062619 7.297739 7.936916 8.165323 8.717420
## 2000 12.511462 7.457199 8.591191 8.474000 9.386803 9.560399 10.834295
## 2001 14.497581 8.049275 10.312891 9.753358 10.850382 9.961719 11.443601
## 2002 16.300269 9.053485 10.002449 10.788750 12.106705 10.954101 12.844566
## 2003 16.828350 9.800215 10.816994 10.654223 12.512323 12.161210 12.998046
## 2004 18.003768 11.938030 12.997900 12.882645 13.943447 13.989472 15.339097
## 2005 20.778723 12.154552 13.402392 14.459239 14.795102 15.705248 15.829550
## 2006 23.486694 12.536987 15.467018 14.233539 17.783058 16.291602 16.980282
## 2007 28.038383 16.763869 19.792754 16.427305 21.000742 20.681002 21.834890
## 2008 29.665356 21.654285 18.264945 23.107677 22.912510 19.431740
## Aug Sep Oct Nov Dec
## 1991 3.180891 3.252221 3.611003 3.565869 4.306371
## 1992 3.558776 3.777202 3.924490 4.386531 5.810549
## 1993 4.562185 4.608662 4.667851 5.093841 7.179962
## 1994 5.350605 5.204455 5.301651 5.773742 6.204593
## 1995 5.855277 5.490729 6.115293 6.088473 7.416598
## 1996 6.300569 6.467476 6.828629 6.649078 8.606937
## 1997 6.704919 7.250988 7.819733 7.398101 10.096233
## 1998 7.431892 8.275117 8.260441 8.596156 10.558939
## 1999 9.070964 9.177113 9.251887 9.933136 11.532974
## 2000 10.643751 9.908162 11.710041 11.340151 12.079132
## 2001 11.659239 10.647060 12.652134 13.674466 12.965735
## 2002 12.196500 12.854748 13.542004 13.287640 15.134918
## 2003 12.517276 13.268658 14.733622 13.669382 16.503966
## 2004 15.370764 16.142005 16.685754 17.636728 18.869325
## 2005 17.554701 18.100864 17.496668 19.347265 20.031291
## 2006 18.612189 16.623343 21.430241 23.575517 23.334206
## 2007 23.930204 22.930357 23.263340 25.250030 25.806090
## 2008
str(a10)
## Time-Series [1:204] from 1992 to 2008: 3.53 3.18 3.25 3.61 3.57 ...
tsa10=ts(a10,frequency=12)#here, the data in month, so frequency=12
plot(a10)

#it has seaonality in the data
#seprate train and test(3years) data
traina10=ts(tsa10[1:174],frequency=12)
testa10=ts(tsa10[175:204], frequency = 12)
#build forecasting ets model
fit1a10=forecast(ets(traina10,model="ZZZ"),h=36)#here, forecast in 3 years
plot(fit1a10)

accu1a10=accuracy(fit1a10,testa10[1:12])
#build forecasting snaive model
fit2a10=snaive(traina10,h=36)
plot(fit2a10)

accu2a10=accuracy(fit2a10,testa10[1:12])
#build forecasting stlf model
fit3a10=stlf(traina10,lambda=TRUE, h=36)
plot(fit3a10)

accu3a10=accuracy(fit3a10,testa10[1:12])
#compare the three models accuracy
comparea10=(rbind(accu1a10,accu2a10,accu3a10))
comparea10
## ME RMSE MAE MPE MAPE
## Training set 0.04825856 0.5265220 0.3780555 0.25056715 4.045948
## Test set 0.21140712 1.5084250 1.1287669 0.09853637 6.139584
## Training set 0.97358705 1.1917569 0.9958977 10.74221748 11.026139
## Test set 1.72492342 2.4143765 2.0087936 8.39665450 10.142308
## Training set 0.07828765 0.5157047 0.3913199 0.51809164 4.408700
## Test set 0.02991077 1.9773416 1.7257221 -1.87746870 9.589619
## MASE ACF1
## Training set 0.3702065 -0.11563614
## Test set 1.1053321 NA
## Training set 0.9752215 0.36353154
## Test set 1.9670882 NA
## Training set 0.3831956 0.04375805
## Test set 1.6898937 NA
#4th time series, by comparing ME, RMSE, MAE, MPE, MAPE,MASE, model ets and stlf both has low errors, I think both models are good for this data set
Fifth time series: h02
h02
## Jan Feb Mar Apr May Jun Jul
## 1991 0.4297950
## 1992 0.6601190 0.3362200 0.3513480 0.3798080 0.3618010 0.4105340 0.4833887
## 1993 0.7515028 0.3875543 0.4272832 0.4138902 0.4288588 0.4701264 0.5092097
## 1994 0.8193253 0.4376698 0.5061213 0.4704912 0.5106963 0.5405138 0.5581189
## 1995 0.8031126 0.4752582 0.5525723 0.5271078 0.5612498 0.5889776 0.6231336
## 1996 0.9372759 0.5287616 0.5593399 0.5778717 0.6149274 0.5941888 0.7077584
## 1997 0.8468335 0.4638225 0.4852732 0.5280586 0.5623365 0.5885704 0.6694804
## 1998 0.8005444 0.4905572 0.5244080 0.5366495 0.5520905 0.6033656 0.6812454
## 1999 0.8930815 0.5126960 0.6529959 0.5739764 0.6392384 0.7038719 0.7706482
## 2000 0.9696557 0.5732915 0.6185068 0.6189957 0.6652092 0.7265201 0.8558649
## 2001 1.0438053 0.5106472 0.6725690 0.6484701 0.7041147 0.6994307 0.8519259
## 2002 1.1458676 0.5755844 0.6411646 0.6798621 0.7679384 0.7520959 0.9180636
## 2003 1.0781449 0.5782962 0.6433333 0.6633674 0.7505160 0.8007456 0.9163610
## 2004 1.1301252 0.6679887 0.7490143 0.7399860 0.7951286 0.8568028 1.0015932
## 2005 1.1706900 0.5976390 0.6525900 0.6705050 0.6952480 0.8422630 0.8743360
## 2006 1.2306910 0.5871350 0.7069590 0.6396410 0.8074050 0.7979700 0.8843120
## 2007 1.2233190 0.5977530 0.7043980 0.5617600 0.7452580 0.8379340 0.9541440
## 2008 1.2199410 0.7618220 0.6494350 0.8278870 0.8162550 0.7621370
## Aug Sep Oct Nov Dec
## 1991 0.4009060 0.4321590 0.4925430 0.5023690 0.6026520
## 1992 0.4754634 0.5347610 0.5686061 0.5952233 0.7712578
## 1993 0.5584430 0.6015141 0.6329471 0.6996054 0.9630805
## 1994 0.6728521 0.6858974 0.6896920 0.7413036 0.8133076
## 1995 0.7408372 0.7253718 0.8158030 0.8140095 0.9266531
## 1996 0.7195020 0.7443237 0.8048551 0.7885423 0.9710894
## 1997 0.6779937 0.7629955 0.7997237 0.7705219 0.9943893
## 1998 0.6780753 0.7948926 0.7846239 0.8130087 0.9777323
## 1999 0.8461859 0.8927289 0.8978999 0.9472807 1.0507073
## 2000 0.8659843 0.8252488 0.9554210 0.9385960 1.0130244
## 2001 0.9077052 0.8674445 1.0242928 1.1095902 1.0123132
## 2002 0.9243675 1.0131977 1.0269761 1.0067960 1.1027757
## 2003 0.9168868 1.0846589 1.1506482 1.0508382 1.2232345
## 2004 0.9948643 1.1344320 1.1810110 1.2160370 1.2572380
## 2005 1.0064970 1.0947360 1.0270430 1.1492320 1.1607120
## 2006 1.0496480 0.9957090 1.1682530 1.1080380 1.1200530
## 2007 1.0782195 1.1109816 1.1099791 1.1635343 1.1765890
## 2008
tsh02=ts(h02,frequency=12)#here, the data in month, so frequency=12
plot(tsh02)

str(tsh02)
## Time-Series [1:204] from 1 to 17.9: 0.43 0.401 0.432 0.493 0.502 ...
#it has seaonality in the data
#seprate train and test(3years) data
trainh02=ts(tsh02[1:174],frequency=12)
testh02=ts(tsh02[175:204], frequency = 12)
#build forecasting ets model
fit1h02=forecast(ets(trainh02,model="ZZZ"),h=36)#forecast for 3 years
plot(fit1h02)

accu1h02=accuracy(fit1h02,testh02[1:12])
#build forecasting snaive model
fit2h02=snaive(trainh02,h=36)
plot(fit2h02)

accu2h02=accuracy(fit2h02,testh02[1:12])
#build forecasting stlf model
fit3h02=stlf(trainh02,lambda=TRUE, h=36)
plot(fit3h02)

accu3h02=accuracy(fit3h02,testh02[1:12])
#compare the three models accuracy
compareh02=(rbind(accu1h02,accu2h02,accu3h02))
compareh02
## ME RMSE MAE MPE MAPE
## Training set -0.0027700094 0.04833063 0.03580177 -0.5059607 4.672153
## Test set -0.0242074479 0.07311548 0.06043370 -3.3770469 6.757805
## Training set 0.0344520802 0.07229492 0.06007068 4.7420132 8.153534
## Test set 0.0128602500 0.06905211 0.05728375 1.1937675 6.103460
## Training set -0.0003299416 0.04242058 0.03211635 -0.1738802 4.471225
## Test set -0.0324648849 0.07050482 0.06389786 -4.6709418 7.392647
## MASE ACF1
## Training set 0.3925138 0.08019171
## Test set 0.6625667 NA
## Training set 0.6585867 0.43987979
## Test set 0.6280321 NA
## Training set 0.3521085 -0.06369395
## Test set 0.7005460 NA
#Fifth time series, by comparing RMSE, MAE, ME, MAPE,MASE, snaive model has the lowest value, and it will get a higher accuracy
#For h02, the best model is snaive model
Sixth time series: usmelec
usmelec
## Jan Feb Mar Apr May Jun Jul Aug
## 1973 160.218 143.539 148.158 139.589 147.395 161.244 173.733 177.365
## 1974 157.555 142.748 150.342 142.312 153.813 156.440 178.247 174.119
## 1975 164.623 147.349 155.760 146.495 153.531 162.717 177.057 179.931
## 1976 178.609 156.966 164.467 153.467 157.664 173.674 186.691 186.639
## 1977 196.665 162.949 169.437 157.117 169.596 181.031 199.168 196.363
## 1978 198.108 173.746 173.461 160.013 175.549 188.585 202.947 206.659
## 1979 209.987 186.587 183.154 170.260 178.409 186.976 202.522 205.101
## 1980 200.296 188.961 187.745 169.017 176.066 189.748 217.058 215.629
## 1981 206.758 179.860 185.834 172.841 178.139 203.021 220.655 210.639
## 1982 209.694 180.546 187.968 172.877 177.480 186.447 210.865 205.892
## 1983 195.871 172.725 182.769 170.669 174.725 191.367 220.447 230.193
## 1984 216.924 189.810 200.387 181.381 192.550 209.967 221.526 229.532
## 1985 228.148 198.488 195.250 185.173 197.123 205.682 227.004 226.286
## 1986 217.761 192.582 197.115 186.370 197.647 215.334 242.954 225.402
## 1987 223.041 194.281 202.130 189.792 206.407 225.908 248.196 247.881
## 1988 238.188 217.183 214.294 196.297 208.704 233.066 257.742 267.929
## 1989 246.766 233.783 241.946 222.869 234.825 250.919 273.458 275.420
## 1990 255.187 229.499 244.761 229.770 241.774 268.992 287.448 290.655
## 1991 269.214 228.290 240.561 227.799 254.873 269.099 294.648 292.159
## 1992 267.773 239.514 247.733 233.406 242.412 261.077 293.617 281.927
## 1993 271.021 248.015 261.248 234.695 244.326 275.360 312.225 311.450
## 1994 289.768 249.172 257.998 240.637 252.745 294.162 311.257 307.605
## 1995 279.773 252.307 261.343 244.736 264.288 286.258 330.416 345.780
## 1996 296.923 270.685 275.019 251.613 282.266 302.717 327.708 329.286
## 1997 300.574 258.131 272.258 258.284 272.914 299.092 344.516 332.899
## 1998 295.260 260.590 286.878 261.230 299.640 328.903 361.936 357.366
## 1999 315.814 274.820 298.145 280.719 300.098 328.924 376.538 364.031
## 2000 327.994 294.169 301.580 285.578 322.954 339.054 356.528 368.669
## 2001 332.493 282.940 300.707 278.079 300.492 327.694 357.614 370.533
## 2002 319.941 281.826 302.549 289.848 307.675 341.023 381.542 374.586
## 2003 341.989 299.249 304.317 285.756 307.545 328.694 374.396 381.816
## 2004 346.546 314.280 308.812 290.560 327.380 345.085 377.332 368.439
## 2005 343.121 298.500 317.458 289.562 315.062 363.672 402.274 404.941
## 2006 328.658 307.333 318.730 297.858 330.616 364.260 410.421 407.763
## 2007 353.531 323.230 320.471 303.129 330.203 362.755 393.226 421.797
## 2008 362.998 325.106 324.630 305.865 325.245 373.109 402.900 388.987
## 2009 354.947 300.841 310.543 289.486 311.252 347.599 372.483 381.161
## 2010 360.401 319.004 311.601 287.279 328.208 376.100 409.972 408.761
## Sep Oct Nov Dec
## 1973 156.875 154.197 148.138 153.605
## 1974 152.467 152.196 150.071 160.009
## 1975 155.441 155.188 153.035 169.628
## 1976 165.237 164.009 169.346 184.144
## 1977 176.498 166.645 167.388 184.589
## 1978 185.802 176.013 176.389 192.105
## 1979 180.975 179.953 177.770 188.970
## 1980 191.698 178.761 178.771 195.849
## 1981 187.051 181.558 175.792 195.826
## 1982 180.875 173.172 173.599 184.958
## 1983 195.817 183.137 183.171 212.555
## 1984 195.411 191.142 190.602 200.232
## 1985 202.712 194.995 192.649 219.490
## 1986 206.905 197.960 196.654 213.787
## 1987 213.221 203.215 200.479 220.736
## 1988 220.392 210.814 209.815 232.988
## 1989 242.794 235.669 234.250 274.448
## 1990 258.357 244.372 231.242 255.770
## 1991 256.027 245.378 241.661 254.089
## 1992 259.925 244.994 244.162 267.343
## 1993 264.032 250.553 252.102 272.165
## 1994 266.262 256.528 251.868 269.520
## 1995 277.575 263.978 261.478 285.554
## 1996 283.151 271.446 269.122 284.250
## 1997 301.057 284.971 271.121 296.356
## 1998 318.924 284.446 267.136 297.986
## 1999 305.516 283.935 271.096 295.174
## 2000 312.447 289.452 284.670 319.011
## 2001 306.929 294.734 278.934 305.496
## 2002 331.279 307.059 296.290 324.834
## 2003 323.136 306.741 297.867 331.680
## 2004 335.622 312.450 302.101 341.948
## 2005 350.218 316.398 306.115 348.101
## 2006 332.055 321.567 309.159 336.283
## 2007 355.394 332.615 314.103 346.290
## 2008 338.056 318.547 310.046 343.898
## 2009 327.345 306.991 296.586 350.461
## 2010 345.064 307.054
tsusm=ts(usmelec,frequency=12)#here, the data in month, so frequency=12
plot(tsusm)

str(tsusm)
## Time-Series [1:454] from 1 to 38.8: 160 144 148 140 147 ...
#it has seaonality in the data
#seprate train and test(3years) data
trainusm=ts(tsusm[1:420],frequency=12)
testusm=ts(tsusm[421:454], frequency = 12)
#build forecasting ets model
fit1usm=forecast(ets(trainusm,model="ZZZ"),h=36)
plot(fit1usm)

accu1usm=accuracy(fit1,testusm[1:12])
#build forecasting snaive model
fit2usm=snaive(trainusm,h=36)
plot(fit2usm)

accu2usm=accuracy(fit2usm,testusm[1:12])
#build forecasting stlf model
fit3usm=stlf(trainusm,lambda=TRUE, h=36)
plot(fit3usm)

accu3usm=accuracy(fit3usm,testusm[1:12])
#compare the three models accuracy
compareusm=(rbind(accu1usm,accu2usm,accu3usm))
compareusm
## ME RMSE MAE MPE MAPE
## Training set -0.40717827 15.955893 12.161081 -0.07590221 2.908499
## Test set -76.67589778 88.855497 77.185052 -23.20267787 23.329050
## Training set 5.61933333 11.287238 8.755417 2.25331043 3.502457
## Test set -3.11308333 12.716432 9.490750 -0.91428053 2.670424
## Training set 0.00253638 6.185007 4.707792 -0.05492942 1.881865
## Test set -8.57414475 11.834681 10.123618 -2.54652521 2.982407
## MASE ACF1
## Training set 0.2114693 -0.1924442
## Test set 1.3421728 NA
## Training set 0.4845123 0.4341421
## Test set 0.5252046 NA
## Training set 0.2605225 0.1204840
## Test set 0.5602266 NA
#The sixth time series usmelec, snaive model has the lowest MAE, MAPE, MASE error value, so snaive model is the best