Imports

library(fpp3)
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.0 ──
## ✔ tibble      3.2.1     ✔ tsibble     1.1.5
## ✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
## ✔ tidyr       1.3.1     ✔ feasts      0.3.2
## ✔ lubridate   1.9.3     ✔ fable       0.3.4
## ✔ ggplot2     3.5.1     ✔ fabletools  0.4.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()
library(USgas)

Excercise 2.1

Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.

Quarterly production of selected commodities in Australia.

aus_production
## # A tsibble: 218 x 7 [1Q]
##    Quarter  Beer Tobacco Bricks Cement Electricity   Gas
##      <qtr> <dbl>   <dbl>  <dbl>  <dbl>       <dbl> <dbl>
##  1 1956 Q1   284    5225    189    465        3923     5
##  2 1956 Q2   213    5178    204    532        4436     6
##  3 1956 Q3   227    5297    208    561        4806     7
##  4 1956 Q4   308    5681    197    570        4418     6
##  5 1957 Q1   262    5577    187    529        4339     5
##  6 1957 Q2   228    5651    214    604        4811     7
##  7 1957 Q3   236    5317    227    603        5259     7
##  8 1957 Q4   320    6152    222    582        4735     6
##  9 1958 Q1   272    5758    199    554        4608     5
## 10 1958 Q2   233    5641    229    620        5196     7
## # ℹ 208 more rows

What is the time interval? Quarterly

Brick Time Series
autoplot(aus_production  ,.vars = Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

Pelt trading records
pelt
## # A tsibble: 91 x 3 [1Y]
##     Year  Hare  Lynx
##    <dbl> <dbl> <dbl>
##  1  1845 19580 30090
##  2  1846 19600 45150
##  3  1847 19610 49150
##  4  1848 11990 39520
##  5  1849 28040 21230
##  6  1850 58000  8420
##  7  1851 74600  5560
##  8  1852 75090  5080
##  9  1853 88480 10170
## 10  1854 61280 19600
## # ℹ 81 more rows

What is the time interval?

Yearly

Lynx furs trade Time Series
autoplot(pelt,.vars = Lynx)

Gafa Stock Prices
gafa_stock
## # A tsibble: 5,032 x 8 [!]
## # Key:       Symbol [4]
##    Symbol Date        Open  High   Low Close Adj_Close    Volume
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>     <dbl>
##  1 AAPL   2014-01-02  79.4  79.6  78.9  79.0      67.0  58671200
##  2 AAPL   2014-01-03  79.0  79.1  77.2  77.3      65.5  98116900
##  3 AAPL   2014-01-06  76.8  78.1  76.2  77.7      65.9 103152700
##  4 AAPL   2014-01-07  77.8  78.0  76.8  77.1      65.4  79302300
##  5 AAPL   2014-01-08  77.0  77.9  77.0  77.6      65.8  64632400
##  6 AAPL   2014-01-09  78.1  78.1  76.5  76.6      65.0  69787200
##  7 AAPL   2014-01-10  77.1  77.3  75.9  76.1      64.5  76244000
##  8 AAPL   2014-01-13  75.7  77.5  75.7  76.5      64.9  94623200
##  9 AAPL   2014-01-14  76.9  78.1  76.8  78.1      66.1  83140400
## 10 AAPL   2014-01-15  79.1  80.0  78.8  79.6      67.5  97909700
## # ℹ 5,022 more rows

What is the time interval?

Daily frequency from year 2014 to 2018

Gafa Stock Time Series
autoplot(gafa_stock,.vars = Close)

Electricity demand for Victoria, Australia

vic_elec
## # A tsibble: 52,608 x 5 [30m] <Australia/Melbourne>
##    Time                Demand Temperature Date       Holiday
##    <dttm>               <dbl>       <dbl> <date>     <lgl>  
##  1 2012-01-01 00:00:00  4383.        21.4 2012-01-01 TRUE   
##  2 2012-01-01 00:30:00  4263.        21.0 2012-01-01 TRUE   
##  3 2012-01-01 01:00:00  4049.        20.7 2012-01-01 TRUE   
##  4 2012-01-01 01:30:00  3878.        20.6 2012-01-01 TRUE   
##  5 2012-01-01 02:00:00  4036.        20.4 2012-01-01 TRUE   
##  6 2012-01-01 02:30:00  3866.        20.2 2012-01-01 TRUE   
##  7 2012-01-01 03:00:00  3694.        20.1 2012-01-01 TRUE   
##  8 2012-01-01 03:30:00  3562.        19.6 2012-01-01 TRUE   
##  9 2012-01-01 04:00:00  3433.        19.1 2012-01-01 TRUE   
## 10 2012-01-01 04:30:00  3359.        19.0 2012-01-01 TRUE   
## # ℹ 52,598 more rows

What is the time interval? Half-hourly

autoplot(vic_elec,.vars = Demand)+ xlab("Half-hourly electricity demand") + ylab("Total electricity demand in MWh")

Excercise 2.2

Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.

#gafa_stock |> filter(Close,max(Close) > 0)

gafa_stock %>% group_by(Symbol) %>% filter(Close == max(Close))
## # A tsibble: 4 x 8 [!]
## # Key:       Symbol [4]
## # Groups:    Symbol [4]
##   Symbol Date        Open  High   Low Close Adj_Close   Volume
##   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
## 1 AAPL   2018-10-03  230.  233.  230.  232.      230. 28654800
## 2 AMZN   2018-09-04 2026. 2050. 2013  2040.     2040.  5721100
## 3 FB     2018-07-25  216.  219.  214.  218.      218. 58954200
## 4 GOOG   2018-07-26 1251  1270. 1249. 1268.     1268.  2405600

The results are as follows:

  1. Google (GOOG) peak closing price was 1268.33$.
  2. Amazon (AMZN) peak closing price was 2039.51$.
  3. Facebook (FB) peak closing price was 217.50$.
  4. Apple (AAPL) peak closing price was 232.07$.

Excercise 2.3

Download the file tute1.csv from the book website, open it in Excel (or some other spreadsheet application), and review its contents. You should find four columns of information. Columns B through D each contain a quarterly series, labelled Sales, AdBudget and GDP. Sales contains the quarterly sales for a small company over the period 1981-2005. AdBudget is the advertising budget and GDP is the gross domestic product. All series have been adjusted for inflation.

read the data into R:

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)
  1. Convert the data to time series:
