fb <- gafa_stock |>
filter(Symbol == "FB") |>
mutate(day = row_number()) |>
update_tsibble(index = day, regular = TRUE)
fb_2014 <- fb |>
filter(year(Date) == 2014)
fb_2014 |>
autoplot(.vars = Close) +
labs(
x = "Days (from Jan. 2nd, 2014)",
y = "Close ($US)",
title = "Facebook Closing Values in 2014"
)Chapter 5 Homework
2. Use the Facebook stock price (from gafa_stock)
(a): Produce a plot of the time series
Here is a plot of the time series consisting of Facebook stock price closing values in 2014. The data had to be regularized as closing values were missing from weekend dates, as per market close.
(b + c): Use the drift method to make (and plot) forecasts. Show that the forcasted line is the same as extending the line from the first and last observations
fb_fit <- fb_2014 |>
model(
Drift = RW(Close ~ drift())
)
fb_2015_preds <- fb |>
filter(year(Date) == 2015) |>
filter(row_number() < 80)
fb_forecast <- fb_fit |>
forecast(new_data = fb_2015_preds)
fb_forecast |>
autoplot(
fb_2014,
level = NULL,
linewidth = 1,
alpha = 1) +
autolayer(fb_2015_preds, Close, color = "darkred") +
labs(
x = "Days (from Jan. 2nd, 2014)",
y = "Close ($US)",
title = "Facebook Closing Values in 2014"
) +
guides(color = guide_legend(title = "Forecast")) +
geom_abline(
slope = 23.31/251,
intercept = 54.6171,
color = "blue",
linetype = "dashed",
alpha = 0.75
)This is the plot of the 2014 values (again), as well as the first 80 closing values in 2015. The 80 values from 2015 are in dark red, and the forecasted line (from the drift model) in solid blue. The forecasted values are seen to lie along the same line as that which passes through the first and last observations from 2014, shown as the dashed blue line.
(d): Try forecasting the same data with the other benchmark tests
fb_fit <- fb_2014 |>
model(
Mean = MEAN(Close),
Naive = NAIVE(Close),
Drift = RW(Close ~ drift())
)
fb_2015_preds <- fb |>
filter(year(Date) == 2015) |>
filter(row_number() < 80)
fb_forecast <- fb_fit |>
forecast(new_data = fb_2015_preds)
fb_forecast |>
autoplot(
fb_2014,
level = NULL,
linewidth = 1,
alpha = 1) +
autolayer(fb_2015_preds, Close, color = "darkred") +
labs(
x = "Days (from Jan. 2nd, 2014)",
y = "Close ($US)",
title = "Facebook Closing Values in 2014"
) +
guides(color = guide_legend(title = "Forecast"))The data here is stock data, which is highly random and volatile. It can essentially be modeled by a random walk. I would expect that the drift and naive methods are best in the short-term as they both forecast values near the current observation. But, it’s hard to say for long-term forecasting, as the performance of the stock is highly dependent on the decisions of the company (and market factors).
3. Apply a seasonal naïve method to the quarterly Australian beer production data, check out the residuals
# Extract data of interest
recent_production <- aus_production |>
filter(year(Quarter) >= 1992)
# Define and estimate a model
fit <- recent_production |> model(SNAIVE(Beer))
# Look at some forecasts
fit |> forecast() |> autoplot(recent_production)# Look at the residuals
fit |> gg_tsresiduals()First, a plot of some forecasted values. Neat! Then, looking at the residual plots, we see strong evidence of the residuals reasonably clustering around zero. So, the seasonal naive method should, in theory, account for the great majority of the information available, and so will likely yield accurate forecasts.
The histogram of residuals also seems well-centered at zero, without significant left or right skew, and so the residuals should also be (roughly) normally distributed.