install.packages("tidyverse")
install.packages("openintro")
library(tidyverse)
library(openintro)
library(fpp3)
Plot GDP per Capita
global_economy %>%
ggplot(aes(x = Year, y = GDP / Population, color = Country)) +
geom_line() +
labs(title = "GDP Per Capita Over Time", x = "Year", y = "GDP Per Capita")
Find the Country with the Highest GDP Per Capita
global_economy %>%
mutate(GDP_per_capita = GDP / Population) %>%
filter(GDP_per_capita == max(GDP_per_capita, na.rm = TRUE))
## # A tsibble: 1 x 10 [1Y]
## # Key: Country [1]
## Country Code Year GDP Growth CPI Imports Exports Population
## <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Monaco MCO 2014 7060236168. 7.18 NA NA NA 38132
## # ℹ 1 more variable: GDP_per_capita <dbl>
#Analysis for 3.1:
The country with the highest GDP per capita has varied over time.
Small but wealthy nations, such as Luxembourg, often dominate due to high income and low population.
Exercise 3.2
Graph and Transform Data Where Needed
global_economy %>%
filter(Country == "United States") %>%
autoplot(GDP) +
labs(title = "United States GDP Over Time")
The GDP exhibits an increasing trend, with periods of economic slowdown
visible.
aus_livestock %>%
filter(Animal == "Bulls, bullocks and steers", State == "Victoria") %>%
autoplot(Count) +
labs(title = "Slaughter of Bulls, Bullocks, and Steers in Victoria")
Seasonal fluctuations may exist due to farming cycles and demand
patterns.
autoplot(vic_elec, Demand) +
labs(title = "Electricity Demand in Victoria")
Electricity demand follows a strong seasonal pattern, with peaks in
colder months.
autoplot(aus_production, Gas) +
labs(title = "Gas Production Over Time")
Increasing trend with fluctuations that might require
transformation.
Exercise 3.3 Why is a Box-Cox Transformation Unhelpful for canadian_gas?
colnames(canadian_gas)
## [1] "Month" "Volume"
canadian_gas %>%
autoplot(Volume) +
labs(title = "Canadian Gas Production Over Time", x = "Month", y = "Gas Volume")
Explanation:
The data follows a clear seasonal pattern rather than just a variance issue. Box-Cox transformation primarily helps stabilize variance, but in this case, a seasonal adjustment or differencing is needed instead.
Exercise 3.4
Select an Appropriate Box-Cox Transformation for Retail Data
lambda <- aus_retail %>%
features(Turnover, features = guerrero) %>%
pull(lambda_guerrero)
Explanation:
The Box-Cox transformation parameter (lambda) suggests the best transformation for stabilizing variance.
The value of lambda will indicate whether a log transformation (lambda = 0) or another power transformation is appropriate.
Exercise 3.5
Find an Appropriate Box-Cox Transformation for Given Time Series
lambda_tobacco <- aus_production %>%
features(Tobacco, features = guerrero) %>%
pull(lambda_guerrero)
colnames(ansett)
## [1] "Week" "Airports" "Class" "Passengers"
lambda_passengers <- ansett %>%
filter(Class == "Economy", Airports == "MEL-SYD") %>%
features(Passengers, features = guerrero) %>%
pull(lambda_guerrero)
lambda_pedestrian <- pedestrian %>%
filter(Sensor == "Southern Cross Station") %>%
features(Count, features = guerrero) %>%
pull(lambda_guerrero)
Analysis: The computed lambda values indicate the best transformation for each dataset.
If lambda ≈ 0, a log transformation is suggested.
If lambda ≈ 1, no transformation is needed.
The transformed data should exhibit a more stable variance, improving forecasting accuracy.
Exercise 3.7: Gas Data Analysis (a) Plot the Time Series
gas <- tail(aus_production, 5*4) |> select(Gas)
autoplot(gas) +
labs(title = "Gas Production (Last 5 Years)", x = "Year", y = "Gas Production")
The plot will help visualize any seasonal fluctuations and trend-cycle.
Expected observation: Gas production generally exhibits an increasing
trend with seasonal fluctuations.
gas_decomp <- gas |> model(classical_decomposition(Gas, type = "multiplicative"))
components <- components(gas_decomp)
autoplot(components) +
labs(title = "Multiplicative Decomposition of Gas Production")
This decomposes the series into trend-cycle, seasonal, and remainder
components. Trend-cycle shows long-term movements, while seasonal
indices highlight recurring fluctuations.
Does the decomposition support the graphical interpretation? If the trend-cycle shows a steady increase, it confirms the long-term growth observed in (a). If the seasonal component has a regular pattern, it confirms consistent seasonality in gas production.
Compute and Plot Seasonally Adjusted Data
gas_sa <- components |> mutate(Seasonally_Adjusted = Gas / seasonal)
autoplot(gas_sa, Seasonally_Adjusted) +
labs(title = "Seasonally Adjusted Gas Production")
Seasonally adjusted series removes periodic fluctuations, revealing
clearer trends.
gas_outlier <- gas
gas_outlier$Gas[10] <- gas_outlier$Gas[10] + 300 # Introduce an outlier
gas_outlier_decomp <- gas_outlier |> model(classical_decomposition(Gas, type = "multiplicative"))
components_outlier <- components(gas_outlier_decomp)
autoplot(components_outlier) +
labs(title = "Decomposition with Outlier")
The outlier affects the remainder component, leading to irregular
seasonal indices. Trend-cycle may shift, impacting predictions.
Exercise 3.8: X-11 Decomposition on Retail Data
85.85/3853./
library(seasonal)
aus_retail |> count()
## # A tibble: 1 × 1
## n
## <int>
## 1 64532
set.seed(123)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`, 1))
myseries %>%
autoplot(Turnover)
myseries_x11 <- myseries %>%
model(x11 = feasts:::X11(Turnover, type = "multiplicative")) %>%
components()
I noticed some unexpected outliers in the data, especially the biggest one in 2001. I was also surprised to see that the seasonality effect decreased as the trend increased.
Exercise 3.9: Australian Civilian Labour Force Decomposition (Based on Figures 3.19 and 3.20)
The trendg-cycle component shows a steady increase in civilian labor force participation over time. The seasonal component fluctuates within a narrow range, indicating regular seasonal employment variations. The remainder component captures deviations, with noticeable fluctuations around 1991/1992, which likely reflects the economic recession. The recession of 1991/1992 is visible as a dip in the trend-cycle, showing a slowdown in labor force growth.
9.b Is the recession of 1991/1992 visible in the estimated components?
Answer) Yes, you can definitely see the impact of the 1991/1992 recession in the estimated components. During that time, there were noticeable drops in key economic indicators like GDP growth and industrial production. Unemployment rates also spiked, reflecting the tough economic conditions. These changes are clearly captured in the data from that period, showing how the recession affected various aspects of the economy.