Time Series Analysis

Author

AR

remove(list = ls()) #clear environment


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("tsla", 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 tsla   2015-01-02  14.9  14.9  14.2  14.6  71466000     14.6
 2 tsla   2015-01-05  14.3  14.4  13.8  14.0  80527500     14.0
 3 tsla   2015-01-06  14.0  14.3  13.6  14.1  93928500     14.1
 4 tsla   2015-01-07  14.2  14.3  14.0  14.1  44526000     14.1
 5 tsla   2015-01-08  14.2  14.3  14.0  14.0  51637500     14.0
 6 tsla   2015-01-09  13.9  14.0  13.7  13.8  70024500     13.8
 7 tsla   2015-01-12  13.5  13.6  13.3  13.5  89254500     13.5
 8 tsla   2015-01-13  13.6  13.8  13.4  13.6  67159500     13.6
 9 tsla   2015-01-14  12.4  13.0  12.3  12.8 173278500     12.8
10 tsla   2015-01-15  13.0  13.1  12.7  12.8  78247500     12.8
# ℹ 2,651 more rows
#sp_500 <- tq_index("SP500") %>% tq_get(get = "stock.prices")  
df_daily <-
tq_get(x = "TSLA", 
         get = "stock.prices", 
         from = "1992-01-01")

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

str(df_daily)
tibble [3,797 × 8] (S3: tbl_df/tbl/data.frame)
 $ symbol  : chr [1:3797] "TSLA" "TSLA" "TSLA" "TSLA" ...
 $ date    : Date[1:3797], format: "2010-06-29" "2010-06-30" ...
 $ open    : num [1:3797] 1.27 1.72 1.67 1.53 1.33 ...
 $ high    : num [1:3797] 1.67 2.03 1.73 1.54 1.33 ...
 $ low     : num [1:3797] 1.17 1.55 1.35 1.25 1.06 ...
 $ close   : num [1:3797] 1.59 1.59 1.46 1.28 1.07 ...
 $ volume  : num [1:3797] 2.81e+08 2.58e+08 1.23e+08 7.71e+07 1.03e+08 ...
 $ adjusted: num [1:3797] 1.59 1.59 1.46 1.28 1.07 ...
tsla_monthly_data <- df_daily %>%
  mutate(month = yearmonth(date)) %>%
  group_by(month) %>%
  summarise(adjusted = last(adjusted)) %>%
  as_tsibble(index = month)
  

write.csv(x = tsla_monthly_data, file = "tsla_monthly_data.csv")
train <- tsla_monthly_data[1:323,] # 80%
test <- tsla_monthly_data[324:404,] # 20%
colnames(train)
[1] "month"    "adjusted"
head(train)
# A tibble: 6 × 2
     month adjusted
     <mth>    <dbl>
1 2010 Jun     1.59
2 2010 Jul     1.33
3 2010 Aug     1.30
4 2010 Sep     1.36
5 2010 Oct     1.46
6 2010 Nov     2.36
?fabletools
library(fable)
library(fabletools)
library(ggplot2)



train <- tsla_monthly_data %>%
  as_tsibble(index = month)


model_tsla <- model(train, 
                    ETS = ETS(adjusted), 
                    Drift = RW(adjusted ~ drift()), 
                    NAIVE = NAIVE(adjusted), 
                    SNAIVE = SNAIVE(adjusted)) 

h <-nrow(test)

fc_tsla <- forecast(model_tsla, h = h) 

autoplot(fc_tsla, train) +
         labs(title = "My Forecasts", x = "Time", y = "Adjusted Prices")