library(fpp3)
library(dplyr)
library(tsibble)
library(lubridate)
a10 <- PBS |>
filter(ATC2 == "A10") |>
summarise(Cost = sum(Cost)/1e6)
## 2.1 Simple annual tsibble
y <- tsibble(
Year = 2015:2019,
Observation = c(123, 39, 78, 52, 110),
index = Year
)
y
## # A tsibble: 5 x 2 [1Y]
## Year Observation
## <int> <dbl>
## 1 2015 123
## 2 2016 39
## 3 2017 78
## 4 2018 52
## 5 2019 110
melsyd_economy <- ansett |>
filter(Airports == "MEL-SYD", Class == "Economy") |>
mutate(Passengers = Passengers/1000)
autoplot(melsyd_economy, Passengers)
There is a drop to almost zero passengers around 1989.
There are dips around the start of each year.
Passenger numbers increase around 1991.
autoplot(a10, Cost)
There is a clear upward trend.
There is strong seasonality.
The series drops at the start of each year. ## 2.3 Time series patterns
(Trend, Seasonal, Cyclic)
library(fpp3)
autoplot(a10, Cost) +
labs(title = "A10 antidiabetic drug sales",
y = "$ (millions)")
What I notice: This series shows a clear
upward trend and strong seasonality
(repeating yearly pattern).
I don’t see an obvious multi-year cycle here.
# Using an example series that shows longer swings (cycles)
# This dataset is available in fpp3; autoplot will show the time plot
autoplot(us_employment, Employed) +
labs(title = "US employment",
y = "Employed")
## Warning: Removed 71073 rows containing missing values or values outside the scale range
## (`geom_line()`).
What I notice: There are longer ups and downs over multiple years that look like cyclic behaviour.
# Daily changes in a financial series often look like random fluctuations
autoplot(gafa_stock, Close) +
labs(title = "GAFA stock prices (Close)",
y = "Close price")
What I notice: This looks much harder to predict; I
don’t see a clean seasonal pattern.
(Depending on which company is plotted, you may see trends, but daily
variation is noisy.) ## 2.4 Seasonal plots
a10 |>
gg_season(Cost, labels = "both") +
labs(y = "$ (millions)",
title = "Seasonal plot: Antidiabetic drug sales")
## Warning: `gg_season()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_season()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
What I notice: - There is a large jump in sales every January. - The seasonal pattern repeats each year. - Some months (like March or June in certain years) look different from the usual pattern. ## 2.5 Seasonal subseries plots
a10 |>
gg_subseries(Cost) +
labs(
y = "$ (millions)",
title = "Australian antidiabetic drug sales"
)
## Warning: `gg_subseries()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_subseries()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
What I notice: - Each month has its own mini time plot. - Some months consistently have higher sales than others. - The seasonal pattern is easy to compare across months. ## 2.8 Autocorrelation
a10 |>
ACF(Cost, lag_max = 48) |>
autoplot() +
labs(title = "ACF: Australian antidiabetic drug sales")
What I notice: - The autocorrelations start high and slowly decrease, which suggests a trend. - There are repeating spikes at regular lags, showing seasonality. - This pattern indicates the series has both trend and seasonal components.