mytimeseries <- tute1 |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(index = Quarter)
  1. 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")

  1. Check what happens when you don’t include facet_grid():
mytimeseries |>
  pivot_longer(-Quarter) |>
  ggplot(aes(x = Quarter, y = value, colour = name)) +
  geom_line() 

By using facet_grid(), you can separate data into distinct panels based on one or more variables, making it easier to compare distributions across different categories or levels. Without it, all data is overlaid in a single panel, which can obscure important differences or patterns, especially in complex datasets.

Excercise 2.4

The USgas package contains data on the demand for natural gas in the US.

  1. USgas package installed
head(us_total)
##   year   state      y
## 1 1997 Alabama 324158
## 2 1998 Alabama 329134
## 3 1999 Alabama 337270
## 4 2000 Alabama 353614
## 5 2001 Alabama 332693
## 6 2002 Alabama 379343
  1. Create a tsibble from us_total with year as the index and state as the key.
gasData <- us_total |> as_tsibble(key=state,index=year)
gasData
## # A tsibble: 1,266 x 3 [1Y]
## # Key:       state [53]
##     year state        y
##    <int> <chr>    <int>
##  1  1997 Alabama 324158
##  2  1998 Alabama 329134
##  3  1999 Alabama 337270
##  4  2000 Alabama 353614
##  5  2001 Alabama 332693
##  6  2002 Alabama 379343
##  7  2003 Alabama 350345
##  8  2004 Alabama 382367
##  9  2005 Alabama 353156
## 10  2006 Alabama 391093
## # ℹ 1,256 more rows
  1. 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).
gasData |>
  filter(state %in% c("Maine","Vermont","New Hampshire","Massachusetts","Connecticut","Rhode Island")) |>       
  autoplot(.var=y) + facet_grid(state ~ ., scales = "free_y")+xlab("Year") +
  ylab("monthly natural gas consumption (million cubic feet)") 

Excercise 2.5

Download tourism.xlsx and read it into R using readxl::read_excel().

  1. Create a tsibble which is identical to the tourism tsibble from the tsibble package. Tsibble tourism package
