kings <- scan("http://robjhyndman.com/tsdldata/misc/kings.dat",skip=3)
kings
##  [1] 60 43 67 50 56 42 50 65 68 43 65 34 47 34 49 41 13 35 53 56 16 43 69
## [24] 59 48 59 86 55 68 51 33 49 67 77 81 67 71 81 68 70 77 56
plot(kings,type="l")

class(kings)
## [1] "numeric"
kingsts=ts(kings)
kingsts
## Time Series:
## Start = 1 
## End = 42 
## Frequency = 1 
##  [1] 60 43 67 50 56 42 50 65 68 43 65 34 47 34 49 41 13 35 53 56 16 43 69
## [24] 59 48 59 86 55 68 51 33 49 67 77 81 67 71 81 68 70 77 56
plot.ts(kingsts)
#install.packages("TTR")
library("TTR")

kingstsMA <- SMA(kingsts,n=8)
plot.ts(kingstsMA )

library(forecast)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: timeDate
## This is forecast 7.3
fit <- HoltWinters(kingsts, beta=FALSE, gamma=FALSE)
class(fit)
## [1] "HoltWinters"
forecast(fit, 3)
##    Point Forecast    Lo 80    Hi 80    Lo 95     Hi 95
## 43       67.79249 48.05162 87.53335 37.60144  97.98353
## 44       67.79249 47.35022 88.23475 36.52873  99.05624
## 45       67.79249 46.67209 88.91288 35.49163 100.09334
plot(forecast(fit, 3))

# double exponential - models level and trend
fit2 <- HoltWinters(kingsts, gamma=FALSE)


forecast(fit2, 3)
##    Point Forecast    Lo 80    Hi 80     Lo 95     Hi 95
## 43       63.12468 39.03467 87.21470 26.282183  99.96718
## 44       61.44472 32.15661 90.73283 16.652415 106.23702
## 45       59.76475 24.38591 95.14359  5.657488 113.87202
plot(forecast(fit2, 3))   

fit4<- ets(kingsts)

forecast(fit4, 3)
##    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 43       67.82931 48.61620 87.04241 38.44540 97.21322
## 44       67.82931 47.99645 87.66217 37.49756 98.16106
## 45       67.82931 47.39547 88.26314 36.57846 99.08016
plot(forecast(fit4, 3))

fit5<- auto.arima(kingsts)

forecast(fit5, 3)
##    Point Forecast    Lo 80    Hi 80    Lo 95     Hi 95
## 43       67.75063 48.05479 87.44646 37.62845  97.87281
## 44       67.75063 47.30662 88.19463 36.48422  99.01703
## 45       67.75063 46.58489 88.91637 35.38042 100.12084
plot(forecast(fit5, 3))

#Airpassengers

data("AirPassengers")

plot(AirPassengers)

class(AirPassengers)
## [1] "ts"
mod1=ets(AirPassengers)
fmod1=forecast(mod1,20)
fmod1
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 1961       441.7479 420.9284 462.5675 409.9071 473.5887
## Feb 1961       433.0931 407.5924 458.5938 394.0931 472.0931
## Mar 1961       496.6067 462.3205 530.8930 444.1705 549.0430
## Apr 1961       483.5263 445.6985 521.3541 425.6737 541.3790
## May 1961       485.1026 443.0128 527.1925 420.7317 549.4735
## Jun 1961       551.1085 498.8558 603.3612 471.1949 631.0221
## Jul 1961       613.3810 550.5136 676.2484 517.2336 709.5284
## Aug 1961       610.4359 543.3613 677.5105 507.8542 713.0177
## Sep 1961       530.9494 468.8133 593.0855 435.9204 625.9783
## Oct 1961       462.5032 405.1625 519.8439 374.8081 550.1982
## Nov 1961       402.0130 349.4447 454.5813 321.6167 482.4093
## Dec 1961       450.8391 388.8923 512.7858 356.0996 545.5785
## Jan 1962       458.2429 392.2920 524.1938 357.3798 559.1060
## Feb 1962       448.8867 381.4061 516.3673 345.6841 552.0894
## Mar 1962       514.2943 433.7353 594.8534 391.0899 637.4988
## Apr 1962       500.3479 418.8586 581.8372 375.7208 624.9751
## May 1962       501.5883 416.8132 586.3634 371.9359 631.2406
## Jun 1962       569.4049 469.7070 669.1029 416.9301 721.8798
## Jul 1962       633.2762 518.5862 747.9663 457.8729 808.6795
## Aug 1962       629.7813 511.9731 747.5894 449.6093 809.9533
plot(fmod1)

