For this week’s discussion, I decided to use the Microsoft stock data from my discussion 6. The one caveat to using stock price data is that there are no data points for Saturdays and Sundays. Since tsibble() needs a continuous time series, I used tsibble::fill_gaps() to plug in the missing dates. These dates do not have a closing price and default to NA. I replaced the NAs with the closing price mean.

I did have an issue with reading the neural net results. You can see the results seem oddly high as the model it recommends a NNAR(29,1,15)[7] model. Did anyone else have this issue or have any critiques? I followed the textbook’s code. Overall, the NNETAR() produced a decent forecast. The downside forecasts were way more severe than the peaks, that is as expected. This is normal for a NNETAR() compared to an AR model, where the AR model has symmetrical cycles.

If anyone has feedback as far as the output of their NNETAR() model creation, I’m interested to hear.

# Clear the workspace
rm(list = ls()) # Clear environment
gc()            # Clear unused memory
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 460850 24.7     997242 53.3   638942 34.2
## Vcells 827100  6.4    8388608 64.0  1633064 12.5
cat("\f")       # Clear the console
library("feasts")
library("seasonal")
library("tsibble")
library("MASS")
library("tsibbledata")
library("dplyr")
library("ggplot2")
library("forecast")
library("fable")
library("tseries")
library("weathermetrics")
library("seasonalview")
library("tibbletime")
library("fpp3")
library("fma")
library("vars")
library("expsmooth")

setwd("/Users/spoll/OneDrive/Documents/Boston College/Predictive Analytics_Forecasting/Week 6")

# Load the datasets
msft <- read.csv("MSFT.csv"
                  , check.names = FALSE
                  , stringsAsFactors = FALSE
                  , na.strings = ""
)

msft2 <- msft %>%
  dplyr::select("Date", "Close")
msft2$Date <- mdy(msft2$Date)

# Plot the time series of each stock
msft_ts <- ts(msft2$Close, frequency = 365)
msft_ts %>% autoplot() + labs(title="MSFT Stock Close") + 
  ylab("Price ($)") +
  theme(plot.title = element_text(hjust = 0.5))

The forecast results show a that the NNETAR model

## # A mable: 1 x 1
##   `NNETAR(sqrt(Close))`
##                 <model>
## 1    <NNAR(29,1,15)[7]>
fit_nnet_fc <- forecast(fit_nnet
                        , PI=TRUE
                        , h=30)
autoplot(fit_nnet_fc)

fit_nnet_fc %>%
  autoplot(msft2) +
  labs(x = "Time", y = "Closing Price", title = "MSFT Forecasted Closing Price using Neural Net")
## Using `Date` as index variable.

# fit_nnet_fc %>%
#   generate(times = 9, h = 30) %>%
#   autoplot(.sim) +
#   autolayer(msft2, Close) +
#   theme(legend.position = "none")