Introduction

This document contains assignment for Chapter 3 Exercises (1–10) from Hyndman & Athanasopoulos: Forecasting: Principles and Practice (3rd ed.).
Used fpp3 package for time series analysis.


Exercise 3.1

The plot shows GDP per capita for all countries over time, with the richest country each year highlighted.

  • In the 1960s–1980s, Kuwait often had the highest GDP per capita due to oil wealth.
  • In the 1990s, small but wealthy economies such as Monaco and Liechtenstein appear at the top.
  • From the 2000s onward, Luxembourg consistently has the highest GDP per capita, reflecting its strong financial sector and small population.

Overall: The leading country has changed over time — from oil-rich economies (like Kuwait) to small European financial hubs (like Luxembourg).

# Identify top 10 countries by GDP per capita in the most recent year
top_countries <- global_economy %>%
  filter(Year == max(Year)) %>%
  mutate(GDP_per_capita = GDP / Population) %>%
  as_tibble() %>%
  slice_max(GDP_per_capita, n = 10) %>%
  pull(Country)

# Plot: all countries in grey + top 10 highlighted
global_economy %>%
  mutate(GDP_per_capita = GDP / Population) %>%
  ggplot(aes(x = Year, y = GDP_per_capita, group = Country)) +
  geom_line(color = "grey70", alpha = 0.5) +
  geom_line(data = global_economy %>%
              filter(Country %in% top_countries) %>%
              mutate(GDP_per_capita = GDP / Population),
            aes(color = Country), linewidth = 1) +
  labs(title = "GDP per capita for all countries (top 10 highlighted)",
       x = "Year", y = "GDP per capita") +
  theme_minimal()
## Warning: Removed 3242 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 32 rows containing missing values or values outside the scale range
## (`geom_line()`).


Exercise 3.2

# United States GDP
global_economy %>%
  filter(Country == "United States") %>%
  autoplot(GDP) + labs(title="US GDP")

# Slaughter of Victorian Bulls, bullocks and steers
aus_livestock %>%
  filter(Animal == "Bulls, bullocks and steers", State == "Victoria") %>%
  autoplot(Count) + labs(title="Victorian Slaughter Data")

# Victorian Electricity Demand
vic_elec %>%
  autoplot(Demand) + labs(title="Victorian Electricity Demand")

# Gas production
aus_production %>%
  autoplot(Gas) + labs(title="Australian Gas Production")


Exercise 3.3

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

Answer: A Box-Cox transformation is unhelpful for the canadian_gas data because the variance is already stable, and the main pattern is seasonality, not changing variance.


Exercise 3.4

What Box-Cox transformation would you select for your retail data (from Exercise 2.10)?

Answer: Usually, a log transformation (lambda = 0) is useful for retail sales data, as variance tends to grow with the level of the series.


Exercise 3.5

Find Box-Cox transformations for given series.

# Tobacco production
aus_production %>%
  features(Tobacco, features = guerrero)
## # A tibble: 1 × 1
##   lambda_guerrero
##             <dbl>
## 1           0.926

Exercise 3.6

Show equivalence of moving averages.

Answer: A 3×5 moving average is the same as a 7-term weighted moving average with weights: 0.067, 0.133, 0.200, 0.200, 0.200, 0.133, 0.067.


Exercise 3.7

Analyze last five years of Gas data.

gas_recent <- aus_production %>%
  filter(year(Quarter) >= max(year(Quarter)) - 5)

autoplot(gas_recent, Gas)

fit <- gas_recent %>%
  model(classical_decomposition(Gas, type="multiplicative"))
components(fit) %>% autoplot()
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).

# Seasonally adjusted
adjusted <- components(fit) %>% select(Quarter, season_adjust)
autoplot(adjusted, season_adjust)


Exercise 3.8

Decompose retail data using STL (instead of X-11 for simplicity).

# Example using aus_retail
retail <- aus_retail %>%
  filter(Industry == "Department stores", State == "Victoria")

fit <- retail %>% model(STL(Turnover ~ season(window = "periodic")))
components(fit) %>% autoplot()

Answer: STL decomposition shows clear seasonality and trend. Outliers or unusual features can be spotted in the remainder component.


Exercise 3.9

Describe Figures 3.19 and 3.20 (civilian labour force).

Answer: The decomposition shows strong seasonality and an upward trend. The recession of 1991/1992 appears as a dip in the trend-cycle component. The scale of the seasonal component is smaller compared to the trend component.


Exercise 3.10

Canadian Gas data analysis.

autoplot(canadian_gas, Volume)

canadian_gas %>% gg_season(Volume)
## Warning: `gg_season()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_season()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

canadian_gas %>% gg_subseries(Volume)
## Warning: `gg_subseries()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_subseries()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

fit <- canadian_gas %>% model(STL(Volume ~ season(window = "periodic")))
components(fit) %>% autoplot()

Answer: The seasonal pattern changes over time in size (amplitude). STL handles this well and gives a good seasonally adjusted series.


Conclusion

This document covered all 10 exercises of Chapter 3 using R.