Use ? (or help()) to find out about the data in each series. What is the time interval of each series? Use autoplot() to produce a time plot of each series. For the last plot, modify the axis labels and title.
library(fpp3)
## Warning: package 'fpp3' was built under R version 4.5.2
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.2 ──
## ✔ tibble 3.3.0 ✔ tsibble 1.1.6
## ✔ dplyr 1.1.4 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.1 ✔ feasts 0.4.2
## ✔ lubridate 1.9.4 ✔ fable 0.5.0
## ✔ ggplot2 4.0.1
## Warning: package 'ggplot2' was built under R version 4.5.2
## Warning: package 'tsibble' was built under R version 4.5.2
## Warning: package 'tsibbledata' was built under R version 4.5.2
## Warning: package 'feasts' was built under R version 4.5.2
## Warning: package 'fabletools' was built under R version 4.5.2
## Warning: package 'fable' was built under R version 4.5.2
## ── 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()
?aus_production
## starting httpd help server ...
## done
?pelt
?gafa_stock
?vic_elec
# 1. Australian brick production
aus_production %>%
autoplot(Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
# 2. Lynx pelts traded
pelt %>%
autoplot(Lynx)
# 3. GAFA stock closing prices
gafa_stock %>%
autoplot(Close)
### Modified Plot for Electricity Demand
For the vic_elec dataset, we use labs() to customize the title and axis labels as requested.
vic_elec %>%
autoplot(Demand) +
labs(
title = "Half-hourly Electricity Demand",
subtitle = "Victoria, Australia",
x = "Year",
y = "Demand (Megawatts)")
# Find the peak closing price for each stock
gafa_stock %>%
group_by(Symbol) %>%
filter(Close == max(Close)) %>%
select(Symbol, Date, Close)
## # A tsibble: 4 x 3 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Close
## <chr> <date> <dbl>
## 1 AAPL 2018-10-03 232.
## 2 AMZN 2018-09-04 2040.
## 3 FB 2018-07-25 218.
## 4 GOOG 2018-07-26 1268.
All four stocks reached their peak prices in 2018 between July and October. Amazon had the highest value at 2039.51, while Facebook and Google peaked on consecutive days in July. This shows that the tech sector hit a collective high point during the third quarter of that year.
You can read the data into R with the following script:
tute1 <- readr::read_csv("tute1.csv")
## Rows: 100 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): Sales, AdBudget, GDP
## date (1): Quarter
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(tute1)
Convert the data to time series
mytimeseries <- tute1 |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter)
View(mytimeseries)
Construct time series plots of each of the three series
mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")
Check what happens when you don’t include facet_grid().
Without facet_grid(), all three variables are plotted on a single set of axes. This makes the plot difficult to read because the series have very different scales; for example, a large series like GDP will make smaller series like AdBudget look like flat lines. Faceting is necessary to compare their individual trends and seasonal patterns clearly.
Install the USgas package. Create a tsibble from us_total with year as the index and state as the key. Plot the annual natural gas consumption by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).
# Install and load the package
library(USgas)
## Warning: package 'USgas' was built under R version 4.5.2
library(fpp3)
# 1. Create the tsibble
us_tsibble <- us_total %>%
as_tsibble(index = year, key = state)
View(us_tsibble)
# 2. Filter for New England states and plot
us_tsibble %>%
filter(state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")) %>%
autoplot(y) +
labs(
title = "Annual Natural Gas Consumption - New England",
y = "Billion Cubic Feet",
x = "Year")
The plot shows that natural gas consumption in New England varies significantly by state. Massachusetts has the highest usage by far, followed by a steady increase in Connecticut over the last two decades. In contrast, Maine, New Hampshire, Rhode Island, and Vermont show much lower and relatively stable consumption levels throughout the period.
Create a tsibble which is identical to the tourism tsibble from the tsibble package. Find what combination of Region and Purpose had the maximum number of overnight trips on average. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
library(readxl)
# Read the file
tourism_data <- readxl::read_excel("tourism.xlsx")
# Convert to tsibble
tourism_ts <- tourism_data %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(index = Quarter, key = c(Region, State, Purpose))
View(tourism_ts)
tourism_ts %>%
as_tibble() %>%
group_by(Region, Purpose) %>%
summarise(Average_Trips = mean(Trips), .groups = "drop") %>%
filter(Average_Trips == max(Average_Trips))
## # A tibble: 1 × 3
## Region Purpose Average_Trips
## <chr> <chr> <dbl>
## 1 Sydney Visiting 747.
To get the total trips by State, we must sum the values across all Regions and Purposes for each time period.
state_tourism <- tourism_ts %>%
group_by(State) %>%
summarise(Total_Trips = sum(Trips))
View(state_tourism)
Can you spot any seasonality, cyclicity and trend? What do you learn about the series? What can you say about the seasonal patterns? Can you identify any unusual years?
“Total Private” Employed (us_employment) This series tracks the number of private-sector employees in the US.
# Filter for Total Private and plot
total_private <- us_employment %>%
filter(Title == "Total Private")
total_private %>% autoplot(Employed)
total_private %>% gg_season(Employed)
## 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.
total_private %>% gg_subseries(Employed)
## 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.
total_private %>% ACF(Employed) %>% autoplot()
Analysis:
-Trend: Strong and consistent upward trend from 1940 to 2020.
-Seasonality: Clear monthly seasonality; the subseries plot shows higher average employment toward the second half of the year.
-Unusual Years: Significant sharp drops are visible during the 2001 and 2008 recessions.
-ACF: Very slow decay with high values (above 0.9), confirming a dominant trend and high persistence in the data.
This series tracks quarterly clay brick production in Australia.
# Bricks production
aus_production %>%
filter(!is.na(Bricks)) %>%
autoplot(Bricks)
aus_production %>% gg_season(Bricks, labels = "both")
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_text()`).
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_text()`).
aus_production %>% gg_subseries(Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
aus_production %>% ACF(Bricks) %>% autoplot()
Analysis:
-Trend: Increased sharply until the mid-1970s, remained high through the 80s, then declined and turned volatile.
-Seasonality: Strong quarterly pattern; the seasonal plot shows production is consistently lowest in Q1 and usually peaks in Q2 or Q3.
-Unusual Years: A deep, singular plunge is visible around 1982-1983 and another in the mid-70s.
-ACF: Shows a “scalloped” or wavy pattern with local peaks at lags 4, 8, and 12, confirming strong quarterly seasonality.
This series tracks the number of snowshoe hare pelts traded by the Hudson’s Bay Company.
# Hare population
pelt %>% autoplot(Hare)
pelt %>% ACF(Hare) %>% autoplot()
Analysis
-Trend: No clear long-term upward or downward trend.
-Cyclicity: Highly cyclical behavior with population peaks roughly every 9-10 years.
-Unusual Years: Extreme population spikes occurred around 1865 and 1885, followed by rapid crashes.
-ACF: The plot shows a sinusoidal pattern, which is a classic indicator of cyclic data where values are positively correlated with recent years but negatively correlated with periods half-cycle away.
# Filter for H02 and sum trips across all other keys
h02_cost <- PBS %>%
filter(ATC2 == "H02") %>%
summarise(Total_Cost = sum(Cost))
h02_cost %>% autoplot(Total_Cost)
h02_cost %>% gg_season(Total_Cost)
h02_cost %>% gg_subseries(Total_Cost)
h02_cost %>% ACF(Total_Cost) %>% autoplot()
Analysis:
-Trend: A steady upward trend in costs over time.
-Seasonality: Extremely strong monthly seasonality. There is a massive spike every January followed by a plunge in February.
-Unusual Years: The pattern is remarkably consistent year over year.
-ACF: Large spikes at lag 12, 24, etc., confirm the strong annual seasonal pattern.
This tracks weekly finished motor gasoline products supplied in the US.
# Gasoline supply
us_gasoline %>% autoplot(Barrels)
us_gasoline %>% gg_season(Barrels)
us_gasoline %>% ACF(Barrels) %>% autoplot()
Analysis:
-Trend: A general upward trend until approximately 2007, then it becomes more volatile.
-Seasonality: Weekly seasonality is present but “noisy” and harder to see than in the monthly series.
-Unusual Years: Significant volatility is visible around the 2008 financial crisis.
-ACF: Shows very slow decay, indicating a highly persistent trend and high autocorrelation.