install.packages("fpp3")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
## ✔ tibble 3.3.1 ✔ tsibble 1.2.0
## ✔ dplyr 1.2.1 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.2 ✔ ggtime 1.0.0
## ✔ lubridate 1.9.5 ✔ feasts 0.5.0
## ✔ ggplot2 4.0.3 ✔ fable 0.5.0
## ── 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()
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?
library(fpp3)
global_economy |>
mutate(GDP_per_capita = GDP / Population) |>
ggplot(aes(x = Year, y = GDP_per_capita, group = Country)) +
geom_line() +
labs(
title = "GDP Per Capita by Country",
x = "Year",
y = "GDP Per Capita"
)
## Warning: Removed 3242 rows containing missing values or values outside the scale range
## (`geom_line()`).
global_economy |>
mutate(GDP_per_capita = GDP / Population) |>
as_tibble() |>
arrange(desc(GDP_per_capita)) |>
select(Country, Year, GDP_per_capita)
GDP per capita is calculated by dividing GDP by population. Monaco has the highest GDP per capita in the data, reaching about 185,153 in 2014. GDP per capita generally increases over time, although countries grow at different rates and the country with the highest value changes over time.
For each of the following series, 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” from aus_livestock Victorian Electricity Demand from vic_elec Gas production from aus_production
global_economy |>
filter(Country == "United States") |>
autoplot(GDP)
global_economy |>
filter(Country == "United States") |>
autoplot(log(GDP))
The original GDP data show strong growth over time. Taking the log
reduces the effect of the large values and makes changes in GDP easier
to compare over time.
aus_livestock |>
filter(
State == "Victoria",
Animal == "Bulls, bullocks and steers"
) |>
autoplot(Count)
The series changes over time, but a transformation is not obviously
necessary from the graph.
vic_elec |>
autoplot(Demand)
Electricity demand has large short-term fluctuations, but the size of
the variation is reasonably stable over time. Therefore, a
transformation does not appear necessary.
aus_production |>
autoplot(Gas)
aus_production |>
autoplot(log(Gas))
Gas production shows increasing variation as production increases. The
log transformation reduces this changing variance and makes the seasonal
fluctuations more consistent.
Why is a Box-Cox transformation unhelpful for the canadian_gas data?
canadian_gas |>
autoplot(Volume)
canadian_gas |>
features(Volume, features = guerrero)
The Box-Cox lambda is 0.5767648, which suggests a transformation close to a square-root transformation. However, the transformation is not very helpful for the Canadian gas data because the seasonal pattern changes over time. A Box-Cox transformation can help stabilize the variance, but it does not fix the changing seasonal pattern.
What Box-Cox transformation would you select for your retail data from Exercise 7 in Section 2.10?
set.seed(12345678)
myseries <- aus_retail |>
filter(`Series ID` == sample(aus_retail$`Series ID`, 1))
myseries
myseries |>
autoplot(Turnover)
myseries |>
features(Turnover, features = guerrero)
myseries |>
autoplot(box_cox(Turnover, 0.08303631 ))
The Box-Cox lambda is 0.08303631. Since this value is close to 0, a transformation similar to a log transformation is appropriate. The transformation helps stabilize the variance and makes the seasonal fluctuations more consistent over time.
Find an appropriate Box-Cox transformation to stabilize the variance for:
Tobacco from aus_production Economy class passengers between Melbourne and Sydney from ansett Pedestrian counts at Southern Cross Station from pedestrian
###Tobacco
aus_production |>
features(Tobacco, features = guerrero)
aus_production |>
autoplot(box_cox(Tobacco, 0.9264636))
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
ansett |>
filter(
Airports == "MEL-SYD",
Class == "Economy"
) |>
features(Passengers, features = guerrero)
ansett |>
filter(
Airports == "MEL-SYD",
Class == "Economy"
) |>
autoplot(box_cox(Passengers, 1.999927))
pedestrian |>
filter(Sensor == "Southern Cross Station") |>
features(Count, features = guerrero)
pedestrian |>
filter(Sensor == "Southern Cross Station") |>
autoplot(box_cox(Count, -0.2501616 ))
For Tobacco, the lambda value is 0.9264636. Since this is close to 1, very little transformation is needed.
For Melbourne-Sydney Economy passengers, the lambda value is 1.999927. Since this is close to 2, a transformation similar to squaring the data would be appropriate.
For Southern Cross Station pedestrian counts, the lambda value is -0.2501616. This suggests using a negative power transformation to help stabilize the variance.
Consider the last five years of Gas data from aus_production. The exercise asks you to plot the series, perform a multiplicative classical decomposition, interpret it, plot seasonally adjusted data, and examine what happens when an outlier is introduced.
gas <- tail(aus_production, 5*4) |>
select(Gas)
gas |>
autoplot(Gas)
There is a clear seasonal pattern in gas production. Production also changes over time, showing a trend-cycle.
gas |>
model(
classical_decomposition(
Gas,
type = "multiplicative"
)
) |>
components() |>
autoplot()
## Warning: Removed 8 rows containing missing values or values outside the scale range
## (`geom_line()`).
The decomposition shows that gas production has a clear seasonal pattern, with similar increases and decreases occurring each year. The trend generally increases over time, showing that gas production is growing. The random component stays close to 1 for most of the series, meaning that most of the variation is explained by the trend and seasonal components.
Overall, the decomposition supports what we see in the original graph: gas production has both an upward trend and strong seasonality.
gas |>
model(
classical_decomposition(
Gas,
type = "multiplicative"
)
) |>
components() |>
autoplot(season_adjust)
Removing seasonality makes the underlying movement of gas production
easier to see.
gas_outlier <- gas
gas_outlier$Gas[10] <- gas_outlier$Gas[10] + 300
gas_outlier |>
model(
classical_decomposition(
Gas,
type = "multiplicative"
)
) |>
components() |>
autoplot(season_adjust)
The outlier creates a large spike in the seasonally adjusted series and also affects the estimated trend. This shows that classical decomposition can be sensitive to extreme observations.
gas_outlier <- gas
gas_outlier$Gas[19] <- gas_outlier$Gas[19] + 300
gas_outlier |>
model(
classical_decomposition(
Gas,
type = "multiplicative"
)
) |>
components() |>
autoplot()
## Warning: Removed 8 rows containing missing values or values outside the scale range
## (`geom_line()`).
Yes. Moving the outlier near the end changes its effect on the
decomposition. The large spike causes the estimated trend to increase
sharply near the end, showing that both the size and location of an
outlier can affect classical decomposition.
Recall your retail time series from Exercise 7 in Section 2.10. Decompose the series using X-11. Does it reveal any outliers or unusual features you had not noticed previously?
install.packages("seasonal")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
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))
myseries
myseries |>
autoplot(Turnover)
fit <- myseries |>
model(
x11 = X_13ARIMA_SEATS(Turnover ~ x11())
)
fit |>
components() |>
autoplot()
The X-11 decomposition shows an overall upward trend and a strong repeating seasonal pattern. The irregular component shows several noticeable spikes and drops, especially in the earlier years of the series. These unusual movements are easier to see after the decomposition and may represent outliers that were not as obvious in the original graph.
Figures 3.19 and 3.20 show a decomposition of the number of people in Australia’s civilian labour force from February 1978 through August 1995. You are asked to describe the decomposition in about 3–5 sentences and determine whether the 1991/1992 recession is visible in the estimated components.
The labour force shows a strong upward trend over the entire period. The seasonal pattern is relatively small when compared with the scale of the overall labour force. The remainder shows short-term fluctuations around the main pattern. Around 1991–1992, the trend becomes flatter, which is consistent with the recession during this period. The recession is most noticeable in the trend-cycle rather than the seasonal component.