tourism.xlsx from the book website and read it
into R.tourism tsibble
from the tsibble package.Region and Purpose had the
maximum number of overnight trips on average.knitr::opts_chunk$set(message = FALSE, warning = FALSE)
options(cli.unicode = FALSE)
library(fpp3)
library(USgas)
library(readxl)
Explore the following four time series: Bricks from
aus_production, Lynx from pelt,
Close from gafa_stock, Demand
from vic_elec.
? (or help()) to find out about the
data in each series.?aus_production
?pelt
?gafa_stock
?vic_elec
Bricks is clay brick production
in millions of bricks.Lynx is
the number of Canadian Lynx pelts traded.Close is the
closing price.Demand is total
electricity demand in MWh.interval(aus_production)
## <interval[1]>
## [1] 1Q
interval(pelt)
## <interval[1]>
## [1] 1Y
interval(gafa_stock)
## <interval[1]>
## [1] !
interval(vic_elec)
## <interval[1]>
## [1] 30m
!) because there is no trading on weekends and
holidays.autoplot() to produce a time plot of each
series.aus_production |> autoplot(Bricks)
pelt |> autoplot(Lynx)
gafa_stock |> autoplot(Close)
vic_elec |>
autoplot(Demand) +
labs(title = "Half-hourly Electricity Demand in Victoria, Australia",
x = "Time (30-minute intervals)",
y = "Demand (MWh)")
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)) |>
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 closing price in the second half of 2018:
tute1 <- readr::read_csv("tute1.csv")
tute1
## # A tibble: 100 x 4
## Quarter Sales AdBudget GDP
## <date> <dbl> <dbl> <dbl>
## 1 1981-03-01 1020. 659. 252.
## 2 1981-06-01 889. 589 291.
## 3 1981-09-01 795 512. 291.
## 4 1981-12-01 1004. 614. 292.
## 5 1982-03-01 1058. 647. 279.
## 6 1982-06-01 944. 602 254
## 7 1982-09-01 778. 531. 296.
## 8 1982-12-01 932. 608. 272.
## 9 1983-03-01 996. 638. 260.
## 10 1983-06-01 908. 582. 280.
## # i 90 more rows
The file has a Quarter column and three quarterly series
from 1981 to 2005: Sales, AdBudget and
GDP.
mytimeseries <- tute1 |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter)
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():
mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()
Without facet_grid(), all three series are drawn on the
same plot with one shared y-axis. Sales is around 1,000, AdBudget around
600 and GDP around 280, so the axis has to cover a wide range. The ups
and downs of each series look smaller and are harder to see, especially
for GDP, which looks almost flat. With facet_grid() and
scales = "free_y", each series gets its own panel and
y-axis, so its pattern is much clearer.
USgas package.install.packages("USgas")
us_total with year as the
index and state as the key.us_gas <- us_total |>
as_tsibble(index = year, key = state)
us_gas
## # 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
## # i 1,256 more rows
us_gas |>
filter(state %in% c("Maine", "Vermont", "New Hampshire",
"Massachusetts", "Connecticut", "Rhode Island")) |>
autoplot(y) +
labs(title = "Annual Natural Gas Consumption in New England",
x = "Year", y = "Million cubic feet")
tourism.xlsx from the book website and read
it into R.tourism_xl <- read_excel("tourism.xlsx")
tourism_xl
## # A tibble: 24,320 x 5
## Quarter Region State Purpose Trips
## <chr> <chr> <chr> <chr> <dbl>
## 1 1998-01-01 Adelaide South Australia Business 135.
## 2 1998-04-01 Adelaide South Australia Business 110.
## 3 1998-07-01 Adelaide South Australia Business 166.
## 4 1998-10-01 Adelaide South Australia Business 127.
## 5 1999-01-01 Adelaide South Australia Business 137.
## 6 1999-04-01 Adelaide South Australia Business 200.
## 7 1999-07-01 Adelaide South Australia Business 169.
## 8 1999-10-01 Adelaide South Australia Business 134.
## 9 2000-01-01 Adelaide South Australia Business 154.
## 10 2000-04-01 Adelaide South Australia Business 169.
## # i 24,310 more rows
tourism
tsibble from the tsibble package.my_tourism <- tourism_xl |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter, key = c(Region, State, Purpose))
my_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.
## # i 24,310 more rows
all.equal(my_tourism, tourism)
## [1] TRUE
Region and
Purpose had the maximum number of overnight trips on
average.my_tourism |>
as_tibble() |>
group_by(Region, Purpose) |>
summarise(Trips = mean(Trips), .groups = "drop") |>
filter(Trips == max(Trips))
## # A tibble: 1 x 3
## Region Purpose Trips
## <chr> <chr> <dbl>
## 1 Sydney Visiting 747.
Sydney with purpose Visiting had the highest average, about 747 thousand overnight trips per quarter.
state_trips <- my_tourism |>
group_by(State) |>
summarise(Trips = sum(Trips))
state_trips
## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter Trips
## <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.
## # i 630 more rows
Use autoplot(), gg_season(),
gg_subseries(), gg_lag() and
ACF() to explore the following time series. For each one,
look for seasonality, cyclicity and trend, describe what you learn about
the series and its seasonal patterns, and identify any unusual
years.
us_employmenttotal_private <- us_employment |>
filter(Title == "Total Private")
total_private |> autoplot(Employed)
total_private |> gg_season(Employed)
total_private |> gg_subseries(Employed)
total_private |> gg_lag(Employed, geom = "point")
total_private |> ACF(Employed, lag_max = 48) |> autoplot()
aus_productionaus_production |> autoplot(Bricks)
aus_production |> gg_season(Bricks)
aus_production |> gg_subseries(Bricks)
aus_production |> gg_lag(Bricks, geom = "point")
aus_production |> ACF(Bricks, lag_max = 24) |> autoplot()
peltpelt |> autoplot(Hare)
pelt |> gg_lag(Hare, geom = "point")
pelt |> ACF(Hare, lag_max = 30) |> autoplot()
pelt is annual data, so gg_season() and
gg_subseries() cannot be used because there is no seasonal
period.
PBSh02 <- PBS |>
filter(ATC2 == "H02") |>
summarise(Cost = sum(Cost))
h02 |> autoplot(Cost)
h02 |> gg_season(Cost)
h02 |> gg_subseries(Cost)
h02 |> gg_lag(Cost, geom = "point")
h02 |> ACF(Cost, lag_max = 48) |> autoplot()
us_gasolineus_gasoline |> autoplot(Barrels)
us_gasoline |> gg_season(Barrels)
us_gasoline |> gg_subseries(Barrels)
us_gasoline |> gg_lag(Barrels, geom = "point")
us_gasoline |> ACF(Barrels, lag_max = 104) |> autoplot()