tourism
## # A tsibble: 24,320 x 5 [1Q]
## # Key:       Region, State, Purpose [304]
##    Quarter Region   State           Purpose  Trips
##      <qtr> <chr>    <chr>           <chr>    <dbl>
##  1 1998 Q1 Adelaide South Australia Business  135.
##  2 1998 Q2 Adelaide South Australia Business  110.
##  3 1998 Q3 Adelaide South Australia Business  166.
##  4 1998 Q4 Adelaide South Australia Business  127.
##  5 1999 Q1 Adelaide South Australia Business  137.
##  6 1999 Q2 Adelaide South Australia Business  200.
##  7 1999 Q3 Adelaide South Australia Business  169.
##  8 1999 Q4 Adelaide South Australia Business  134.
##  9 2000 Q1 Adelaide South Australia Business  154.
## 10 2000 Q2 Adelaide South Australia Business  169.
## # ℹ 24,310 more rows

Tourism Data

tourismData <- readxl::read_excel("tourism.xlsx") |> mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(key=c("Region","State","Purpose"),index=Quarter)
tourismData
## # A tsibble: 24,320 x 5 [1Q]
## # Key:       Region, State, Purpose [304]
##    Quarter Region   State           Purpose  Trips
##      <qtr> <chr>    <chr>           <chr>    <dbl>
##  1 1998 Q1 Adelaide South Australia Business  135.
##  2 1998 Q2 Adelaide South Australia Business  110.
##  3 1998 Q3 Adelaide South Australia Business  166.
##  4 1998 Q4 Adelaide South Australia Business  127.
##  5 1999 Q1 Adelaide South Australia Business  137.
##  6 1999 Q2 Adelaide South Australia Business  200.
##  7 1999 Q3 Adelaide South Australia Business  169.
##  8 1999 Q4 Adelaide South Australia Business  134.
##  9 2000 Q1 Adelaide South Australia Business  154.
## 10 2000 Q2 Adelaide South Australia Business  169.
## # ℹ 24,310 more rows
identical(tourism,tourism)
## [1] TRUE

Tourism Data

  1. Find what combination of Region and Purpose had the maximum number of overnight trips on average.
tourismData |> as_tibble() |> group_by(Region,Purpose) |> summarise(averageTrips=mean(Trips)) |> ungroup() |>
  filter(averageTrips == max(averageTrips))
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
## # A tibble: 1 × 3
##   Region Purpose  averageTrips
##   <chr>  <chr>           <dbl>
## 1 Sydney Visiting         747.
  1. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
 tourismData |> group_by(State) |> summarise(totalTrips=sum(Trips)) |> ungroup()
## # A tsibble: 640 x 3 [1Q]
## # Key:       State [8]
##    State Quarter totalTrips
##    <chr>   <qtr>      <dbl>
##  1 ACT   1998 Q1       551.
##  2 ACT   1998 Q2       416.
##  3 ACT   1998 Q3       436.
##  4 ACT   1998 Q4       450.
##  5 ACT   1999 Q1       379.
##  6 ACT   1999 Q2       558.
##  7 ACT   1999 Q3       449.
##  8 ACT   1999 Q4       595.
##  9 ACT   2000 Q1       600.
## 10 ACT   2000 Q2       557.
## # ℹ 630 more rows

Excercise 2.8

Use the following graphics functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() and explore features from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and Barrels from us_gasoline.

  1. Can you spot any seasonality, cyclicity and trend?
  2. What do you learn about the series?
  3. What can you say about the seasonal patterns?
  4. Can you identify any unusual years?

USA Employment

totalPrivateEmployment <-us_employment |> filter(Title == "Total Private")
totalPrivateEmployment
## # A tsibble: 969 x 4 [1M]
## # Key:       Series_ID [1]
##       Month Series_ID     Title         Employed
##       <mth> <chr>         <chr>            <dbl>
##  1 1939 Jan CEU0500000001 Total Private    25338
##  2 1939 Feb CEU0500000001 Total Private    25447
##  3 1939 Mar CEU0500000001 Total Private    25833
##  4 1939 Apr CEU0500000001 Total Private    25801
##  5 1939 May CEU0500000001 Total Private    26113
##  6 1939 Jun CEU0500000001 Total Private    26485
##  7 1939 Jul CEU0500000001 Total Private    26481
##  8 1939 Aug CEU0500000001 Total Private    26848
##  9 1939 Sep CEU0500000001 Total Private    27468
## 10 1939 Oct CEU0500000001 Total Private    27830
## # ℹ 959 more rows
totalPrivateEmployment |> autoplot() 
## Plot variable not specified, automatically selected `.vars = Employed`

