## import necessary packages
library(fpp3)
## ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
## ✔ tibble 3.2.1 ✔ tsibble 1.1.4
## ✔ dplyr 1.1.3 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.0 ✔ feasts 0.3.1
## ✔ lubridate 1.9.3 ✔ fable 0.3.3
## ✔ ggplot2 3.4.4 ✔ fabletools 0.4.0
## ── 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()
## Question 1
australia <- global_economy %>%
filter(Country == "Australia")
australia %>%
model(NAIVE(Population)) %>%
forecast(h = 10) %>%
autoplot(australia)
australia %>%
model(RW(Population ~ drift())) %>%
forecast(h = 20) %>%
autoplot(australia) +
labs(title = "Population Growth in Australia")
brick_fit <- aus_production %>%
filter(!is.na(Bricks)) %>%
model(
Seasonal_naive = SNAIVE(Bricks)
)
brick_fc <- brick_fit %>%
forecast(h = "5 years")
brick_fc %>%
autoplot(aus_production, level = NULL) +
labs(title = "Brick Production in Australia", y = "Millions of Bricks") +
guides(colour = guide_legend(title = "Forecast"))
## Warning: Removed 20 rows containing missing values (`geom_line()`).
nsw_lambs <- aus_livestock %>% filter(Animal == "Lambs" & State == "New South Wales")
lambs_fit <- nsw_lambs %>%
filter(!is.na(Count)) %>%
model(seasonal_naive = SNAIVE(Count),
naive = NAIVE(Count),
drift = RW(Count ~ drift())
)
lambs_fc <-lambs_fit %>%
forecast(h = "10 years")
lambs_fc %>%
autoplot(nsw_lambs, level = NULL) +
labs(title = "Lamb Counts in Australia") +
guides(color = guide_legend((title = "Forecast")))
aus_wealth <- hh_budget %>%
filter(Country == "Australia")
wealth_fit <- aus_wealth %>%
filter(!is.na(Wealth)) %>%
model(seasonal_naive = SNAIVE(Wealth),
naive = NAIVE(Wealth),
drive = RW(Wealth ~ drift()))
## Warning: 1 error encountered for seasonal_naive
## [1] Non-seasonal model specification provided, use RW() or provide a different lag specification.
wealth_fc <- wealth_fit %>%
forecast(h = "5 years")
wealth_fc %>%
autoplot(aus_wealth, level = NULL) +
labs(title = "Household Wealth in Australia")
## Warning: Removed 5 rows containing missing values (`geom_line()`).
wealth_fc %>% filter(.model == "drive") %>%
autoplot(aus_wealth) + labs(title = "Household wealth in Australia")
food_turnover <- aus_retail %>%
filter(Industry == "Takeaway food services" & State == "Australian Capital Territory")
food_fit <- food_turnover %>%
filter(!is.na(Turnover)) %>%
model(naive = NAIVE(Turnover),
seasonal_naive = SNAIVE(Turnover),
drive = RW(Turnover ~ drift()))
food_fc <- food_fit %>%
forecast(h = "5 years")
food_fc %>%
autoplot(food_turnover, level = NULL)
facebook <- gafa_stock %>%
filter(Symbol == "FB") %>%
mutate(trading_day = row_number()) %>%
update_tsibble(index = trading_day, regular = TRUE)
facebook %>% autoplot(Close) +
labs(title = "Facebook Closing Price")
facebook_fit <- facebook %>%
model(drift = RW(Close ~ drift()))
facebook_fc <- facebook_fit %>%
forecast(h = 100)
facebook_fc %>%
autoplot(facebook, level = NULL) +
labs(title ="Facebook Adjusted Closing Price Forecast")
C.)
facebook_slope <- ((facebook$Close[1258] - facebook$Close[1])/(1258 - 1))
print(facebook_slope)
## [1] 0.06076372
facebook_fc %>%
autoplot(facebook, level = NULL) +
labs(title = "Facebook Adjusted Closing Price Forecast") +
geom_abline(slope = facebook_slope, intercept = 54.71, alpha = 0.3)
facebook_fit <- facebook %>%
model(naive = NAIVE(Close),
drift = RW(Close ~ drift(),
seasonal_naive = SNAIVE(Close),
mean = MEAN(Close)))
facebook_fc <- facebook_fit %>%
forecast(h = 200)
facebook_fc %>%
autoplot(facebook, level = NULL) +
labs(title ="Facebook Adjusted Closing Price Forecast")
# 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 the residuals
fit %>% gg_tsresiduals()
## Warning: Removed 4 rows containing missing values (`geom_line()`).
## Warning: Removed 4 rows containing missing values (`geom_point()`).
## Warning: Removed 4 rows containing non-finite values (`stat_bin()`).
# Look a some forecasts
fit %>% forecast() %>% autoplot(recent_production)
recent_exports <- global_economy %>%
filter(Country == "Australia" & Year >= 1992)
fit_exports <- recent_exports %>% model(NAIVE(Exports))
fit_exports %>% gg_tsresiduals()
## Warning: Removed 1 row containing missing values (`geom_line()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_bin()`).
fit_exports %>% forecast(h = 10) %>%
autoplot(recent_exports)
recent_bricks <- aus_production %>%
filter(year(Quarter) >= 1992 & !is.na(Bricks))
fit_bricks <- recent_bricks %>%
model(seaonal_naive = SNAIVE(Bricks))
fit_bricks %>%
gg_tsresiduals()
## Warning: Removed 4 rows containing missing values (`geom_line()`).
## Warning: Removed 4 rows containing missing values (`geom_point()`).
## Warning: Removed 4 rows containing non-finite values (`stat_bin()`).
fit_bricks %>%
forecast(h = "5 years") %>%
autoplot(recent_bricks) +
labs(title = "Brick Production Forecast")
set.seed(12645678)
myseries <- aus_retail |>
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries_train <- myseries |>
filter(year(Month) < 2011)
autoplot(myseries, Turnover) +
autolayer(myseries_train, Turnover,colour = "red")
fit <- myseries_train |>
model(SNAIVE(Turnover))
fit %>% gg_tsresiduals()
## Warning: Removed 12 rows containing missing values (`geom_line()`).
## Warning: Removed 12 rows containing missing values (`geom_point()`).
## Warning: Removed 12 rows containing non-finite values (`stat_bin()`).
fc <- fit |>
forecast(new_data = anti_join(myseries, myseries_train))
## Joining with `by = join_by(State, Industry, `Series ID`, Month, Turnover)`
fc |> autoplot(myseries, level = NULL)
fit |> accuracy()
## # A tibble: 1 × 12
## State Industry .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 South A… Clothin… SNAIV… Trai… 2.87 6.26 4.81 4.24 9.16 1 1 0.695
fc |> accuracy(myseries)
## # A tibble: 1 × 12
## .model State Industry .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 SNAIVE(T… Sout… Clothin… Test -4.06 12.0 8.80 -5.47 8.91 1.83 1.91 0.627
myseries_train_less <- myseries %>%
filter(year(Month) < 2000)
fit <- myseries_train_less %>%
model(SNAIVE(Turnover))
fc <- fit %>%
forecast(new_data = anti_join(myseries, myseries_train_less))
## Joining with `by = join_by(State, Industry, `Series ID`, Month, Turnover)`
fit %>% accuracy()
## # A tibble: 1 × 12
## State Industry .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 South A… Clothin… SNAIV… Trai… 1.22 4.50 3.58 2.59 9.35 1 1 0.748
fc %>% accuracy(myseries)
## # A tibble: 1 × 12
## .model State Industry .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 SNAIVE(T… Sout… Clothin… Test 37.7 43.7 38.0 39.9 40.4 10.6 9.72 0.859
fc |> autoplot(myseries, level = NULL)