Time Series Analysis

Author

AS

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.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()
✖ 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(fpp3)
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
✔ tibble      3.3.1     ✔ tsibble     1.2.0
✔ dplyr       1.2.1     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.2     ✔ ggtime      0.2.0
✔ lubridate   1.9.5     ✔ feasts      0.5.0
✔ ggplot2     4.0.3     ✔ fable       0.5.0
── 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
starting httpd help server ... done
df_daily <-
tq_get(x = "CVX", 
     get = "stock.prices", 
     from = "1992-01-01")


cvx_data_monthly <- df_daily %>%
  mutate(month = yearmonth(date)
         ) %>%
  group_by(month) %>%
  summarise(adjusted = mean(adjusted)
            ) %>%
  as_tsibble(index = month)

write.csv(x = cvx_data_monthly, 
          file = "cvx_monthly_data.csv"
          )
train <- cvx_data_monthly[1:323,]    
test  <- cvx_data_monthly[324:404,]  
?fabletools::model
?fabletools

models_cvx <- model(
  .data = train,

  Drift  = RW(adjusted ~ drift()),
  NAIVE  = NAIVE(adjusted),
  SNAIVE = SNAIVE(adjusted)
)

# Forecast
h <- nrow(test)
fc_cvx <- forecast(models_cvx, h = h)

autoplot(object = fc_cvx, data = train) +
  autolayer(test, adjusted, colour = "black") +
  labs(title = "CVX Forecasts vs. Actual Test Data",
       x = "Time", y = "Adjusted Price")

accuracy(fc_cvx, test)
# A tibble: 3 × 10
  .model .type    ME  RMSE   MAE   MPE  MAPE  MASE RMSSE  ACF1
  <chr>  <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Drift  Test   18.2  32.2  26.9  9.79  22.6   NaN   NaN 0.953
2 NAIVE  Test   28.4  42.2  34.1 18.3   27.0   NaN   NaN 0.958
3 SNAIVE Test   27.2  41.5  33.7 17.1   26.9   NaN   NaN 0.950
data(mtcars)

model_biv <- lm(mpg ~ wt, data = mtcars)
coef(model_biv)
(Intercept)          wt 
  37.285126   -5.344472 
slope_manual <- cov(mtcars$wt, mtcars$mpg) / var(mtcars$wt)
slope_manual
[1] -5.344472
round(coef(model_biv)[["wt"]], 6) == round(slope_manual, 6)
[1] TRUE
model_multi <- lm(mpg ~ wt + hp, data = mtcars)
coef(model_multi)
(Intercept)          wt          hp 
37.22727012 -3.87783074 -0.03177295 
slope_manual_multi <- cov(mtcars$wt, mtcars$mpg) / var(mtcars$wt)
slope_manual_multi
[1] -5.344472
round(coef(model_multi)[["wt"]], 6) == round(slope_manual_multi, 6)
[1] FALSE