Season

totalPrivateEmployment |> gg_season(Employed) 

Subseries

totalPrivateEmployment |> gg_subseries(Employed) 

Lag

totalPrivateEmployment |> gg_lag(Employed) 

Auto Correlation

totalPrivateEmployment |> ACF(Employed) |> autoplot()

Answer

The employment data in the USA demonstrates a clear overall upward trend over time, with strong evidence of seasonality. Two key seasonal patterns are observed: an increase in employment during the summer months and a decrease, or trough, by early January. This repetitive pattern of employment fluctuations remains highly correlated with previous data points, suggesting a predictable trend over the years. However, there are exceptions to this predictability, such as the significant decline in employment between 2008 and 2010, which corresponds to the global financial crisis. Despite such disruptions, the long-term trend shows consistent growth with seasonal variation.

Aus Production

aus_production
## # A tsibble: 218 x 7 [1Q]
##    Quarter  Beer Tobacco Bricks Cement Electricity   Gas
##      <qtr> <dbl>   <dbl>  <dbl>  <dbl>       <dbl> <dbl>
##  1 1956 Q1   284    5225    189    465        3923     5
##  2 1956 Q2   213    5178    204    532        4436     6
##  3 1956 Q3   227    5297    208    561        4806     7
##  4 1956 Q4   308    5681    197    570        4418     6
##  5 1957 Q1   262    5577    187    529        4339     5
##  6 1957 Q2   228    5651    214    604        4811     7
##  7 1957 Q3   236    5317    227    603        5259     7
##  8 1957 Q4   320    6152    222    582        4735     6
##  9 1958 Q1   272    5758    199    554        4608     5
## 10 1958 Q2   233    5641    229    620        5196     7
## # ℹ 208 more rows
aus_production |> autoplot(.var=Bricks) 
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

Season

aus_production |> gg_season(Bricks) 
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

Subseries

