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
# Estimate parameters
fit <- algeria_economy |>
  model(ETS(Exports ~ error("A") + trend("N") + season("N")))
fc <- fit |>
  forecast(h = 5)
fit
## # A mable: 1 x 2
## # Key:     Country [1]
##   Country `ETS(Exports ~ error("A") + trend("N") + season("N"))`
##   <fct>                                                  <model>
## 1 Algeria                                           <ETS(A,N,N)>
# Estimate parameters
fit <- algeria_economy |>
  model(ETS(Exports ~ error("A") + trend("N") + season("N")))
fc <- fit |>
  forecast(h = 5)
aus_economy
## # A tsibble: 58 x 10 [1Y]
## # Key:       Country [1]
##    Country   Code   Year       GDP Growth   CPI Imports Exports Population   Pop
##    <fct>     <fct> <dbl>     <dbl>  <dbl> <dbl>   <dbl>   <dbl>      <dbl> <dbl>
##  1 Australia AUS    1960   1.86e10  NA     7.96    14.1    13.0   10276477  10.3
##  2 Australia AUS    1961   1.96e10   2.49  8.14    15.0    12.4   10483000  10.5
##  3 Australia AUS    1962   1.99e10   1.30  8.12    12.6    13.9   10742000  10.7
##  4 Australia AUS    1963   2.15e10   6.21  8.17    13.8    13.0   10950000  11.0
##  5 Australia AUS    1964   2.38e10   6.98  8.40    13.8    14.9   11167000  11.2
##  6 Australia AUS    1965   2.59e10   5.98  8.69    15.3    13.2   11388000  11.4
##  7 Australia AUS    1966   2.73e10   2.38  8.98    15.1    12.9   11651000  11.7
##  8 Australia AUS    1967   3.04e10   6.30  9.29    13.9    12.9   11799000  11.8
##  9 Australia AUS    1968   3.27e10   5.10  9.52    14.5    12.3   12009000  12.0
## 10 Australia AUS    1969   3.66e10   7.04  9.83    13.3    12.0   12263000  12.3
## # ℹ 48 more rows
library(fable)
library(dplyr)

fit <- aus_economy %>%
  filter(Year <= 2010) %>%
  model(
    ses = ETS(Pop ~ error("A") + trend("N") + season("N")),
    holt = ETS(Pop ~ error("A") + trend("A") + season("N")),
    damped = ETS(Pop ~ error("A") + trend("Ad") + season("N"))
  )

# Extracting coefficients using coef()
coef(fit)
## # A tibble: 11 × 4
##    Country   .model term  estimate
##    <fct>     <chr>  <chr>    <dbl>
##  1 Australia ses    alpha    1.00 
##  2 Australia ses    l[0]    10.3  
##  3 Australia holt   alpha    1.00 
##  4 Australia holt   beta     0.296
##  5 Australia holt   l[0]    10.1  
##  6 Australia holt   b[0]     0.224
##  7 Australia damped alpha    1.00 
##  8 Australia damped beta     0.402
##  9 Australia damped phi      0.980
## 10 Australia damped l[0]    10.0  
## 11 Australia damped b[0]     0.246
# Getting model accuracy
accuracy(fit)
## # A tibble: 3 × 11
##   Country   .model .type       ME   RMSE    MAE    MPE  MAPE  MASE RMSSE    ACF1
##   <fct>     <chr>  <chr>    <dbl>  <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>   <dbl>
## 1 Australia ses    Train… 0.231   0.242  0.231  1.48   1.48  0.980 0.990  0.267 
## 2 Australia holt   Train… 0.00716 0.0646 0.0451 0.0336 0.289 0.192 0.264  0.0504
## 3 Australia damped Train… 0.0157  0.0665 0.0466 0.0883 0.300 0.198 0.272 -0.0334