Time Series Analysis

Author

AR

remove(list = ls())


library("tidyquant")
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
✔ PerformanceAnalytics 2.0.8      ✔ TTR                  0.24.4
✔ quantmod             0.4.28     ✔ xts                  0.14.1
── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
✖ zoo::as.Date()                 masks base::as.Date()
✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
✖ PerformanceAnalytics::legend() masks graphics::legend()
✖ quantmod::summary()            masks base::summary()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#install.packages("fpp3")
library(fpp3)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.1 ──
✔ tibble      3.3.0     ✔ tsibble     1.1.6
✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.1     ✔ feasts      0.4.1
✔ lubridate   1.9.4     ✔ fable       0.4.1
✔ ggplot2     3.5.2     
── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ dplyr::first()       masks xts::first()
✖ tsibble::index()     masks zoo::index()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ dplyr::last()        masks xts::last()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()
✖ fable::VAR()         masks tidyquant::VAR()

Attaching package: 'fpp3'

The following object is masked from 'package:PerformanceAnalytics':

    prices
tq_get("msft", get = "stock.prices")
# A tibble: 2,661 × 8
   symbol date        open  high   low close   volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 msft   2015-01-02  46.7  47.4  46.5  46.8 27913900     40.0
 2 msft   2015-01-05  46.4  46.7  46.2  46.3 39673900     39.6
 3 msft   2015-01-06  46.4  46.8  45.5  45.7 36447900     39.0
 4 msft   2015-01-07  46.0  46.5  45.5  46.2 29114100     39.5
 5 msft   2015-01-08  46.8  47.8  46.7  47.6 29645200     40.7
 6 msft   2015-01-09  47.6  47.8  46.9  47.2 23944200     40.4
 7 msft   2015-01-12  47.4  47.5  46.4  46.6 23651900     39.9
 8 msft   2015-01-13  47.0  47.9  46.1  46.4 35270600     39.7
 9 msft   2015-01-14  46.0  46.2  45.6  46.0 29719600     39.3
10 msft   2015-01-15  46.2  46.4  45.4  45.5 32750800     38.9
# ℹ 2,651 more rows
#sp_500 <- tq_index("SP500") %>% tq_get(get = "stock.prices")  
df_daily <-
tq_get(x = "MSFT", 
         get = "stock.prices", 
         from = "1992-01-01")

library(dplyr)
library(tsibble)
library(lubridate)

str(df_daily)
tibble [8,456 × 8] (S3: tbl_df/tbl/data.frame)
 $ symbol  : chr [1:8456] "MSFT" "MSFT" "MSFT" "MSFT" ...
 $ date    : Date[1:8456], format: "1992-01-02" "1992-01-03" ...
 $ open    : num [1:8456] 2.31 2.38 2.36 2.43 2.48 ...
 $ high    : num [1:8456] 2.39 2.38 2.45 2.51 2.62 ...
 $ low     : num [1:8456] 2.28 2.33 2.34 2.41 2.47 ...
 $ close   : num [1:8456] 2.38 2.35 2.43 2.5 2.59 ...
 $ volume  : num [1:8456] 74265600 38246400 63057600 64363200 90763200 ...
 $ adjusted: num [1:8456] 1.46 1.44 1.49 1.53 1.59 ...
msft_monthly_data <-
df_daily %>%
  mutate(month = yearmonth(date)) %>%
  group_by(month) %>%
  summarise(adjusted = last(adjusted)) %>%
  as_tsibble(index = month)
  

write.csv(x = msft_monthly_data, file = "msft_monthly_data.csv")
train <- msft_monthly_data[1:323,] # 80%
test <- msft_monthly_data[324:404,] # 20%
model_msft <- model(
  train, 
  ETS = ETS(adjusted), 
  NAIVE = NAIVE(adjusted), 
  SNAIVE = SNAIVE(adjusted)
  )

h <- nrow(test)
fc_msft <- forecast(model_msft, h = h)
autoplot(fc_msft, train)