aus_production |> gg_subseries(Bricks) 
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_line()`).

Lag

aus_production |> gg_lag(Bricks, geom = "point") 
## Warning: Removed 20 rows containing missing values (gg_lag).

Auto Correlation

aus_production |> ACF(Bricks) |> autoplot()

Answer

The bricks production data initially shows an upward trend until Q1 of 1980, after which a downtrend becomes evident. The data also exhibits cyclicality, with seasonal patterns that can be observed in the autocorrelation plot. Over the years, Q2 and Q3 have consistently experienced the largest increases in production, followed by a decline leading up to Q1 of the following year, when production begins to rise again, albeit more moderately. Between 1980 and 1983, there is a notable sharp decrease in brick production across all quarters, marking a significant trough during this period. Despite these fluctuations, the data follows both a trend and seasonal cycles.

Pelt

pelt
## # A tsibble: 91 x 3 [1Y]
##     Year  Hare  Lynx
##    <dbl> <dbl> <dbl>
##  1  1845 19580 30090
##  2  1846 19600 45150
##  3  1847 19610 49150
##  4  1848 11990 39520
##  5  1849 28040 21230
##  6  1850 58000  8420
##  7  1851 74600  5560
##  8  1852 75090  5080
##  9  1853 88480 10170
## 10  1854 61280 19600
## # ℹ 81 more rows
pelt |> autoplot(.var=Hare)

Season No season

Subseries

pelt |> gg_subseries(Hare) 

Lag

pelt |> gg_lag(Hare, geom = "point") 

Auto Correlation

pelt |> ACF(Hare) |> autoplot()

Answer

The data does not reveal seasonality trends, as it wasn’t divided into shorter periods like days, weeks, months, or quarters. However, while there’s no indication of an overarching trend, clear cyclicality is present. The ACF plot suggests that cycles occur every 10 years, with peaks every decade and troughs happening 5 years after each peak, following a consistent 10-year pattern.

PBS

PBS
## # A tsibble: 67,596 x 9 [1M]
## # Key:       Concession, Type, ATC1, ATC2 [336]
##       Month Concession   Type      ATC1  ATC1_desc ATC2  ATC2_desc Scripts  Cost
##       <mth> <chr>        <chr>     <chr> <chr>     <chr> <chr>       <dbl> <dbl>
##  1 1991 Jul Concessional Co-payme… A     Alimenta… A01   STOMATOL…   18228 67877
##  2 1991 Aug Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15327 57011
##  3 1991 Sep Concessional Co-payme… A     Alimenta… A01   STOMATOL…   14775 55020
##  4 1991 Oct Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15380 57222
##  5 1991 Nov Concessional Co-payme… A     Alimenta… A01   STOMATOL…   14371 52120
##  6 1991 Dec Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15028 54299
##  7 1992 Jan Concessional Co-payme… A     Alimenta… A01   STOMATOL…   11040 39753
##  8 1992 Feb Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15165 54405
##  9 1992 Mar Concessional Co-payme… A     Alimenta… A01   STOMATOL…   16898 61108
## 10 1992 Apr Concessional Co-payme… A     Alimenta… A01   STOMATOL…   18141 65356
## # ℹ 67,586 more rows
H02Data <- PBS |> filter(ATC2 == "H02")
H02Data
## # A tsibble: 816 x 9 [1M]
## # Key:       Concession, Type, ATC1, ATC2 [4]
##       Month Concession   Type     ATC1  ATC1_desc ATC2  ATC2_desc Scripts   Cost
##       <mth> <chr>        <chr>    <chr> <chr>     <chr> <chr>       <dbl>  <dbl>
##  1 1991 Jul Concessional Co-paym… H     Systemic… H02   CORTICOS…   63261 317384
##  2 1991 Aug Concessional Co-paym… H     Systemic… H02   CORTICOS…   53528 269891
##  3 1991 Sep Concessional Co-paym… H     Systemic… H02   CORTICOS…   52822 269703
##  4 1991 Oct Concessional Co-paym… H     Systemic… H02   CORTICOS…   54016 280418
##  5 1991 Nov Concessional Co-paym… H     Systemic… H02   CORTICOS…   49281 268070
##  6 1991 Dec Concessional Co-paym… H     Systemic… H02   CORTICOS…   51798 277139
##  7 1992 Jan Concessional Co-paym… H     Systemic… H02   CORTICOS…   42436 221772
##  8 1992 Feb Concessional Co-paym… H     Systemic… H02   CORTICOS…   52913 272345
##  9 1992 Mar Concessional Co-paym… H     Systemic… H02   CORTICOS…   62908 325700
## 10 1992 Apr Concessional Co-paym… H     Systemic… H02   CORTICOS…   68499 349271
## # ℹ 806 more rows
H02Data |> autoplot(.var=Cost) +  scale_y_continuous(labels = scales::comma) 

Season

H02Data |> gg_season(Cost) +  scale_y_continuous(labels = scales::comma) 

subseries

H02Data |> gg_subseries(Cost) +  scale_y_continuous(labels = scales::comma) 

Auto Correlation

H02Data |> ACF(Cost) |> autoplot()

US Gasoline

us_gasoline
## # A tsibble: 1,355 x 2 [1W]
##        Week Barrels
##      <week>   <dbl>
##  1 1991 W06    6.62
##  2 1991 W07    6.43
##  3 1991 W08    6.58
##  4 1991 W09    7.22
##  5 1991 W10    6.88
##  6 1991 W11    6.95
##  7 1991 W12    7.33
##  8 1991 W13    6.78
##  9 1991 W14    7.50
## 10 1991 W15    6.92
## # ℹ 1,345 more rows
us_gasoline |> autoplot()
## Plot variable not specified, automatically selected `.vars = Barrels`

Season

us_gasoline |> gg_season()
## Plot variable not specified, automatically selected `y = Barrels`

Subseries

us_gasoline |> gg_subseries() 
## Plot variable not specified, automatically selected `y = Barrels`

Lag

us_gasoline |> gg_lag(Barrels, geom = "point") 

Auto Correlation

us_gasoline |> ACF(Barrels) |> autoplot()