library(fpp3)
## ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
## ✔ tibble 3.2.1 ✔ tsibble 1.1.4
## ✔ dplyr 1.1.2 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.0 ✔ feasts 0.3.1
## ✔ lubridate 1.9.2 ✔ fable 0.3.3
## ✔ ggplot2 3.4.4 ✔ fabletools 0.3.4
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(seasonal)
##
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
##
## view
library(dplyr)
library(ggplot2)
library('fabletools')
aus_population = global_economy %>% filter(Country=="Australia")
autoplot(aus_population, Population)+
labs(title = "Australian Population")
fit = aus_population %>% model(Drift = NAIVE(Population~drift()))
#generate forecast
aus_pop_forcast = fit %>%
forecast(h=10)
#plot forecast
aus_pop_forcast %>%
autoplot(aus_population)
### Bricks (aus_production)
aus_bricks = aus_production %>% filter(!is.na(Bricks))
autoplot(aus_bricks , Bricks)
brick_model = aus_bricks %>% model(snaive = SNAIVE(Bricks ~ lag("year")))
brick_forcast = brick_model %>% forecast(h=10)
#plot
brick_forcast %>% autoplot(aus_bricks )
### NSW Lambs (aus_livestock)
NSW_Lambs = aus_livestock %>% filter(Animal == "Lambs", State=="New South Wales")
autoplot(NSW_Lambs)
## Plot variable not specified, automatically selected `.vars = Count`
NSW_Lambs_model = NSW_Lambs %>% model(snaive = SNAIVE(Count ~ lag("year")))
nsw_forcast = NSW_Lambs_model %>% forecast(h=10)
nsw_forcast %>% autoplot(NSW_Lambs)
### Household wealth (hh_budget).
hh_wealth = hh_budget %>% select(Wealth) %>% filter(Country=='Australia') %>% filter(!is.na(Wealth))
hh_wealth_fit = hh_wealth %>%
model(`naive` = NAIVE(Wealth))
hh_wealth_fit %>% forecast(h = 10) %>% autoplot(hh_wealth)
aus_takeaway = aus_retail %>% filter(Industry=='Takeaway food services',State=='Australian Capital Territory')
aus_takeaway_fit = aus_takeaway %>%
model(`snaive` = SNAIVE(Turnover))
aus_takeaway_fit %>% forecast(h = 36) %>% autoplot(aus_takeaway)
Stock = gafa_stock %>% filter(Symbol == "FB") %>%
select(Close) %>% mutate(day=row_number())
autoplot(Stock)
## Plot variable not specified, automatically selected `.vars = Close`
### Produce forecasts using the drift method and plot them.
fb_ts <- gafa_stock |> filter(Symbol=='FB') |>
mutate(trading_day = row_number()) |>
update_tsibble(index = trading_day, regular = TRUE)
fb_jan_2018 <- fb_ts |>
filter(yearmonth(Date) >= yearmonth("2017 Dec"))
fb_pre_2018 <- fb_ts |> filter(Symbol=='FB' & year(Date)<= 2017)
fb_pre_2018 |>
model(RW(Close ~ drift())) |>
forecast(h=30) |>
autoplot(fb_pre_2018)
### Show that the forecasts are identical to extending the line drawn
between the first and last observations.
fb_pre_2018 |>
model(RW(Close ~ drift())) |>
forecast(h=30) |>
autoplot(fb_pre_2018) +
#autolayer()
geom_segment(aes(x = 0, y = pull(fb_pre_2018 |> filter(trading_day==1),Close), xend = 1007, yend = pull(fb_pre_2018 |> filter(trading_day==1007),Close),linetype=2 ),,colour='blue',linetype=2)
recent_production <- aus_production |>
filter(year(Quarter) >= 1992)
fit <- recent_production |> model(SNAIVE(Beer))
fit |> gg_tsresiduals()
What do you conclude?
For your retail time series (from Exercise 7 in Section 2.10):
Create a training dataset consisting of observations before 2011 using
myseries_train <- myseries |> filter(year(Month) < 2011) Check that your data have been split appropriately by producing the following plot.
autoplot(myseries, Turnover) + autolayer(myseries_train, Turnover, colour = “red”) Fit a seasonal naïve model using SNAIVE() applied to your training data (myseries_train).
fit <- myseries_train |> model(SNAIVE()) Check the residuals.
fit |> gg_tsresiduals() Do the residuals appear to be uncorrelated and normally distributed?
Produce forecasts for the test data
fc <- fit |> forecast(new_data = anti_join(myseries, myseries_train)) fc |> autoplot(myseries) Compare the accuracy of your forecasts against the actual values.
fit |> accuracy() fc |> accuracy(myseries) How sensitive are the accuracy measures to the amount of training data used?