library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## ── Attaching packages ──────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.1     ✓ dplyr   1.0.0
## ✓ tidyr   1.1.0     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
cold <- read_csv('https://raw.githubusercontent.com/ywchiu/cdc_course/master/data/cold.csv')
## Parsed with column specification:
## cols(
##   month = col_character(),
##   cold = col_double()
## )
head(cold)
## # A tibble: 6 x 2
##   month    cold
##   <chr>   <dbl>
## 1 2004-01     0
## 2 2004-02    13
## 3 2004-03     8
## 4 2004-04     6
## 5 2004-05     6
## 6 2004-06     0
M <- ts(cold$cold, frequency = 12, start = c(2004,1))
plot(M)

#install.packages('TTR')
#??TTR
library(TTR)
## Warning: package 'TTR' was built under R version 4.0.2
M.SMA12 = SMA(M, n = 12)
M.SMA12
##            Jan       Feb       Mar       Apr       May       Jun       Jul
## 2004        NA        NA        NA        NA        NA        NA        NA
## 2005 11.083333 11.833333 12.083333 12.666667 13.750000 14.083333 13.583333
## 2006 14.083333 12.750000 12.833333 12.666667 11.666667 12.666667 14.000000
## 2007 14.666667 15.666667 16.000000 16.333333 16.583333 16.416667 15.750000
## 2008 20.166667 21.500000 22.083333 22.250000 22.750000 22.833333 23.666667
## 2009 22.416667 22.500000 23.250000 23.500000 24.750000 24.916667 25.333333
## 2010 29.833333 29.583333 28.500000 28.916667 28.166667 27.916667 27.666667
## 2011 26.333333 26.833333 28.416667 28.666667 28.500000 29.500000 29.583333
## 2012 30.833333 30.916667 31.750000 31.833333 32.833333 32.916667 32.833333
## 2013 37.500000 38.000000 38.250000 38.916667 39.000000 38.500000 38.583333
## 2014 39.750000 40.333333 39.083333 39.333333 38.916667 39.083333 39.083333
## 2015 39.750000 39.583333 40.916667 41.416667 41.916667 42.333333 42.750000
## 2016 39.916667 36.666667 34.083333 31.416667 29.666667 28.083333 26.666667
## 2017 19.166667 23.416667 25.916667 28.750000 31.333333 33.416667 35.000000
## 2018 51.916667 53.333333 53.666667 52.833333 52.333333 52.083333 51.750000
##            Aug       Sep       Oct       Nov       Dec
## 2004        NA        NA        NA        NA  8.666667
## 2005 14.083333 13.833333 14.416667 15.000000 14.666667
## 2006 13.666667 14.083333 13.500000 13.500000 13.916667
## 2007 16.500000 17.416667 18.000000 18.750000 20.000000
## 2008 23.666667 23.416667 23.666667 23.750000 22.916667
## 2009 25.250000 26.250000 27.750000 28.500000 29.416667
## 2010 28.000000 27.500000 25.916667 24.500000 24.916667
## 2011 29.166667 29.750000 30.166667 31.583333 31.666667
## 2012 33.916667 34.416667 35.416667 34.833333 35.250000
## 2013 38.250000 38.250000 38.500000 39.416667 39.833333
## 2014 39.250000 39.250000 39.250000 39.250000 39.583333
## 2015 43.083333 43.500000 44.333333 44.500000 44.416667
## 2016 25.000000 22.166667 18.833333 15.666667 14.250000
## 2017 37.166667 40.083333 43.166667 46.666667 49.583333
## 2018
plot(M, col='blue')
lines(M.SMA12, col = 'red')

mean(M[1:12])
## [1] 8.666667
M.SMA12[12]
## [1] 8.666667
library(forecast)
## Warning: package 'forecast' was built under R version 4.0.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
cold.pred <- HoltWinters(M, beta = FALSE, gamma = FALSE)
cold.pred
## Holt-Winters exponential smoothing without trend and without seasonal component.
## 
## Call:
## HoltWinters(x = M, beta = FALSE, gamma = FALSE)
## 
## Smoothing parameters:
##  alpha: 0.9999339
##  beta : FALSE
##  gamma: FALSE
## 
## Coefficients:
##       [,1]
## a 24.00046
plot(cold.pred)

?HoltWinters

cold.pred2 <-  predict(cold.pred, n.ahead = 10)
plot(cold.pred2)

library(forecast)
cold.pred<- HoltWinters(M,gamma = FALSE) 
cold.pred2 <- predict(cold.pred, n.ahead = 10) 
cold.pred2
##            Jan       Feb       Mar       Apr       May Jun Jul       Aug
## 2018                                                           22.396699
## 2019 14.380197 12.776896 11.173596  9.570295  7.966995                  
##            Sep       Oct       Nov       Dec
## 2018 20.793399 19.190098 17.586798 15.983497
## 2019
plot(cold.pred2)

components <- decompose(M)
plot(components)

library(forecast)
cold.pred2<- HoltWinters(M)
cold.pred2 <- predict(cold.pred, n.ahead = 10) 
cold.pred2
##            Jan       Feb       Mar       Apr       May Jun Jul       Aug
## 2018                                                           22.396699
## 2019 14.380197 12.776896 11.173596  9.570295  7.966995                  
##            Sep       Oct       Nov       Dec
## 2018 20.793399 19.190098 17.586798 15.983497
## 2019
plot(cold.pred2)

