library(fpp3)

1 Exercise 3.7.1

Prompt: Consider the GDP information in global_economy. Plot the GDP per capita for each country over time. Which country has the highest GDP per capita? How has this changed over time?

# GDP per capita over time (all countries) — can be busy but should run
gdp_pc <- global_economy |>
  filter(!is.na(GDP), !is.na(Population)) |>
  mutate(GDP_per_capita = GDP / Population) |>
  select(Country, Year, GDP_per_capita)

autoplot(gdp_pc, GDP_per_capita) +
  labs(title = "GDP per capita by country over time", y = "GDP per capita", x = "Year")

# NOTE: gdp_pc is a tsibble (Country key, Year index). To group by Year, convert to a tibble first.
gdp_pc_tbl <- gdp_pc |> as_tibble()

# Country with the highest GDP per capita each year
top_by_year <- gdp_pc_tbl |>
  group_by(Year) |>
  slice_max(GDP_per_capita, n = 1, with_ties = FALSE) |>
  ungroup()

head(top_by_year, 15)
## # A tibble: 15 × 3
##    Country        Year GDP_per_capita
##    <fct>         <dbl>          <dbl>
##  1 United States  1960          3007.
##  2 United States  1961          3067.
##  3 United States  1962          3244.
##  4 United States  1963          3375.
##  5 United States  1964          3574.
##  6 Kuwait         1965          4429.
##  7 Kuwait         1966          4556.
##  8 United States  1967          4336.
##  9 United States  1968          4696.
## 10 United States  1969          5032.
## 11 Monaco         1970         12480.
## 12 Monaco         1971         13813.
## 13 Monaco         1972         16734.
## 14 Monaco         1973         21423.
## 15 Monaco         1974         22707.
# Overall highest GDP per capita observation in the dataset
overall_top <- gdp_pc_tbl |>
  slice_max(GDP_per_capita, n = 1, with_ties = FALSE)

overall_top
## # A tibble: 1 × 3
##   Country  Year GDP_per_capita
##   <fct>   <dbl>          <dbl>
## 1 Monaco   2014        185153.

Write-up - State which country appears most often as the top GDP per capita (from top_by_year). - Mention whether leadership changes over time (some years a different country leads).

2 Exercise 3.7.2

Prompt: For each series below, make a graph of the data. If transforming seems appropriate, do so and describe the effect. - United States GDP from global_economy - Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock - Victorian Electricity Demand from vic_elec - Gas production from aus_production

2.1 (a) United States GDP (and optional log transform)

us_gdp <- global_economy |>
  filter(Country == "United States") |>
  select(Year, GDP)

autoplot(us_gdp, GDP) +
  labs(title = "United States GDP", x = "Year", y = "GDP")

# Optional transform: log(GDP) often makes growth more linear
autoplot(us_gdp |> mutate(log_GDP = log(GDP)), log_GDP) +
  labs(title = "United States GDP (log scale)", x = "Year", y = "log(GDP)")

2.2 (b) Victoria livestock: Bulls, bullocks and steers (and optional Box-Cox)

vic_bulls <- aus_livestock |>
  filter(State == "Victoria", Animal == "Bulls, bullocks and steers") |>
  select(Month, Count)

autoplot(vic_bulls, Count) +
  labs(title = "Victoria: Bulls, bullocks and steers slaughter", x = "Month", y = "Count")

# Optional transform: Box-Cox using Guerrero method (works if positive)
lambda_bulls <- vic_bulls |> features(Count, guerrero) |> pull(lambda_guerrero)
autoplot(vic_bulls |> mutate(BC = box_cox(Count, lambda_bulls)), BC) +
  labs(title = paste0("Box-Cox transformed (lambda=", round(lambda_bulls, 2), ")"),
       x = "Month", y = "Transformed Count")

2.3 (c) Victorian electricity demand (use a small window)

# Plot only ONE WEEK to keep it light
vic_week <- vic_elec |>
  filter(Time >= ymd_hms("2014-01-01 00:00:00"),
         Time <  ymd_hms("2014-01-08 00:00:00")) |>
  select(Time, Demand)