str(fmod1)
## List of 9
##  $ model    :List of 18
##   ..$ loglik    : num -680
##   ..$ aic       : num 1395
##   ..$ bic       : num 1449
##   ..$ aicc      : num 1401
##   ..$ mse       : num 114
##   ..$ amse      : num 192
##   ..$ fit       :List of 4
##   .. ..$ value  : num 1359
##   .. ..$ par    : num [1:17] 0.7322 0.0188 0.0001 0.98 120.9759 ...
##   .. ..$ fail   : int 1
##   .. ..$ fncount: int 2001
##   ..$ residuals : Time-Series [1:144] from 1949 to 1961: 0.00815 0.0656 -0.01866 -0.01149 -0.07697 ...
##   ..$ fitted    : Time-Series [1:144] from 1949 to 1961: 111 111 135 130 131 ...
##   ..$ states    : Time-Series [1:145, 1:14] from 1949 to 1961: 121 123 131 131 132 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : NULL
##   .. .. ..$ : chr [1:14] "l" "b" "s1" "s2" ...
##   ..$ par       : Named num [1:17] 0.7322 0.0188 0.0001 0.98 120.9759 ...
##   .. ..- attr(*, "names")= chr [1:17] "alpha" "beta" "gamma" "phi" ...
##   ..$ m         : num 12
##   ..$ method    : chr "ETS(M,Ad,M)"
##   ..$ components: chr [1:4] "M" "A" "M" "TRUE"
##   ..$ call      : language ets(y = AirPassengers)
##   ..$ initstate : Named num [1:14] 120.976 1.801 0.893 0.798 0.921 ...
##   .. ..- attr(*, "names")= chr [1:14] "l" "b" "s1" "s2" ...
##   ..$ sigma2    : num 0.00135
##   ..$ x         : Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...
##   ..- attr(*, "class")= chr "ets"
##  $ mean     : Time-Series [1:20] from 1961 to 1963: 442 433 497 484 485 ...
##  $ level    : num [1:2] 80 95
##  $ x        : Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...
##  $ upper    : Time-Series [1:20, 1:2] from 1961 to 1963: 463 459 531 521 527 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:2] "Series 1" "Series 2"
##  $ lower    : Time-Series [1:20, 1:2] from 1961 to 1963: 421 408 462 446 443 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:2] "Series 1" "Series 2"
##  $ fitted   : Time-Series [1:144] from 1949 to 1961: 111 111 135 130 131 ...
##  $ method   : chr "ETS(M,Ad,M)"
##  $ residuals: Time-Series [1:144] from 1949 to 1961: 0.00815 0.0656 -0.01866 -0.01149 -0.07697 ...
##  - attr(*, "class")= chr "forecast"
fmod1$model$aic
## [1] 1395.092
mod2=auto.arima(AirPassengers)
fmod2=forecast(mod2,20)
fmod2
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Jan 1961       446.7582 431.6858 461.8306 423.7070 469.8094
## Feb 1961       420.7582 402.5180 438.9984 392.8622 448.6542
## Mar 1961       448.7582 427.8241 469.6923 416.7423 480.7741
## Apr 1961       490.7582 467.4394 514.0770 455.0952 526.4212
## May 1961       501.7582 476.2770 527.2395 462.7880 540.7284
## Jun 1961       564.7582 537.2842 592.2323 522.7403 606.7761
## Jul 1961       651.7582 622.4264 681.0900 606.8991 696.6173
## Aug 1961       635.7582 604.6796 666.8368 588.2275 683.2889
## Sep 1961       537.7582 505.0258 570.4906 487.6983 587.8181
## Oct 1961       490.7582 456.4516 525.0648 438.2908 543.2256
## Nov 1961       419.7582 383.9466 455.5698 364.9891 474.5273
## Dec 1961       461.7582 424.5023 499.0141 404.7803 518.7361
## Jan 1962       476.5164 431.4567 521.5761 407.6036 545.4292
## Feb 1962       450.5164 400.9938 500.0390 374.7781 526.2547
## Mar 1962       478.5164 424.9010 532.1318 396.5188 560.5141
## Apr 1962       520.5164 463.0993 577.9335 432.7045 608.3283
## May 1962       531.5164 470.5341 592.4987 438.2520 624.7808
## Jun 1962       594.5164 530.1661 658.8667 496.1011 692.9317
## Jul 1962       681.5164 613.9659 749.0670 578.2068 784.8261
## Aug 1962       665.5164 594.9105 736.1223 557.5340 773.4988
plot(fmod2)