library(tidyverse)
cold <- read_csv('https://raw.githubusercontent.com/ywchiu/cdc_course/master/data/cold.csv')
## Parsed with column specification:
## cols(
##   month = col_character(),
##   cold = col_double()
## )
head(cold)
## # A tibble: 6 x 2
##   month    cold
##   <chr>   <dbl>
## 1 2004-01     0
## 2 2004-02    13
## 3 2004-03     8
## 4 2004-04     6
## 5 2004-05     6
## 6 2004-06     0
#install.packages('quantmod')
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.0.2
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.0.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.0.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(TTR)
M <- ts(cold$cold, frequency = 12, start = c(2004,1))
M
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2004   0  13   8   6   6   0   6   5  10  15  16  19
## 2005  29  22  11  13  19   4   0  11   7  22  23  15
## 2006  22   6  12  11   7  16  16   7  12  15  23  20
## 2007  31  18  16  15  10  14   8  16  23  22  32  35
## 2008  33  34  23  17  16  15  18  16  20  25  33  25
## 2009  27  35  32  20  31  17  23  15  32  43  42  36
## 2010  32  32  19  25  22  14  20  19  26  24  25  41
## 2011  49  38  38  28  20  26  21  14  33  29  42  42
## 2012  39  39  48  29  32  27  20  27  39  41  35  47
## 2013  66  45  51  37  33  21  21  23  39  44  46  52
## 2014  65  52  36  40  28  23  21  25  39  44  46  56
## 2015  67  50  52  46  34  28  26  29  44  54  48  55
## 2016  13  11  21  14  13   9   9   9  10  14  10  38
## 2017  72  62  51  48  44  34  28  35  45  51  52  73
## 2018 100  79  55  38  38  31  24
ts_merge <- cbind(Lag(M,-3),Lag(M,-2),Lag(M,-1), M)
colnames(ts_merge) <- c('t-3','t-2', 't-1', 'y')
head(ts_merge)
##          t-3 t-2 t-1  y
## Jan 2004  NA  NA  NA  0
## Feb 2004  NA  NA   0 13
## Mar 2004  NA   0  13  8
## Apr 2004   0  13   8  6
## May 2004  13   8   6  6
## Jun 2004   8   6   6  0
library(tidyverse)
sample_ts <- read_csv('https://raw.githubusercontent.com/ywchiu/cathayts/master/data/samples.csv')
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_character(),
##   a = col_double(),
##   b = col_double(),
##   c = col_double(),
##   d = col_double()
## )
M <- ts(sample_ts$b, frequency = 12, start = c(1950,1))
head(M)
##      Jan Feb Mar Apr May Jun
## 1950  27  22  17  15  13  16
plot(M)

library(quantmod)
plot(M - Lag(M,-1)) 

func <- function(x){
  x * 3 + 5
}

plot(seq(1,10), func(seq(1,10)), type = 'l')

a <- func(seq(1,10)) 
cbind(a, Lag(a, 1))
##        a Lag.1
##  [1,]  8    NA
##  [2,] 11     8
##  [3,] 14    11
##  [4,] 17    14
##  [5,] 20    17
##  [6,] 23    20
##  [7,] 26    23
##  [8,] 29    26
##  [9,] 32    29
## [10,] 35    32
func2 <- function(x){
  2 * x^2 + 3 * x + 5
}
plot(seq(1,10), func2(seq(1,10)), type = 'l')

a <- func2(seq(1,10))
b <- a - Lag(a, 1)
b - Lag(b,1)
##       Lag.1
##  [1,]    NA
##  [2,]    NA
##  [3,]     4
##  [4,]     4
##  [5,]     4
##  [6,]     4
##  [7,]     4
##  [8,]     4
##  [9,]     4
## [10,]     4
func <- function(x){
  x * 3 + 5
}
s <- seq(1,10)
plot(s, func(s), type='l')

## Regression
# x => s
# y => func(s)

## Time Series
# x => Lag(func(s), -n)...Lag(func(s), -2),Lag(func(s), -1)
# y => func(s)
library(tidyverse)
cold <- read_csv('https://raw.githubusercontent.com/ywchiu/cdc_course/master/data/cold.csv')
## Parsed with column specification:
## cols(
##   month = col_character(),
##   cold = col_double()
## )
head(cold)
## # A tibble: 6 x 2
##   month    cold
##   <chr>   <dbl>
## 1 2004-01     0
## 2 2004-02    13
## 3 2004-03     8
## 4 2004-04     6
## 5 2004-05     6
## 6 2004-06     0
M <- ts(cold$cold, frequency = 12, start = c(2004,1))
plot(M)

M.diff <- diff(M, lag = 1, differences = 1)
plot(M.diff)

acf(M.diff)

acf(M)

pacf(M.diff[-1])

plot(M.diff)

a <- data.frame(col = c('001', '002','003'))
paste(a$col, collapse = "','")
## [1] "001','002','003"
library(tseries)
## Warning: package 'tseries' was built under R version 4.0.2
adf.test(M)
## Warning in adf.test(M): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  M
## Dickey-Fuller = -5.9284, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
adf.test(M.diff)
## Warning in adf.test(M.diff): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  M.diff
## Dickey-Fuller = -8.6215, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
?arima
library(forecast)
plot(M)

ar_ts <- arima(M, order = c(0,1,0))
ar_forecast <- forecast(ar_ts, 12)
plot(ar_forecast)

M.arima <- auto.arima(M, ic= 'aic')
M.forecast <- forecast(M.arima, h = 12)
plot(M.forecast)

ts_merge <- cbind(Lag(M,-3),Lag(M,-2),Lag(M,-1), M)

dataset <- na.remove(ts_merge)

colnames(dataset) <- c('t_3', 't_2', 't_1', 'y')
fit <- lm(y ~ t_3+ t_2 + t_1, data = dataset)

fit
## 
## Call:
## lm(formula = y ~ t_3 + t_2 + t_1, data = dataset)
## 
## Coefficients:
## (Intercept)          t_3          t_2          t_1  
##     6.10644     -0.03917     -0.07634      0.90781