autoplot(vic_week, Demand) +
  labs(title = "Victoria electricity demand (one week sample)", x = "Time", y = "Demand (MWh)")

# Optional transform: log demand (Demand must be positive)
autoplot(vic_week |> mutate(log_Demand = log(Demand)), log_Demand) +
  labs(title = "Victoria electricity demand (log scale, same week)", x = "Time", y = "log(Demand)")

2.4 (d) Gas production (and optional Box-Cox)

gas <- aus_production |>
  select(Quarter, Gas)

autoplot(gas, Gas) +
  labs(title = "Australian gas production", x = "Quarter", y = "Gas")

lambda_gas <- gas |> features(Gas, guerrero) |> pull(lambda_guerrero)
autoplot(gas |> mutate(BC = box_cox(Gas, lambda_gas)), BC) +
  labs(title = paste0("Gas production (Box-Cox, lambda=", round(lambda_gas, 2), ")"),
       x = "Quarter", y = "Transformed Gas")

Write-up - For each series, note trend/seasonality/variance changes. - If the transform helped, say it stabilized variance or made growth more linear.

3 Exercise 3.7.3

Prompt: Why is a Box-Cox transformation unhelpful for the canadian_gas data?

autoplot(canadian_gas, Volume) +
  labs(title = "Canadian gas", x = "Month", y = "Volume")

Write-up A Box–Cox transformation is mainly useful to stabilize changing variance (e.g., when seasonal amplitude grows with the level). For canadian_gas, the issue is not primarily variance changing with the level — the series has strong seasonal patterns and structural changes, and a simple power transform does not remove seasonality or fix those structural shifts. As a result, Box–Cox does not substantially simplify the pattern.

4 Exercise 3.7.4

Prompt: What Box–Cox transformation would you select for your retail data (from Exercise 7 in Section 2.10)?

# Choose ONE retail series (keep memory low)
retail <- aus_retail |>
  filter(State == "New South Wales", Industry == "Department stores") |>
  select(Month, Turnover)

lambda_retail <- retail |> features(Turnover, guerrero) |> pull(lambda_guerrero)
lambda_retail
## [1] 0.2193207

Write-up - Report the selected lambda from Guerrero’s method. - Mention that lambda near 0 corresponds to a log transform. - Briefly explain why (variance/seasonality grows with the level → transform helps stabilize).

5 Session Info

sessionInfo()
## R version 4.5.2 (2025-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.4 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
##  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
##  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
## [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
## 
## time zone: UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] fable_0.5.0       feasts_0.5.0      fabletools_0.6.1  ggtime_0.2.0     
##  [5] tsibbledata_0.4.1 tsibble_1.2.0     ggplot2_4.0.2     lubridate_1.9.5  
##  [9] tidyr_1.3.2       dplyr_1.2.0       tibble_3.3.1      fpp3_1.0.3       
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.6         jsonlite_2.0.0       crayon_1.5.3        
##  [4] compiler_4.5.2       tidyselect_1.2.1     Rcpp_1.1.1          
##  [7] jquerylib_0.1.4      scales_1.4.0         yaml_2.3.12         
## [10] fastmap_1.2.0        R6_2.6.1             labeling_0.4.3      
## [13] generics_0.1.4       distributional_0.6.0 knitr_1.51          
## [16] RColorBrewer_1.1-3   bslib_0.10.0         pillar_1.11.1       
## [19] rlang_1.1.7          utf8_1.2.6           cachem_1.1.0        
## [22] xfun_0.56            S7_0.2.1             sass_0.4.10         
## [25] timechange_0.4.0     cli_3.6.5            withr_3.0.2         
## [28] magrittr_2.0.4       digest_0.6.39        grid_4.5.2          
## [31] rstudioapi_0.18.0    rappdirs_0.3.4       lifecycle_1.0.5     
## [34] anytime_0.3.12       vctrs_0.7.1          evaluate_1.0.5      
## [37] glue_1.8.0           farver_2.1.2         purrr_1.2.1         
## [40] rmarkdown_2.30       tools_4.5.2          pkgconfig_2.0.3     
## [43] htmltools_0.5.9