TimeSeries.H.W

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.12 ──
✔ PerformanceAnalytics 2.1.0      ✔ TTR                  0.24.4
✔ quantmod             0.4.29     ✔ xts                  0.14.2── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
✖ zoo::as.Date()                 masks base::as.Date()
✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
✖ dplyr::filter()                masks stats::filter()
✖ xts::first()                   masks dplyr::first()
✖ dplyr::lag()                   masks stats::lag()
✖ xts::last()                    masks dplyr::last()
✖ 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
library(tsibble)

Attaching package: 'tsibble'

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

    index

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

    interval

The following objects are masked from 'package:base':

    intersect, setdiff, union
library(fabletools)
library(fable)

Attaching package: 'fable'

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

    VAR
df_daily <- tq_get(
  x = "GOOGL", 
  get = "stock.prices", 
  from = "2004-08-19" # Google went public in August 2004
)

googl_data_monthly <- df_daily %>%
  mutate(month = yearmonth(date)) %>%
  group_by(month) %>%
  summarise(adjusted = mean(adjusted, na.rm = TRUE)) %>%
  as_tsibble(index = month)


write.csv(
  x = googl_data_monthly, 
  file = "googl_monthly_data.csv",
  row.names = FALSE
)
library(tidyverse)
library(tidyquant)
library(tsibble)


n_total <- nrow(googl_data_monthly)
n_train <- round(0.80 * n_total)

train <- googl_data_monthly[1:n_train, ]
test  <- googl_data_monthly[(n_train + 1):n_total, ]
models_googl <- model(
  .data = train,
  # ETS   = ETS(adjusted),
  Drift = RW(adjusted ~ drift()),
  NAIVE = NAIVE(adjusted),
  SNAIVE = SNAIVE(adjusted)
)


h <- nrow(test)
fc_googl <- forecast(models_googl, h = h)


autoplot(object = fc_googl, data = train) + 
  labs(
    title = "Google Stock Price Forecasts", 
    x = "Time", 
    y = "Adjusted Price"
  )
Warning: `autoplot.fbl_ts()` was deprecated in fabletools 0.6.0.
ℹ Please use `ggtime::autoplot.fbl_ts()` instead.
ℹ Graphics functions have been moved to the {ggtime} package. Please use
  `library(ggtime)` instead.