str(fmod2)
## List of 10
##  $ method   : chr "ARIMA(0,1,1)(0,1,0)[12]"
##  $ model    :List of 17
##   ..$ coef     : Named num -0.318
##   .. ..- attr(*, "names")= chr "ma1"
##   ..$ sigma2   : num 138
##   ..$ var.coef : num [1, 1] 0.00769
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr "ma1"
##   .. .. ..$ : chr "ma1"
##   ..$ mask     : logi TRUE
##   ..$ loglik   : num -508
##   ..$ aic      : num 1021
##   ..$ arma     : int [1:7] 0 1 0 0 12 1 1
##   ..$ residuals: Time-Series [1:144] from 1949 to 1961: 0.0647 0.0336 0.0338 0.0226 0.0108 ...
##   ..$ call     : language auto.arima(y = AirPassengers, x = structure(list(x = structure(c(112,  118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118, 115, 126, 141,  ...
##   ..$ series   : chr "AirPassengers"
##   ..$ code     : int 0
##   ..$ n.cond   : int 0
##   ..$ nobs     : int 131
##   ..$ model    :List of 10
##   .. ..$ phi  : num(0) 
##   .. ..$ theta: num -0.318
##   .. ..$ Delta: num [1:13] 1 0 0 0 0 0 0 0 0 0 ...
##   .. ..$ Z    : num [1:15] 1 0 1 0 0 0 0 0 0 0 ...
##   .. ..$ a    : num [1:15] -1 2.76 390 461 508 ...
##   .. ..$ P    : num [1:15, 1:15] 0.00 0.00 -4.68e-20 -4.81e-35 7.70e-34 ...
##   .. ..$ T    : num [1:15, 1:15] 0 0 1 0 0 0 0 0 0 0 ...
##   .. ..$ V    : num [1:15, 1:15] 1 -0.318 0 0 0 ...
##   .. ..$ h    : num 0
##   .. ..$ Pn   : num [1:15, 1:15] 1.00 -3.18e-01 3.49e-19 3.51e-19 4.20e-18 ...
##   ..$ bic      : num 1026
##   ..$ aicc     : num 1021
##   ..$ x        : Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...
##   ..- attr(*, "class")= chr [1:2] "ARIMA" "Arima"
##  $ level    : num [1:2] 80 95
##  $ mean     : Time-Series [1:20] from 1961 to 1963: 447 421 449 491 502 ...
##  $ lower    : num [1:20, 1:2] 432 403 428 467 476 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:2] "80%" "95%"
##  $ upper    : num [1:20, 1:2] 462 439 470 514 527 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:2] "80%" "95%"
##  $ x        : Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 135 148 148 136 119 ...
##  $ xname    : chr [1:12] "structure(c(112, 118, 132, 129, 121, 135, 148, 148, 136, 119, " "104, 118, 115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, " "140, 145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166, " "171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194, 196, " ...
##  $ fitted   : Time-Series [1:144] from 1949 to 1961: 112 118 132 129 121 ...
##  $ residuals: Time-Series [1:144] from 1949 to 1961: 0.0647 0.0336 0.0338 0.0226 0.0108 ...
##  - attr(*, "class")= chr "forecast"
fmod2$model$aic
## [1] 1020.639
AirPassengerscomponents <- decompose(AirPassengers)
plot(AirPassengerscomponents)

head(AirPassengerscomponents$seasonal,20)
##  [1] -24.748737 -36.188131  -2.241162  -8.036616  -4.506313  35.402778
##  [7]  63.830808  62.823232  16.520202 -20.642677 -53.593434 -28.619949
## [13] -24.748737 -36.188131  -2.241162  -8.036616  -4.506313  35.402778
## [19]  63.830808  62.823232
AirPassengersseasonallyadjusted <- AirPassengers - AirPassengerscomponents$seasonal

plot(AirPassengersseasonallyadjusted)