# Chapter 5 TS Analysis And Forecasting

rm(list = ls())
setwd("C:/Users/Tejendra/Desktop/FoldersOnDesktop/UdemyCourse/Section5")
require(tidyverse)
require(tidymodels)
require(data.table)
require(tidyposterior)
require(tsibble)  #tsibble for time series based on tidy principles
require(fable)  #for forecasting based on tidy principles
require(ggfortify)  #for plotting timeseries
require(forecast)  #for forecast function
require(tseries)
require(chron)
require(lubridate)
require(directlabels)
require(zoo)
require(lmtest)
setwd("C:/Users/Tejendra/Desktop/FoldersOnDesktop/UdemyCourse/Section5")
par(mar = rep(2, 4))
### Decomposing Time Series (U)
plot(nottem)

theme_set(theme_bw())
autoplot(nottem) + xlab("") + ylab("") + ggtitle("Time Series Plot of the nottem' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))  #for centering the text

frequency(nottem)  #whether the data is monthly, quarterly or anything else
## [1] 12
length(nottem)  #number of observations in the data
## [1] 240
decompose(nottem, type = "additive")  #decompose the time series in seasonal, trend and random components
## $x ## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ## 1920 40.6 40.8 44.4 46.7 54.1 58.5 57.7 56.4 54.3 50.5 42.9 39.8 ## 1921 44.2 39.8 45.1 47.0 54.1 58.7 66.3 59.9 57.0 54.2 39.7 42.8 ## 1922 37.5 38.7 39.5 42.1 55.7 57.8 56.8 54.3 54.3 47.1 41.8 41.7 ## 1923 41.8 40.1 42.9 45.8 49.2 52.7 64.2 59.6 54.4 49.2 36.3 37.6 ## 1924 39.3 37.5 38.3 45.5 53.2 57.7 60.8 58.2 56.4 49.8 44.4 43.6 ## 1925 40.0 40.5 40.8 45.1 53.8 59.4 63.5 61.0 53.0 50.0 38.1 36.3 ## 1926 39.2 43.4 43.4 48.9 50.6 56.8 62.5 62.0 57.5 46.7 41.6 39.8 ## 1927 39.4 38.5 45.3 47.1 51.7 55.0 60.4 60.5 54.7 50.3 42.3 35.2 ## 1928 40.8 41.1 42.8 47.3 50.9 56.4 62.2 60.5 55.4 50.2 43.0 37.3 ## 1929 34.8 31.3 41.0 43.9 53.1 56.9 62.5 60.3 59.8 49.2 42.9 41.9 ## 1930 41.6 37.1 41.2 46.9 51.2 60.4 60.1 61.6 57.0 50.9 43.0 38.8 ## 1931 37.1 38.4 38.4 46.5 53.5 58.4 60.6 58.2 53.8 46.6 45.5 40.6 ## 1932 42.4 38.4 40.3 44.6 50.9 57.0 62.1 63.5 56.3 47.3 43.6 41.8 ## 1933 36.2 39.3 44.5 48.7 54.2 60.8 65.5 64.9 60.1 50.2 42.1 35.8 ## 1934 39.4 38.2 40.4 46.9 53.4 59.6 66.5 60.4 59.2 51.2 42.8 45.8 ## 1935 40.0 42.6 43.5 47.1 50.0 60.5 64.6 64.0 56.8 48.6 44.2 36.4 ## 1936 37.3 35.0 44.0 43.9 52.7 58.6 60.0 61.1 58.1 49.6 41.6 41.3 ## 1937 40.8 41.0 38.4 47.4 54.1 58.6 61.4 61.8 56.3 50.9 41.4 37.1 ## 1938 42.1 41.2 47.3 46.6 52.4 59.0 59.6 60.4 57.0 50.7 47.8 39.2 ## 1939 39.4 40.9 42.4 47.8 52.4 58.0 60.7 61.8 58.2 46.7 46.6 37.8 ## ##$seasonal
##             Jan        Feb        Mar        Apr        May        Jun
## 1920 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1921 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1922 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1923 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1924 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1925 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1926 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1927 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1928 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1929 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1930 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1931 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1932 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1933 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1934 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1935 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1936 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1937 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1938 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
## 1939 -9.3393640 -9.8998904 -6.9466009 -2.7573465  3.4533991  8.9865132
##             Jul        Aug        Sep        Oct        Nov        Dec
## 1920 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1921 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1922 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1923 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1924 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1925 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1926 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1927 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1928 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1929 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1930 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1931 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1932 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1933 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1934 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1935 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1936 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1937 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1938 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
## 1939 12.9672149 11.4591009  7.4001096  0.6547149 -6.6176535 -9.3601974
##
## $trend ## Jan Feb Mar Apr May Jun Jul ## 1920 NA NA NA NA NA NA 49.04167 ## 1921 49.56667 50.07083 50.32917 50.59583 50.61667 50.60833 50.45417 ## 1922 48.87083 48.24167 47.89583 47.48750 47.27917 47.32083 47.45417 ## 1923 47.68333 48.21250 48.43750 48.52917 48.38750 47.98750 47.71250 ## 1924 47.59167 47.39167 47.41667 47.52500 47.88750 48.47500 48.75417 ## 1925 49.51250 49.74167 49.71667 49.58333 49.32917 48.76250 48.42500 ## 1926 48.64167 48.64167 48.87083 48.92083 48.92917 49.22083 49.37500 ## 1927 48.83750 48.68750 48.50833 48.54167 48.72083 48.55833 48.42500 ## 1928 48.63333 48.70833 48.73750 48.76250 48.78750 48.90417 48.74167 ## 1929 47.47917 47.48333 47.65833 47.80000 47.75417 47.94167 48.41667 ## 1930 49.48333 49.43750 49.37500 49.32917 49.40417 49.27917 48.96250 ## 1931 48.66250 48.54167 48.26667 47.95417 47.87917 48.05833 48.35417 ## 1932 48.30417 48.58750 48.91250 49.04583 48.99583 48.96667 48.75833 ## 1933 50.00000 50.20000 50.41667 50.69583 50.75417 50.44167 50.32500 ## 1934 49.75000 49.60417 49.37917 49.38333 49.45417 49.90000 50.34167 ## 1935 50.72083 50.79167 50.84167 50.63333 50.58333 50.25000 49.74583 ## 1936 48.65000 48.33750 48.27083 48.36667 48.30000 48.39583 48.74583 ## 1937 49.39167 49.47917 49.43333 49.41250 49.45833 49.27500 49.15417 ## 1938 49.71667 49.58333 49.55417 49.57500 49.83333 50.18750 50.16250 ## 1939 49.67917 49.78333 49.89167 49.77500 49.55833 49.45000 NA ## Aug Sep Oct Nov Dec ## 1920 49.15000 49.13750 49.17917 49.19167 49.20000 ## 1921 50.12917 49.85000 49.41250 49.27500 49.30417 ## 1922 47.69167 47.89167 48.18750 48.07083 47.58750 ## 1923 47.50000 47.20000 46.99583 47.15000 47.52500 ## 1924 48.90833 49.13750 49.22500 49.23333 49.32917 ## 1925 48.51250 48.74167 49.00833 49.03333 48.79167 ## 1926 49.17917 49.05417 49.05833 49.02917 49.00000 ## 1927 48.59167 48.59583 48.50000 48.47500 48.50000 ## 1928 48.08333 47.60000 47.38333 47.33333 47.44583 ## 1929 48.94167 49.19167 49.32500 49.37083 49.43750 ## 1930 48.82917 48.76667 48.63333 48.71250 48.72500 ## 1931 48.57500 48.65417 48.65417 48.46667 48.30000 ## 1932 48.53750 48.75000 49.09583 49.40417 49.70000 ## 1933 50.41250 50.19583 49.95000 49.84167 49.75833 ## 1934 50.55000 50.86250 51.00000 50.86667 50.76250 ## 1935 49.31667 49.02083 48.90833 48.88750 48.92083 ## 1936 49.14167 49.15833 49.07083 49.27500 49.33333 ## 1937 49.21667 49.59583 49.93333 49.82917 49.77500 ## 1938 50.03750 49.82083 49.66667 49.71667 49.67500 ## 1939 NA NA NA NA NA ## ##$random
##               Jan          Feb          Mar          Apr          May
## 1920           NA           NA           NA           NA           NA
## 1921  3.972697368 -0.370942982  1.717434211 -0.838486842  0.029934211
## 1922 -2.031469298  0.358223684 -1.449232456 -2.630153509  4.967434211
## 1923  3.456030702  1.787390351  1.409100877  0.028179825 -2.640899123
## 1924  1.047697368  0.008223684 -2.170065789  0.732346491  1.859100877
## 1925 -0.173135965  0.658223684 -1.970065789 -1.725986842  1.017434211
## 1926 -0.102302632  4.658223684  1.475767544  2.736513158 -1.782565789
## 1927 -0.098135965 -0.287609649  3.738267544  1.315679825 -0.474232456
## 1928  1.506030702  2.291557018  1.009100877  1.294846491 -1.340899123
## 1929 -3.339802632 -6.283442982  0.288267544 -1.142653509  1.892434211
## 1930  1.456030702 -2.437609649 -1.228399123  0.328179825 -1.657565789
## 1931 -2.223135965 -0.241776316 -2.920065789  1.303179825  2.167434211
## 1932  3.435197368 -0.287609649 -1.665899123 -1.688486842 -1.549232456
## 1933 -4.460635965 -1.000109649  1.029934211  0.761513158 -0.007565789
## 1934 -1.010635965 -1.504276316 -2.032565789  0.274013158  0.492434211
## 1935 -1.381469298  1.708223684 -0.395065789 -0.775986842 -4.036732456
## 1936 -2.010635965 -3.437609649  2.675767544 -1.709320175  0.946600877
## 1937  0.747697368  1.420723684 -4.086732456  0.744846491  1.188267544
## 1938  1.722697368  1.516557018  4.692434211 -0.217653509 -0.886732456
## 1939 -0.939802632  1.016557018 -0.545065789  0.782346491 -0.611732456
##               Jun          Jul          Aug          Sep          Oct
## 1920           NA -4.308881579 -4.209100877 -2.237609649  0.666118421
## 1921 -0.894846491  2.878618421 -1.688267544 -0.250109649  4.132785088
## 1922  1.492653509 -3.621381579 -4.850767544 -0.991776316 -1.742214912
## 1923 -4.274013158  3.520285088  0.640899123 -0.200109649  1.549451754
## 1924  0.238486842 -0.921381579 -2.167434211 -0.137609649 -0.079714912
## 1925  1.650986842  2.107785088  1.028399123 -3.141776316  0.336951754
## 1926 -1.407346491  0.157785088  1.361732456  1.045723684 -3.013048246
## 1927 -2.544846491 -0.992214912  0.449232456 -1.295942982  1.145285088
## 1928 -1.490679825  0.491118421  0.957565789  0.399890351  2.161951754
## 1929 -0.028179825  1.116118421 -0.100767544  3.208223684 -0.779714912
## 1930  2.134320175 -1.829714912  1.311732456  0.833223684  1.611951754
## 1931  1.355153509 -0.721381579 -1.834100877 -2.254276316 -2.708881579
## 1932 -0.953179825  0.374451754  3.503399123  0.149890351 -2.450548246
## 1933  1.371820175  2.207785088  3.028399123  2.504057018 -0.404714912
## 1934  0.713486842  3.191118421 -1.609100877  0.937390351 -0.454714912
## 1935  1.263486842  1.886951754  3.224232456  0.379057018 -0.963048246
## 1936  1.217653509 -1.713048246  0.499232456  1.541557018 -0.125548246
## 1937  0.338486842 -0.721381579  1.124232456 -0.695942982  0.311951754
## 1938 -0.174013158 -3.529714912 -1.096600877 -0.220942982  0.378618421
## 1939 -0.436513158           NA           NA           NA           NA
##               Nov          Dec
## 1920  0.325986842 -0.039802632
## 1921 -2.957346491  2.856030702
## 1922  0.346820175  3.472697368
## 1923 -4.232346491 -0.564802632
## 1924  1.784320175  3.631030702
## 1925 -4.315679825 -3.131469298
## 1926 -0.811513158  0.160197368
## 1927  0.442653509 -3.939802632
## 1928  2.284320175 -0.785635965
## 1929  0.146820175  1.822697368
## 1930  0.905153509 -0.564802632
## 1931  3.650986842  1.660197368
## 1932  0.813486842  1.460197368
## 1933 -1.124013158 -4.598135965
## 1934 -1.449013158  4.397697368
## 1935  1.930153509 -3.160635965
## 1936 -1.057346491  1.326864035
## 1937 -1.811513158 -3.314802632
## 1938  4.700986842 -1.114802632
## 1939           NA           NA
##
## $figure ## [1] -9.3393640 -9.8998904 -6.9466009 -2.7573465 3.4533991 8.9865132 ## [7] 12.9672149 11.4591009 7.4001096 0.6547149 -6.6176535 -9.3601974 ## ##$type
##
## attr(,"class")
## [1] "decomposed.ts"
plot(decompose(nottem, type = "additive"))

autoplot(decompose(nottem, type = "additive")) + xlab("") + ylab("") +
ggtitle("Decomposition plot of the nottem' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))

# alternatively the function stl could be used
plot(stl(nottem, s.window = "periodic"))

autoplot(stl(nottem, s.window = "periodic")) + xlab("") + ylab("") +
ggtitle("Decomposed plot of nottem' Time-Series Data") +
theme(plot.title = element_text(hjust = 0.5))

stl(nottem, s.window = "periodic")
##  Call:
##  stl(x = nottem, s.window = "periodic")
##
## Components
##            seasonal    trend    remainder
## Jan 1920 -9.3471980 49.68067  0.266525379
## Feb 1920 -9.8552496 49.54552  1.109728805
## Mar 1920 -6.8533008 49.41037  1.842931803
## Apr 1920 -2.7634710 49.32862  0.134848770
## May 1920  3.5013569 49.24688  1.351767558
## Jun 1920  8.9833032 49.21027  0.306425938
## Jul 1920 12.8452501 49.17367 -4.318916345
## Aug 1920 11.4763813 49.13389 -4.210271506
## Sep 1920  7.4475114 49.09411 -2.241625601
## Oct 1920  0.4736899 49.15198  0.874331161
## Nov 1920 -6.4301309 49.20984  0.120287167
## Dec 1920 -9.4781423 49.49282 -0.214674402
## Jan 1921 -9.3471980 49.77579  3.771408356
## Feb 1921 -9.8552496 50.08132 -0.426073148
## Mar 1921 -6.8533008 50.38686  1.566444922
## Apr 1921 -2.7634710 50.49363 -0.730156884
## May 1921  3.5013569 50.60040 -0.001756869
## Jun 1921  8.9833032 50.50241 -0.785710552
## Jul 1921 12.8452501 50.40441  3.050335100
## Aug 1921 11.4763813 50.15065 -1.727027471
## Sep 1921  7.4475114 49.89688 -0.344388977
## Oct 1921  0.4736899 49.62951  4.096796830
## Nov 1921 -6.4301309 49.36215 -3.232018119
## Dec 1921 -9.4781423 49.04873  3.229417050
## Jan 1922 -9.3471980 48.73530 -1.888103454
## Feb 1922 -9.8552496 48.33172  0.223530715
## Mar 1922 -6.8533008 47.92814 -1.574835542
## Apr 1922 -2.7634710 47.62971 -2.766243150
## May 1922  3.5013569 47.33129  4.867351062
## Jun 1922  8.9833032 47.33932  1.477377256
## Jul 1922 12.8452501 47.34735 -3.392597215
## Aug 1922 11.4763813 47.54030 -4.716683254
## Sep 1922  7.4475114 47.73326 -0.880768228
## Oct 1922  0.4736899 47.85392 -1.227614475
## Nov 1922 -6.4301309 47.97459  0.255538521
## Dec 1922 -9.4781423 48.05637  3.121775401
## Jan 1923 -9.3471980 48.13814  3.009056606
## Feb 1923 -9.8552496 48.28185  1.673400681
## Mar 1923 -6.8533008 48.42556  1.327744328
## Apr 1923 -2.7634710 48.33263  0.230840836
## May 1923  3.5013569 48.23970 -2.541060836
## Jun 1923  8.9833032 47.96007 -4.243372853
## Jul 1923 12.8452501 47.68044  3.674314465
## Aug 1923 11.4763813 47.45963  0.663988147
## Sep 1923  7.4475114 47.23883 -0.286337106
## Oct 1923  0.4736899 47.22609  1.500223556
## Nov 1923 -6.4301309 47.21335 -4.483216539
## Dec 1923 -9.4781423 47.28901 -0.210865451
## Jan 1924 -9.3471980 47.36467  1.282529963
## Feb 1924 -9.8552496 47.43546 -0.080209799
## Mar 1924 -6.8533008 47.50625 -2.352949988
## Apr 1924 -2.7634710 47.73513  0.528338204
## May 1924  3.5013569 47.96401  1.734628216
## Jun 1924  8.9833032 48.30128  0.415415073
## Jul 1924 12.8452501 48.63855 -0.683798735
## Aug 1924 11.4763813 48.87239 -2.148774448
## Sep 1924  7.4475114 49.10624 -0.153749096
## Oct 1924  0.4736899 49.23554  0.090766554
## Nov 1924 -6.4301309 49.36485  1.465281448
## Dec 1924 -9.4781423 49.47714  3.601004136
## Jan 1925 -9.3471980 49.58943 -0.242228849
## Feb 1925 -9.8552496 49.62480  0.730453659
## Mar 1925 -6.8533008 49.66017 -2.006864260
## Apr 1925 -2.7634710 49.44799 -1.584518944
## May 1925  3.5013569 49.23581  1.062828193
## Jun 1925  8.9833032 48.96275  1.453949252
## Jul 1925 12.8452501 48.68968  1.965069647
## Aug 1925 11.4763813 48.66249  0.861133228
## Sep 1925  7.4475114 48.63529 -3.082802125
## Oct 1925  0.4736899 48.68778  0.838525570
## Nov 1925 -6.4301309 48.74028 -4.210147490
## Dec 1925 -9.4781423 48.73479 -2.956649644
## Jan 1926 -9.3471980 48.72931 -0.182107471
## Feb 1926 -9.8552496 48.81914  4.436114170
## Mar 1926 -6.8533008 48.90897  1.344335385
## Apr 1926 -2.7634710 49.03851  2.624958422
## May 1926  3.5013569 49.16806 -2.069416721
## Jun 1926  8.9833032 49.18067 -1.363972554
## Jul 1926 12.8452501 49.19328  0.461470949
## Aug 1926 11.4763813 49.11919  1.404424201
## Sep 1926  7.4475114 49.04511  1.007378518
## Oct 1926  0.4736899 49.01380 -2.787487357
## Nov 1926 -6.4301309 48.98248 -0.952353990
## Dec 1926 -9.4781423 48.92428  0.353859697
## Jan 1927 -9.3471980 48.86608 -0.118882290
## Feb 1927 -9.8552496 48.77246 -0.417208438
## Mar 1927 -6.8533008 48.67884  3.474464987
## Apr 1927 -2.7634710 48.64104  1.222435260
## May 1927  3.5013569 48.60324 -0.404592645
## Jun 1927  8.9833032 48.54473 -2.528029817
## Jul 1927 12.8452501 48.48622 -0.931467654
## Aug 1927 11.4763813 48.46091  0.562707318
## Sep 1927  7.4475114 48.43561 -1.183116646
## Oct 1927  0.4736899 48.47250  1.353805163
## Nov 1927 -6.4301309 48.50940  0.220726214
## Dec 1927 -9.4781423 48.58914 -3.911002529
## Jan 1928 -9.3471980 48.66888  1.478313054
## Feb 1928 -9.8552496 48.71949  2.235758285
## Mar 1928 -6.8533008 48.77010  0.883203089
## Apr 1928 -2.7634710 48.80480  1.258669662
## May 1928  3.5013569 48.83951 -1.440861943
## Jun 1928  8.9833032 48.69762 -1.280921267
## Jul 1928 12.8452501 48.55573  0.799018744
## Aug 1928 11.4763813 48.20763  0.815985704
## Sep 1928  7.4475114 47.85953  0.092953729
## Oct 1928  0.4736899 47.61843  2.107877490
## Nov 1928 -6.4301309 47.37733  2.052800495
## Dec 1928 -9.4781423 47.33335 -0.555212465
## Jan 1929 -9.3471980 47.28938 -3.142181098
## Feb 1929 -9.8552496 47.35578 -6.200531177
## Mar 1929 -6.8533008 47.42218  0.431118317
## Apr 1929 -2.7634710 47.62278 -0.959311653
## May 1929  3.5013569 47.82338  1.775260197
## Jun 1929  8.9833032 48.19582 -0.279118205
## Jul 1929 12.8452501 48.56825  1.086502728
## Aug 1929 11.4763813 48.90395 -0.080335161
## Sep 1929  7.4475114 49.23966  3.112828016
## Oct 1929  0.4736899 49.35027 -0.623963046
## Nov 1929 -6.4301309 49.46089 -0.130754863
## Dec 1929 -9.4781423 49.44736  1.930783783
## Jan 1930 -9.3471980 49.43383  1.513366755
## Feb 1930 -9.8552496 49.38822 -2.432972451
## Mar 1930 -6.8533008 49.34261 -1.289312084
## Apr 1930 -2.7634710 49.29319  0.370279597
## May 1930  3.5013569 49.24377 -1.545126902
## Jun 1930  8.9833032 49.16499  2.251710214
## Jul 1930 12.8452501 49.08620 -1.831453334
## Aug 1930 11.4763813 48.97296  1.150658078
## Sep 1930  7.4475114 48.85972  0.692770554
## Oct 1930  0.4736899 48.78043  1.645879449
## Nov 1930 -6.4301309 48.70114  0.728987588
## Dec 1930 -9.4781423 48.62085 -0.342708316
## Jan 1931 -9.3471980 48.54056 -2.093359893
## Feb 1931 -9.8552496 48.38319 -0.127942861
## Mar 1931 -6.8533008 48.22583 -2.972526256
## Apr 1931 -2.7634710 48.12257  1.140904226
## May 1931  3.5013569 48.01931  1.979336529
## Jun 1931  8.9833032 48.14283  1.273868208
## Jul 1931 12.8452501 48.26635 -0.511600778
## Aug 1931 11.4763813 48.40974 -1.686116919
## Sep 1931  7.4475114 48.55312 -2.200631996
## Oct 1931  0.4736899 48.54088 -2.414570882
## Nov 1931 -6.4301309 48.52864  3.401489476
## Dec 1931 -9.4781423 48.53430  1.543840385
## Jan 1932 -9.3471980 48.53996  3.207235620
## Feb 1932 -9.8552496 48.66617 -0.410916656
## Mar 1932 -6.8533008 48.79237 -1.639069359
## Apr 1932 -2.7634710 48.83691 -1.473441358
## May 1932  3.5013569 48.88145 -1.482811536
## Jun 1932  8.9833032 48.82173 -0.805030457
## Jul 1932 12.8452501 48.76200  0.492749958
## Aug 1932 11.4763813 48.83146  3.192154743
## Sep 1932  7.4475114 48.90093 -0.048439406
## Oct 1932  0.4736899 49.12176 -2.295447972
## Nov 1932 -6.4301309 49.34259  0.687542706
## Dec 1932 -9.4781423 49.59996  1.678183472
## Jan 1933 -9.3471980 49.85733 -4.310131435
## Feb 1933 -9.8552496 50.12104 -0.965788581
## Mar 1933 -6.8533008 50.38475  0.968553847
## Apr 1933 -2.7634710 50.55788  0.905594559
## May 1933  3.5013569 50.73101 -0.032362909
## Jun 1933  8.9833032 50.69026  1.126437333
## Jul 1933 12.8452501 50.64951  2.005236911
## Aug 1933 11.4763813 50.46236  2.961261164
## Sep 1933  7.4475114 50.27520  2.377286481
## Oct 1933  0.4736899 50.02482 -0.298512893
## Nov 1933 -6.4301309 49.77444 -1.244313023
## Dec 1933 -9.4781423 49.60356 -4.325422241
## Jan 1934 -9.3471980 49.43269 -0.685487132
## Feb 1934 -9.8552496 49.38494 -1.329690994
## Mar 1934 -6.8533008 49.33720 -2.083895283
## Apr 1934 -2.7634710 49.49963  0.163845195
## May 1934  3.5013569 49.66206  0.236587495
## Jun 1934  8.9833032 49.99133  0.625370019
## Jul 1934 12.8452501 50.32060  3.334151879
## Aug 1934 11.4763813 50.58387 -1.660249972
## Sep 1934  7.4475114 50.84714  0.905349242
## Oct 1934  0.4736899 50.88993 -0.163620448
## Nov 1934 -6.4301309 50.93272 -1.702590896
## Dec 1934 -9.4781423 50.87092  4.407220183
## Jan 1935 -9.3471980 50.80912 -1.461924412
## Feb 1935 -9.8552496 50.75825  1.696998728
## Mar 1935 -6.8533008 50.70738 -0.354078560
## Apr 1935 -2.7634710 50.57994 -0.716471023
## May 1935  3.5013569 50.45250 -3.953861666
## Jun 1935  8.9833032 50.14917  1.367526515
## Jul 1935 12.8452501 49.84584  1.908914032
## Aug 1935 11.4763813 49.54007  2.983549209
## Sep 1935  7.4475114 49.23430  0.118185451
## Oct 1935  0.4736899 49.02909 -0.902784869
## Nov 1935 -6.4301309 48.82389  1.806244053
## Dec 1935 -9.4781423 48.64539 -2.767248190
## Jan 1936 -9.3471980 48.46689 -1.819696106
## Feb 1936 -9.8552496 48.35225 -3.496996331
## Mar 1936 -6.8533008 48.23760  2.615703017
## Apr 1936 -2.7634710 48.30761 -1.644138274
## May 1936  3.5013569 48.37762  0.821022256
## Jun 1936  8.9833032 48.58645  1.030250876
## Jul 1936 12.8452501 48.79527 -1.640521169
## Aug 1936 11.4763813 48.96677  0.656852958
## Sep 1936  7.4475114 49.13826  1.514228150
## Oct 1936  0.4736899 49.21434 -0.088032903
## Nov 1936 -6.4301309 49.29043 -1.260294714
## Dec 1936 -9.4781423 49.33815  1.439989280
## Jan 1937 -9.3471980 49.38588  0.761317600
## Feb 1937 -9.8552496 49.41158  1.443666982
## Mar 1937 -6.8533008 49.43728 -4.183984063
## Apr 1937 -2.7634710 49.39905  0.764425401
## May 1937  3.5013569 49.36081  1.237836686
## Jun 1937  8.9833032 49.32833  0.288370135
## Jul 1937 12.8452501 49.29585 -0.741097080
## Aug 1937 11.4763813 49.39591  0.927705554
## Sep 1937  7.4475114 49.49598 -0.643490748
## Oct 1937  0.4736899 49.62722  0.799089443
## Nov 1937 -6.4301309 49.75846 -1.928331124
## Dec 1937 -9.4781423 49.75732 -3.179182522
## Jan 1938 -9.3471980 49.75619  1.691010406
## Feb 1938 -9.8552496 49.73327  1.321982868
## Mar 1938 -6.8533008 49.71035  4.442954903
## Apr 1938 -2.7634710 49.78815 -0.424682121
## May 1938  3.5013569 49.86596 -0.967317324
## Jun 1938  8.9833032 49.91577  0.100921835
## Jul 1938 12.8452501 49.96559 -3.210839669
## Aug 1938 11.4763813 49.89688 -0.973257797
## Sep 1938  7.4475114 49.82816 -0.275674861
## Oct 1938  0.4736899 49.79656  0.429748533
## Nov 1938 -6.4301309 49.76496  4.465171170
## Dec 1938 -9.4781423 49.79001 -1.111866934
## Jan 1939 -9.3471980 49.81506 -1.067860710
## Feb 1939 -9.8552496 49.78783  0.967421826
## Mar 1939 -6.8533008 49.76060 -0.507296065
## Apr 1939 -2.7634710 49.68448  0.878994770
## May 1939  3.5013569 49.60836 -0.709712574
## Jun 1939  8.9833032 49.51780 -0.501104910
## Jul 1939 12.8452501 49.42725 -1.572497911
## Aug 1939 11.4763813 49.33655  0.987068518
## Sep 1939  7.4475114 49.24585  1.506636011
## Oct 1939  0.4736899 49.16331 -2.936997128
## Nov 1939 -6.4301309 49.08076  3.949368976
## Dec 1939 -9.4781423 49.00510 -1.726954804
# seasonal adjustment
class(mynottem)
## [1] "decomposed.ts"
# we are subtracting the seasonal element
nottemadjusted = nottem - mynottem$seasonal # getting a plot plot(nottemadjusted) autoplot(nottemadjusted) + xlab("") + ylab("") + ggtitle("Seasonally adjusted nottem' Time-Series") + theme(plot.title = element_text(hjust = 0.5)) plot(mynottem$seasonal)

autoplot(mynottem$seasonal) + xlab("") + ylab("") + ggtitle("Sesonal component of decomposed nottem' Time-Series") + theme(plot.title = element_text(hjust = 0.5)) # a stl forecast from the package forecast plot(stlf(nottem, method = "arima")) autoplot(stlf(nottem, method = "arima")) + xlab("") + ylab("") + ggtitle("Forecast of the nottem' Time-Series using stlf' function") + theme(plot.title = element_text(hjust = 0.5)) ## Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)]: non-numeric argument to binary operator ### Exercise Decomposition 1. Look for the problems in the Time ### Series data autoplot(AirPassengers) + xlab("") + ylab("") + ggtitle("Plot of the AirPassengers' Time-Series") + theme(plot.title = element_text(hjust = 0.5)) #Evidence of trend and seasonality in the Time Series # 2. two models for decomposition additive and multiplicative mymodel1 <- decompose(AirPassengers, type = "additive") mymodel2 <- decompose(AirPassengers, type = "multiplicative") # 3. Plot and compare the two decomposition models autoplot(mymodel1) + xlab("") + ylab("") + ggtitle("Decomposed AirPassengers' Time-Series (Additive Method)") + theme(plot.title = element_text(hjust = 0.5)) autoplot(mymodel1) + xlab("") + ylab("") + ggtitle("Decomposed AirPassengers' Time-Series (Multiplicative Method)") + theme(plot.title = element_text(hjust = 0.5)) # Both the decompsition show evidence of the presence of # trend and seasonality # 4. plot seasonally adjusted time series for mymodel1 autoplot(AirPassengers - (mymodel1$seasonal)) + xlab("") + ylab("") +
ggtitle("Seasonally Adjusted AirPassengers' Time-Series (Additive Method)") +
theme(plot.title = element_text(hjust = 0.5))

autoplot(mymodel1$trend + mymodel1$random) + xlab("") + ylab("") +
theme(plot.title = element_text(hjust = 0.5))

### SMOOTHING SMA in order to identify trends, we can use
### smoothers like a simple moving avg n identfies the order or
### the SMA - you can experiment with this parameter

x = c(1, 2, 3, 4, 5, 6, 7)
SMA(x, n = 3)  # SMA fro TTR package, 3rd order
## [1] NA NA  2  3  4  5  6
lynxsmoothed = SMA(lynx, n = 9)
lynxsmoothed
## Time Series:
## Start = 1821
## End = 1934
## Frequency = 1
##   [1]        NA        NA        NA        NA        NA        NA        NA
##   [8]        NA 2351.4444 2607.8889 2630.3333 2576.2222 2499.8889 2367.0000
##  [15] 2099.0000 1916.4444 1554.4444 1383.2222 1299.5556 1286.8889 1292.7778
##  [22] 1277.3333 1253.8889 1232.1111 1038.8889  855.3333  713.1111  792.2222
##  [29]  853.1111  876.4444  913.3333  930.7778  947.1111  967.6667 1034.8889
##  [36] 1101.1111 1138.3333 1267.4444 1303.3333 1294.6667 1295.8889 1283.1111
##  [43] 1263.2222 1261.5556 1326.6667 1754.4444 1991.6667 1992.0000 1987.1111
##  [50] 2013.4444 2026.0000 2051.7778 2048.5556 1866.8889 1370.2222 1056.0000
##  [57] 1063.6667 1068.5556 1038.3333 1024.0000  989.0000  893.6667  934.3333
##  [64]  996.5556 1330.4444 1525.4444 1535.4444 1521.2222 1500.1111 1453.4444
##  [71] 1378.2222 1172.2222  901.7778  553.0000  721.8889 1067.0000 1124.1111
##  [78] 1131.4444 1143.0000 1179.4444 1242.7778 1346.1111 1587.5556 1916.4444
##  [85] 2229.5556 2585.8889 2778.2222 2799.5556 2799.0000 2804.5556 2813.5556
##  [92] 2730.0000 2375.4444 2017.4444 1927.5556 2144.6667 2181.2222 2147.7778
##  [99] 2066.8889 1924.6667 1648.6667 1270.7778 1053.1111  991.6667  967.6667
## [106] 1218.8889 1380.6667 1430.5556 1472.4444 1520.5556 1587.3333 1638.2222
## [113] 1663.2222 1643.4444
# we can compare the smoothed vs the original lynx data
plot(lynx)

autoplot(lynx) + xlab("") + ylab("") + ggtitle("lynx' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))

plot(lynxsmoothed)

autoplot(lynxsmoothed) + xlab("") + ylab("") + ggtitle("Smoother lynx' Time-Series") +
theme(plot.title = element_text(hjust = 0.5))

# Exponential Smoothing with ets ets Using function ets
etsmodel = ets(nottem)
etsmodel
## ETS(A,N,A)
##
## Call:
##  ets(y = nottem)
##
##   Smoothing parameters:
##     alpha = 0.0392
##     gamma = 1e-04
##
##   Initial states:
##     l = 49.4597
##     s = -9.5635 -6.6186 0.5447 7.4811 11.5783 12.8567
##            8.9762 3.4198 -2.7516 -6.8093 -9.7583 -9.3556
##
##   sigma:  2.3203
##
##      AIC     AICc      BIC
## 1734.944 1737.087 1787.154
# Plotting the model vs original
plot(nottem, lwd = 3)
lines(etsmodel$fitted, col = "red") autoplot(nottem, size = 1.5) + xlab("") + ylab("") + ggtitle("nottem' Time-Series") + theme(plot.title = element_text(hjust = 0.5), legend.position = "none") + autolayer(etsmodel$fitted)

# Plotting the forecast
plot(forecast(etsmodel, h = 12))

# Changing the prediction interval
plot(forecast(etsmodel, h = 12, level = 95))

autoplot(nottem, PI = TRUE, shadecols = c("#596DD5", "#D5DBFF"),
fcol = "#0000AA", flwd = 0.5) + autolayer(forecast(etsmodel,
h = 12), level = 95, series = "nottem")
## Warning: Ignoring unknown parameters: PI, shadecols, fcol, flwd

# Manually setting the ets model
etsmodmult = ets(nottem, model = "MZM")
etsmodmult
## ETS(M,N,M)
##
## Call:
##  ets(y = nottem, model = "MZM")
##
##   Smoothing parameters:
##     alpha = 0.0214
##     gamma = 1e-04
##
##   Initial states:
##     l = 49.3793
##     s = 0.8089 0.8647 1.0132 1.1523 1.2348 1.2666
##            1.1852 1.0684 0.9405 0.8561 0.8005 0.8088
##
##   sigma:  0.0508
##
##      AIC     AICc      BIC
## 1761.911 1764.054 1814.121
# Plot as comparison
plot(nottem, lwd = 3)
lines(etsmodmult$fitted, col = "red") autoplot(nottem, size = 1.5) + xlab("") + ylab("") + ggtitle("nottem' Time-Series") + theme(plot.title = element_text(hjust = 0.5), legend.position = "none") + autolayer(etsmodmult$fitted)