library(tidyverse)
library(fpp3)

1

# calculate GDP per capita
gdp_pc <- global_economy %>%
  mutate(GDP_per_capita = GDP / Population) %>%
  drop_na()

# Plot
autoplot(gdp_pc, GDP_per_capita) +
  facet_wrap(~ Country, scales = "free_y") +
  labs(y = "GDP per Capita")

# Highest GDP per capita 
gdp_pc %>%
  filter(!is.na(GDP_per_capita)) %>%
  group_by(Country) %>%
  summarise(max_gdp_pc = max(GDP_per_capita)) %>%
  slice_max(max_gdp_pc, n = 1)
## # A tsibble: 1 x 3 [1Y]
## # Key:       Country [1]
##   Country     Year max_gdp_pc
##   <fct>      <dbl>      <dbl>
## 1 Luxembourg  2014    119225.
# Luxembourg had highest GDP per capita
# plot Luxembourg
global_economy %>%
  filter(Country == "Luxembourg") %>%
  mutate(GDP_per_capita = GDP / Population) %>%
  ggplot(aes(x = Year, y = GDP_per_capita)) +
  geom_line() +
  labs(title = "Luxembourg GDP per Capita", y = "GDP per Capita")

Luxembourg had the highest GDP per capita. It increased strongly over time, with brief dips during global downturns, but remained the highest overall.

2.

# 1. United States GDP 
global_economy %>%
  filter(Country == "United States") %>%
  ggplot(aes(x = Year, y = GDP)) +
  geom_line()

#log transform
global_economy %>%
  filter(Country == "United States") %>%
  ggplot(aes(x = Year, y = log(GDP))) +
  geom_line()

US GDP: Strong exponential growth. Log transform stabilizes growth and makes long-term trend clearer.

# 2. Victorian Bulls, bullocks and steers
aus_livestock %>%
  filter(Animal == "Bulls, bullocks and steers", State == "Victoria") %>%
  ggplot(aes(x = Month, y = Count)) +
  geom_line()

# 3. Victorian Electricity Demand
vic_elec %>%
  ggplot(aes(x = Time, y = Demand)) +
  geom_line()

# 4. Gas production
aus_production %>%
  ggplot(aes(x = Quarter, y = Gas)) +
  geom_line()

3.

canadian_gas %>%
  autoplot(Volume)

canadian_gas %>%
  features(Volume, features = guerrero)
## # A tibble: 1 × 1
##   lambda_guerrero
##             <dbl>
## 1           0.577
canadian_gas %>%
  mutate(BC_Volume = box_cox(Volume, lambda = 0.576)) %>%  # lambda ≈ 1
  autoplot(BC_Volume)

Both plots look nearly identical. Box-cox seems unnecessary.

4.

set.seed(12345678)

myseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`, 1))

# Pick lambda (Guerrero)
lambda <- myseries %>%
  features(Turnover, guerrero)

lambda
## # A tibble: 1 × 3
##   State              Industry                                    lambda_guerrero
##   <chr>              <chr>                                                 <dbl>
## 1 Northern Territory Clothing, footwear and personal accessory …          0.0830

I would use log transformation since lambda is 0.08.

5.

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

λ = 0.926: close to 1, so no transform.

ansett %>%
  filter(Class == "Economy", Airports == "MEL-SYD") %>%
  features(Passengers, guerrero)
## # A tibble: 1 × 3
##   Airports Class   lambda_guerrero
##   <chr>    <chr>             <dbl>
## 1 MEL-SYD  Economy            2.00

λ = 1.99, so use square type transform.

pedestrian %>%
  filter(Sensor == "Southern Cross Station") %>%
  features(Count, guerrero)
## # A tibble: 1 × 2
##   Sensor                 lambda_guerrero
##   <chr>                            <dbl>
## 1 Southern Cross Station          -0.250

λ = -0.25: negative lambda implies an inverse power transform.

7.

gas <- tail(aus_production, 5 * 4) |> 
  select(Gas)

autoplot(gas)
## Plot variable not specified, automatically selected `.vars = Gas`

There is clear quarterly seasonality trend-cycle over the five years.

#b
gas_decomp <- gas %>%
  model(
    classical_decomposition(Gas, type = "multiplicative")
  ) %>%
  components()

autoplot(gas_decomp)

#c. Support for a. The decomposition confirms the visual impression: there is clear quarterly seasonality and a smooth trend-cycle over the five-year period.

#d
gas_sa <- gas_decomp %>%
  as_tsibble() %>%
  mutate(Gas_SA = Gas / seasonal)

# plot seasonally adjusted series
gas_sa %>%
  autoplot(Gas_SA) +
  labs(
    title = "Australian Gas Production (Seasonally Adjusted, Last 5 Years)",
    y = "Seasonally Adjusted Gas Production"
  )

#e
gas_outlier <- gas %>%
  mutate(
    Gas = if_else(row_number() == 10, Gas + 300, Gas)
  )

gas_decomp_outlier <- gas_outlier %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components()

gas_sa_outlier <- gas_decomp_outlier %>%
  as_tsibble() %>%
  mutate(Gas_SA = Gas / seasonal)

gas_sa_outlier %>%
  autoplot(Gas_SA) +
  labs(
    title = "Seasonally Adjusted Gas Production with an Outlier",
    y = "Seasonally Adjusted Gas Production"
  )

The outlier causes an artificial spike around 2008 Q1 to about 450, while the rest of the seasonally adjusted series remains between roughly 150 and 250, showing that classical decomposition is highly sensitive to extreme values.

#f. Yes, an outlier near the end mostly affects the last few points, while an outlier in the middle affects many points before and after it because the trend is calculated using averages over time.

8.

library(seasonal)
## 
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
## 
##     view
set.seed(12345678)
myseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`, 1))

retail_x11 <- myseries %>%
  model(
    X_11 = X_13ARIMA_SEATS(Turnover ~ x11())
  ) %>%
  components()

autoplot(retail_x11)

Yes. The X-11 decomposition reveals a few sharp spikes and dips in the irregular component that were not obvious in the original retail series.

9.

a. The decomposition shows a strong upward trend, while the seasonal component is small and stable compared with the overall level. The irregular component contains a large negative spike of about −400, which stands out as an unusual short-term shock. The early 1990s recession is mainly visible as a slowdown in the trend-cycle rather than in the seasonal pattern.

b. Yes. The 1991–1992 recession is visible in the trend-cycle component as a clear slowdown and slight dip in growth, while the seasonal component remains unchanged and the irregular component captures short-term disturbances around that period.