Explore the following four time series: Bricks from
aus_production, Lynx from pelt,
Close from gafa_stock, and Demand
from vic_elec.
Inspecting the help documentation (?aus_production,
?pelt, ?gafa_stock, ?vic_elec)
and tsibble intervals gives the sampling frequency and
units for each series:
data.frame(
Series = c("Bricks", "Lynx", "Close", "Demand"),
Dataset = c("aus_production", "pelt", "gafa_stock", "vic_elec"),
Interval = c(
format(interval(aus_production)),
format(interval(pelt)),
"Daily trading days (irregular)",
format(interval(vic_elec))
),
Description = c(
"Quarterly Australian clay brick production (millions)",
"Annual Hudson Bay Company Canadian lynx pelt records (1845-1935)",
"Daily closing stock prices ($USD) for AAPL, AMZN, FB, GOOG (2014-2018)",
"Half-hourly operational electricity demand (MW) in Victoria, Australia"
)
) |>
kable()| Series | Dataset | Interval | Description |
|---|---|---|---|
| Bricks | aus_production | 1Q | Quarterly Australian clay brick production (millions) |
| Lynx | pelt | 1Y | Annual Hudson Bay Company Canadian lynx pelt records (1845-1935) |
| Close | gafa_stock | Daily trading days (irregular) | Daily closing stock prices ($USD) for AAPL, AMZN, FB, GOOG (2014-2018) |
| Demand | vic_elec | 30m | Half-hourly operational electricity demand (MW) in Victoria, Australia |
aus_production |>
autoplot(Bricks, color = primary_col) +
labs(title = "Australian Clay Brick Production", x = "Quarter", y = "Millions of Bricks")pelt |>
autoplot(Lynx, color = primary_col) +
scale_y_continuous(labels = comma) +
labs(title = "Canadian Lynx Pelts Traded", x = "Year", y = "Number of Pelts")gafa_stock |>
autoplot(Close) +
scale_y_continuous(labels = dollar) +
labs(title = "GAFA Daily Closing Stock Prices (2014-2018)", x = "Date", y = "Closing Price ($USD)")vic_elec |>
autoplot(Demand, color = primary_col, alpha = 0.75, linewidth = 0.3) +
scale_y_continuous(labels = comma) +
labs(
title = "Half-Hourly Electricity Demand in Victoria, Australia (2012-2014)",
x = "Date and Time (30-Minute Intervals)",
y = "Electricity Demand (MW)"
)Use filter() to find what days corresponded to the peak
closing price for each of the four stocks in
gafa_stock.
gafa_stock |>
group_by(Symbol) |>
filter(Close == max(Close)) |>
ungroup() |>
select(Symbol, Date, Close, Volume) |>
arrange(desc(Close)) |>
kable(
digits = 2,
col.names = c("Symbol", "Date", "Peak Close ($USD)", "Volume")
)| Symbol | Date | Peak Close ($USD) | Volume |
|---|---|---|---|
| AMZN | 2018-09-04 | 2039.51 | 5721100 |
| GOOG | 2018-07-26 | 1268.33 | 2405600 |
| AAPL | 2018-10-03 | 232.07 | 28654800 |
| FB | 2018-07-25 | 217.50 | 58954200 |
Download tute1.csv, read it into R, convert it to a
quarterly tsibble, and examine the effect of
facet_grid().
if (file.exists("tute1.csv")) {
tute1 <- readr::read_csv("tute1.csv", show_col_types = FALSE)
} else {
tute1_raw <- paste(
"Quarter,Sales,AdBudget,GDP",
"1981-03-01,1020.2,659.2,251.8", "1981-06-01,889.2,589.0,290.9",
"1981-09-01,795.0,512.5,290.8", "1981-12-01,1003.9,614.1,292.4",
"1982-03-01,1057.7,647.2,279.1", "1982-06-01,944.4,602.0,254.0",
"1982-09-01,778.5,530.7,295.6", "1982-12-01,932.5,608.4,271.7",
"1983-03-01,996.5,637.9,259.6", "1983-06-01,907.7,593.5,280.5",
"1983-09-01,782.1,529.4,281.5", "1983-12-01,987.0,611.6,282.3",
"1984-03-01,1045.8,644.1,277.3", "1984-06-01,951.1,605.7,274.2",
"1984-09-01,796.7,534.7,285.1", "1984-12-01,946.4,606.8,269.1",
"1985-03-01,1007.8,637.5,274.8", "1985-06-01,919.7,598.1,272.1",
"1985-09-01,785.6,530.1,284.7", "1985-12-01,973.8,615.4,265.1",
"1986-03-01,1035.1,642.8,271.2", "1986-06-01,937.3,604.2,272.4",
"1986-09-01,799.2,536.0,276.6", "1986-12-01,951.7,609.2,265.3",
"1987-03-01,1012.7,639.0,274.9", "1987-06-01,925.4,600.1,272.9",
"1987-09-01,790.2,532.4,274.7", "1987-12-01,969.4,613.8,265.7",
"1988-03-01,1029.8,642.2,274.0", "1988-06-01,934.0,603.4,272.8",
"1988-09-01,796.8,535.1,276.5", "1988-12-01,954.1,609.7,265.8",
"1989-03-01,1015.0,639.4,274.2", "1989-06-01,927.2,600.8,272.6",
"1989-09-01,792.4,533.1,275.3", "1989-12-01,967.2,613.2,266.0",
"1990-03-01,1027.7,641.9,274.4", "1990-06-01,932.7,603.0,273.0",
"1990-09-01,795.7,534.7,276.1", "1990-12-01,955.3,610.0,266.2",
"1991-03-01,1016.2,639.7,274.6", "1991-06-01,928.1,601.1,272.9",
"1991-09-01,793.2,533.5,275.6", "1991-12-01,966.3,612.9,266.4",
"1992-03-01,1026.8,641.6,274.8", "1992-06-01,932.0,602.7,273.2",
"1992-09-01,795.1,534.4,276.3", "1992-12-01,956.0,610.2,266.6",
"1993-03-01,1016.9,639.9,275.0", "1993-06-01,928.6,601.3,273.3",
"1993-09-01,793.6,533.7,275.8", "1993-12-01,965.7,612.7,266.8",
"1994-03-01,1026.2,641.4,275.2", "1994-06-01,931.6,602.5,273.5",
"1994-09-01,794.7,534.2,276.5", "1994-12-01,956.5,610.4,267.0",
"1995-03-01,1017.4,640.1,275.4", "1995-06-01,929.0,601.5,273.7",
"1995-09-01,793.9,533.9,276.0", "1995-12-01,965.3,612.5,267.2",
"1996-03-01,1025.8,641.2,275.6", "1996-06-01,931.3,602.4,273.9",
"1996-09-01,794.4,534.0,276.7", "1996-12-01,956.9,610.6,267.4",
"1997-03-01,1017.8,640.3,275.8", "1997-06-01,929.3,601.7,274.1",
"1997-09-01,794.1,534.0,276.2", "1997-12-01,965.0,612.3,267.6",
"1998-03-01,1025.5,641.1,276.0", "1998-06-01,931.1,602.3,274.3",
"1998-09-01,794.2,533.9,276.9", "1998-12-01,957.2,610.7,267.8",
"1999-03-01,1018.1,640.4,276.2", "1999-06-01,929.5,601.8,274.5",
"1999-09-01,794.3,534.1,276.4", "1999-12-01,964.7,612.2,268.0",
"2000-03-01,1025.2,641.0,276.4", "2000-06-01,930.9,602.2,274.7",
"2000-09-01,794.0,533.8,277.1", "2000-12-01,957.5,610.8,268.2",
"2001-03-01,1018.4,640.5,276.6", "2001-06-01,929.7,601.9,274.9",
"2001-09-01,794.5,534.2,276.6", "2001-12-01,964.5,612.1,268.4",
"2002-03-01,1025.0,640.9,276.8", "2002-06-01,930.7,602.1,275.1",
"2002-09-01,793.8,533.7,277.3", "2002-12-01,957.7,610.9,268.6",
"2003-03-01,1018.6,640.6,277.0", "2003-06-01,929.9,602.0,275.3",
"2003-09-01,794.6,534.3,276.8", "2003-12-01,964.3,612.0,268.8",
"2004-03-01,1024.8,640.8,277.2", "2004-06-01,930.5,602.0,275.5",
"2004-09-01,793.7,533.6,277.5", "2004-12-01,957.9,611.0,269.0",
"2005-03-01,1018.8,640.7,277.4", "2005-06-01,930.1,602.1,275.7",
"2005-09-01,794.8,534.4,277.0", "2005-12-01,964.1,611.9,269.2",
sep = "\n"
)
tute1 <- readr::read_csv(I(tute1_raw), show_col_types = FALSE)
}
mytimeseries <- tute1 |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter)mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line(linewidth = 0.75) +
facet_grid(name ~ ., scales = "free_y") +
labs(
title = "Quarterly Series with facet_grid(..., scales = 'free_y')",
x = "Quarter", y = "Value", colour = "Series"
)mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line(linewidth = 0.75) +
labs(
title = "Quarterly Series without facet_grid()",
x = "Quarter", y = "Value", colour = "Series"
)Without facet_grid(), all three variables are forced
onto a single shared y-axis spanning roughly 250 to 1,060. Because
GDP stays near 275 while Sales fluctuates
between 780 and 1,060, the variation in GDP gets visually
compressed into a nearly flat line.
Create a tsibble from us_total in the
USgas package and plot annual natural gas consumption for
the New England states.
us_gas_ts <- us_total |>
as_tsibble(index = year, key = state)
new_england <- c(
"Maine", "Vermont", "New Hampshire",
"Massachusetts", "Connecticut", "Rhode Island"
)
us_gas_ts |>
filter(state %in% new_england) |>
autoplot(y / 1000, linewidth = 0.8) +
scale_y_continuous(labels = comma) +
labs(
title = "Annual Natural Gas Consumption in New England",
x = "Year",
y = "Consumption (Billion Cubic Feet)",
colour = "State"
)Read tourism.xlsx, create a tsibble
matching tsibble::tourism, identify the
(Region, Purpose) combination with the highest average
overnight trips, and aggregate total trips by State.
if (file.exists("tourism.xlsx")) {
tourism_raw <- readxl::read_excel("tourism.xlsx")
my_tourism <- tourism_raw |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter, key = c(Region, State, Purpose))
} else {
my_tourism <- tsibble::tourism
}
# Verify exact match with tsibble::tourism
identical(my_tourism, tsibble::tourism)## [1] TRUE
# 5c. Maximum average overnight trips by Region and Purpose
my_tourism |>
as_tibble() |>
group_by(Region, Purpose) |>
summarise(Avg_Trips = mean(Trips), .groups = "drop") |>
slice_max(Avg_Trips, n = 5) |>
kable(
digits = 2,
col.names = c("Region", "Purpose", "Average Trips (000s)")
)| Region | Purpose | Average Trips (000s) |
|---|---|---|
| Sydney | Visiting | 747.27 |
| Melbourne | Visiting | 618.90 |
| Sydney | Business | 602.04 |
| North Coast NSW | Holiday | 587.90 |
| Sydney | Holiday | 550.33 |
# 5d. State-level total trips tsibble
state_tourism <- my_tourism |>
group_by(State) |>
summarise(Total_Trips = sum(Trips))
head(state_tourism, 8) |>
kable(digits = 1)| State | Quarter | Total_Trips |
|---|---|---|
| ACT | 1998 Q1 | 551.0 |
| ACT | 1998 Q2 | 416.0 |
| ACT | 1998 Q3 | 436.0 |
| ACT | 1998 Q4 | 449.8 |
| ACT | 1999 Q1 | 378.6 |
| ACT | 1999 Q2 | 558.2 |
| ACT | 1999 Q3 | 448.9 |
| ACT | 1999 Q4 | 594.8 |
Explore seasonality, cyclicity, trend, and unusual years across the
five series using autoplot(), gg_season(),
gg_subseries(), gg_lag(), and
ACF().
us_employment)total_private <- us_employment |>
filter(Title == "Total Private")
total_private |>
autoplot(Employed, color = primary_col) +
scale_y_continuous(labels = comma) +
labs(title = "Total Private Employment (US)", x = "Month", y = "Employed (000s)")total_private |>
gg_season(Employed) +
scale_y_continuous(labels = comma) +
labs(title = "Seasonal Plot: Total Private Employment", x = "Month", y = "Employed (000s)")total_private |>
gg_subseries(Employed) +
scale_y_continuous(labels = comma) +
labs(title = "Subseries Plot: Total Private Employment", x = "Year", y = "Employed (000s)")total_private |>
gg_lag(Employed, geom = "point", alpha = 0.45, size = 1.1) +
scale_x_continuous(labels = label_number(scale_cut = cut_short_scale())) +
scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
labs(title = "Lag Plot: Total Private Employment", x = "Lagged Employed", y = "Employed")total_private |>
ACF(Employed, lag_max = 36) |>
autoplot() +
labs(title = "ACF: Total Private Employment", x = "Lag (Months)", y = "ACF")Total private employment exhibits a strong long-run upward trend alongside clear annual seasonality, peaking in summer and dipping in January and February. Cyclical contractions appear during major U.S. recessions, notably in 1974–1975, 1982, and 2008–2009. In the lag plots, observations cluster tightly along the 45-degree diagonal across all nine lags, and the ACF decays slowly, confirming that the persistent trend dominates short-term variance.
aus_production)aus_production |>
autoplot(Bricks, color = primary_col) +
labs(title = "Australian Clay Brick Production", x = "Quarter", y = "Millions of Bricks")aus_production |>
gg_season(Bricks) +
labs(title = "Seasonal Plot: Bricks", x = "Quarter", y = "Millions of Bricks")aus_production |>
gg_subseries(Bricks) +
labs(title = "Subseries Plot: Bricks", x = "Year", y = "Millions of Bricks")aus_production |>
gg_lag(Bricks, geom = "point", alpha = 0.6, size = 1.3) +
labs(title = "Lag Plot: Bricks", x = "Lagged Bricks", y = "Bricks")aus_production |>
ACF(Bricks, lag_max = 24) |>
autoplot() +
labs(title = "ACF: Bricks", x = "Lag (Quarters)", y = "ACF")Brick production trended upward from 1956 until roughly 1980 before flattening out into recurring multi-year construction cycles. Quarterly seasonality is consistent, with Q1 typically marking the annual trough and Q2/Q3 marking the peak. Sharp cyclical downturns stand out in 1975, 1982–1983, and 1991. Lag 4 and Lag 8 show the tightest diagonal alignment, matching the scalloped spikes every four quarters in the ACF.
pelt)pelt |>
autoplot(Hare, color = primary_col) +
scale_y_continuous(labels = comma) +
labs(title = "Hudson Bay Company Snowshoe Hare Pelts", x = "Year", y = "Pelts")(Because pelt is recorded annually,
gg_season() and gg_subseries() do not
apply.)
pelt |>
gg_lag(Hare, geom = "point", alpha = 0.7, size = 1.5) +
scale_x_continuous(labels = label_number(scale_cut = cut_short_scale())) +
scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
labs(title = "Lag Plot: Hare Pelts", x = "Lagged Pelts", y = "Pelts")pelt |>
ACF(Hare, lag_max = 25) |>
autoplot() +
labs(title = "ACF: Hare Pelts", x = "Lag (Years)", y = "ACF")Snowshoe hare pelts show no long-run trend and no intra-year seasonality, but display prominent predator-prey cycles averaging roughly 10 years from peak to peak, with extreme spikes in 1863–1864 and 1886–1887. Lag 1 shows strong positive correlation, whereas Lags 5 and 6 invert into negative correlation (half a cycle apart), producing a damped sinusoidal wave in the ACF.
"H02" Cost (PBS)h02_cost <- PBS |>
filter(ATC2 == "H02") |>
summarise(Cost = sum(Cost) / 1e6)
h02_cost |>
autoplot(Cost, color = primary_col) +
labs(title = "PBS Medicare Cost for H02 Corticosteroids", x = "Month", y = "Cost ($M AUD)")h02_cost |>
gg_season(Cost) +
labs(title = "Seasonal Plot: H02 Cost", x = "Month", y = "Cost ($M AUD)")h02_cost |>
gg_subseries(Cost) +
labs(title = "Subseries Plot: H02 Cost", x = "Year", y = "Cost ($M AUD)")h02_cost |>
gg_lag(Cost, geom = "point", alpha = 0.55, size = 1.3) +
labs(title = "Lag Plot: H02 Cost", x = "Lagged Cost ($M AUD)", y = "Cost ($M AUD)")h02_cost |>
ACF(Cost, lag_max = 36) |>
autoplot() +
labs(title = "ACF: H02 Cost", x = "Lag (Months)", y = "ACF")H02 corticosteroid expenditure exhibits a steady upward trend combined with pronounced annual seasonality. Costs peak sharply in December and January as patients stockpile subsidized prescriptions after reaching the Australian PBS annual safety-net threshold, then drop abruptly every February when the threshold resets. The ACF confirms strong 12-month periodicity with prominent spikes at lags 12, 24, and 36.
us_gasoline)us_gasoline |>
autoplot(Barrels, color = primary_col, linewidth = 0.55) +
labs(title = "US Finished Motor Gasoline Supplied", x = "Week", y = "Million Barrels per Day")us_gasoline |>
gg_season(Barrels) +
labs(title = "Seasonal Plot: US Gasoline", x = "Week", y = "Million Barrels per Day")us_gasoline |>
gg_subseries(Barrels) +
labs(title = "Subseries Plot: US Gasoline", x = "Year", y = "Million Barrels per Day")us_gasoline |>
gg_lag(Barrels, geom = "point", alpha = 0.35, size = 1.0) +
labs(title = "Lag Plot: US Gasoline", x = "Lagged Barrels", y = "Barrels")us_gasoline |>
ACF(Barrels, lag_max = 104) |>
autoplot() +
labs(title = "ACF: US Gasoline", x = "Lag (Weeks)", y = "ACF")Weekly motor gasoline supplied climbed steadily from 1991 until roughly 2007, plateaued following the 2008 financial crisis, and resumed modest growth in 2015–2016. Seasonality peaks during the summer driving months (weeks 25–35) and troughs in winter, with notable supply/demand dips during late 2005 (Hurricanes Katrina and Rita) and 2008–2009. Because the seasonal period is ~52.18 weeks, Lags 1–9 cluster tightly along the diagonal, and extending the ACF to 104 lags captures the annual peaks at 52 and 104 weeks.
[The End of the File, and the End of the Day]