knitr::opts_chunk$set(echo = TRUE)
library(fpp3)
## Warning: package 'fpp3' was built under R version 4.3.3
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.0 ──
## ✔ tibble      3.2.1     ✔ tsibble     1.1.5
## ✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
## ✔ tidyr       1.3.0     ✔ feasts      0.3.2
## ✔ lubridate   1.9.3     ✔ fable       0.3.4
## ✔ ggplot2     3.5.1     ✔ fabletools  0.4.2
## Warning: package 'tsibble' was built under R version 4.3.3
## ── 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(USgas)
library(tsibble)
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0     ✔ readr   2.1.4
## ✔ purrr   1.0.2     ✔ stringr 1.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag()        masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fpp3)
library(USgas)
library(tsibble)
library(ggplot2)
library(tidyverse)
library(fable)
library(tsibble)
library(fabletools)
library(fable)
library(dplyr)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

aus_economy <- global_economy |>
  filter(Code == "AUS") |>
  mutate(Pop = Population / 1e6)
autoplot(aus_economy, Pop) +
  labs(y = "Millions", title = "Australian population")

Including Plots

algeria_economy <- global_economy |>
  filter(Country == "Algeria")
algeria_economy |>
  autoplot(Exports) +
  labs(y = "% of GDP", title = "Exports: Algeria")

aus_economy <- global_economy |>
  filter(Code == "AUS") |>
  mutate(Pop = Population / 1e6)
autoplot(aus_economy, Pop) +
  labs(y = "Millions", title = "Australian population")

library(fable)
library(dplyr)

# Fit Holt’s linear trend model to the Australian population data
fit <- aus_economy |>
  model(
    Holt = ETS(Pop ~ error("A") + trend("A") + season("N"))
  )

# Generate the 10-year forecast
fc <- fit |> forecast(h = 10)

# Check the structure of the forecast object
# This will display the column names and a preview of the data
print(fc)
## # A fable: 10 x 5 [1Y]
## # Key:     Country, .model [1]
##    Country   .model  Year           Pop .mean
##    <fct>     <chr>  <dbl>        <dist> <dbl>
##  1 Australia Holt    2018 N(25, 0.0041)  25.0
##  2 Australia Holt    2019  N(25, 0.011)  25.3
##  3 Australia Holt    2020  N(26, 0.023)  25.7
##  4 Australia Holt    2021  N(26, 0.039)  26.1
##  5 Australia Holt    2022  N(26, 0.061)  26.4
##  6 Australia Holt    2023   N(27, 0.09)  26.8
##  7 Australia Holt    2024   N(27, 0.13)  27.2
##  8 Australia Holt    2025   N(28, 0.17)  27.6
##  9 Australia Holt    2026   N(28, 0.22)  27.9
## 10 Australia Holt    2027   N(28, 0.29)  28.3
fit <- aus_economy |>
  model(
    AAN = ETS(Pop ~ error("A") + trend("A") + season("N"))
  )
fc <- fit |> forecast(h = 10)
fc
## # A fable: 10 x 5 [1Y]
## # Key:     Country, .model [1]
##    Country   .model  Year           Pop .mean
##    <fct>     <chr>  <dbl>        <dist> <dbl>
##  1 Australia AAN     2018 N(25, 0.0041)  25.0
##  2 Australia AAN     2019  N(25, 0.011)  25.3
##  3 Australia AAN     2020  N(26, 0.023)  25.7
##  4 Australia AAN     2021  N(26, 0.039)  26.1
##  5 Australia AAN     2022  N(26, 0.061)  26.4
##  6 Australia AAN     2023   N(27, 0.09)  26.8
##  7 Australia AAN     2024   N(27, 0.13)  27.2
##  8 Australia AAN     2025   N(28, 0.17)  27.6
##  9 Australia AAN     2026   N(28, 0.22)  27.9
## 10 Australia AAN     2027   N(28, 0.29)  28.3
www_usage <- as_tsibble(WWWusage)
www_usage |> autoplot(value) +
  labs(x="Minute", y="Number of users",
       title = "Internet usage per minute")

aus_economy |>
  model(
    `Holt's method` = ETS(Pop ~ error("A") +
                       trend("A") + season("N")),
    `Damped Holt's method` = ETS(Pop ~ error("A") +
                       trend("Ad", phi = 0.9) + season("N"))
  ) |>
  forecast(h = 15) |>
  autoplot(aus_economy, level = NULL) +
  labs(title = "Australian population",
       y = "Millions") +
  guides(colour = guide_legend(title = "Forecast"))

www_usage <- as_tsibble(WWWusage)
www_usage |>
  stretch_tsibble(.init = 10) |>
  model(
    SES = ETS(value ~ error("A") + trend("N") + season("N")),
    Holt = ETS(value ~ error("A") + trend("A") + season("N")),
    Damped = ETS(value ~ error("A") + trend("Ad") +
                   season("N"))
  ) |>
  forecast(h = 1) |>
  accuracy(www_usage)
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing. 
## 1 observation is missing at 101
## # 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 Damped Test  0.288   3.69  3.00 0.347  2.26 0.663 0.636 0.336
## 2 Holt   Test  0.0610  3.87  3.17 0.244  2.38 0.701 0.668 0.296
## 3 SES    Test  1.46    6.05  4.81 0.904  3.55 1.06  1.04  0.803