# Loading required packages
library(fpp3)Data 624 Homework 1
2.1 Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.
aus_production |>
autoplot(Bricks)Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
The plot shows a series that raises from around 1950 up until 1980 and then it flattens with some fluctuations.
pelt |>
autoplot(Lynx)In this plot we can see a cyclic series it swings in a similar pattern every 10 years
gafa_stock |>
autoplot(Close)The stock market plot shows four tech companies and the stock market performance since 2014. It is clear that Google and amazon have outperform considerably.
vic_elec |>
autoplot(Demand) +
labs(
title = "Electricity Deman",
x = "Time",
y = "Demand"
)Demand of electricity has had some high spikes at the beginning of each year.
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 |>
group_by(Symbol) |>
filter(Close == max(Close)) |>
select(Symbol, Date, Close) |>
ungroup()# A tsibble: 4 x 3 [!]
# Key: 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.
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 data into R
library(readr)
tute1 <- read_csv("https://otexts.com/fpp3/extrafiles/tute1.csv")- Converting the data to time series
mytimeseries <- tute1 |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter)- Constructing 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")mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()Dropping facet_grid puts all three lines on one shared axisw which changes considerably GDP by making it look flatter
2.4 The USgas package contains data on the demand for natural gas in the US.
- Install the USgas package.
# install.packages("USgas")- Create a tsibble from us_total with year as the index and state as the key.
library(USgas)
tsibble_us <- us_total |>
as_tsibble(key = state, index = year)- 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).
new_england <- c(
"Maine", "Vermont", "New Hampshire",
"Massachusetts", "Connecticut", "Rhode Island"
)
tsibble_us |>
filter(state %in% new_england) |>
autoplot(y) +
labs(
title = "Annual Natural Gas Consumption for the New England area",
x = "Year",
y = "Consumption"
)2.5
- Download tourism.xlsx from the book website and read it into R using readxl::read_excel().
tourism_excel <- readxl::read_excel("data/tourism.xlsx")- Create a tsibble which is identical to the tourism tsibble from the tsibble 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_excel# A tibble: 24,320 × 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.
# ℹ 24,310 more rows
The difference between the two datasets is that that the excel contgains the specific date as oppossed to a data and an additiona quarter column, we can fix that by doing the following
tourism_excel |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(key = c(Region, State, Purpose), index = Quarter)# 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
- Find what combination of Region and Purpose had the maximum number of overnight trips on average.
tourism_excel |>
as_tibble() |>
group_by(Region, Purpose) |>
summarise(avg_trips = mean(Trips))|>
arrange(desc(avg_trips))`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by Region and Purpose.
ℹ Output is grouped by Region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(Region, Purpose))` for per-operation grouping
(`?dplyr::dplyr_by`) instead.
# A tibble: 304 × 3
# Groups: Region [76]
Region Purpose avg_trips
<chr> <chr> <dbl>
1 Sydney Visiting 747.
2 Melbourne Visiting 619.
3 Sydney Business 602.
4 North Coast NSW Holiday 588.
5 Sydney Holiday 550.
6 Gold Coast Holiday 528.
7 Melbourne Holiday 507.
8 South Coast Holiday 495.
9 Brisbane Visiting 493.
10 Melbourne Business 478.
# ℹ 294 more rows
d.Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
tourism_excel <- tourism_excel |>
mutate(Quarter = yearquarter(Quarter))
tourism_by_state <- tourism_excel |>
as_tibble() |>
group_by(State, Quarter) |>
summarise(Trips = sum(Trips)) |>
as_tsibble(key = State, index = Quarter)`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by State and Quarter.
ℹ Output is grouped by State.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(State, Quarter))` for per-operation grouping
(`?dplyr::dplyr_by`) instead.
tourism_by_state# A tsibble: 640 x 3 [1Q]
# Key: State [8]
# Groups: 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.
# ℹ 630 more rows
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.
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 <- 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)total_private |>
ACF(Employed) |>
autoplot()aus_production |>
autoplot(Bricks)Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
aus_production |>
gg_season(Bricks)Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
aus_production |>
gg_subseries(Bricks)Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
aus_production |>
gg_lag(Bricks)Warning: Removed 20 rows containing missing values (gg_lag).
aus_production |>
ACF(Bricks) |>
autoplot()pelt |>
autoplot(Hare)pelt |>
gg_subseries(Hare)pelt |>
gg_lag(Hare)pelt |>
ACF(Hare) |>
autoplot()h02 <- PBS |>
filter(ATC2 == "H02") |>
summarise(Cost = sum(Cost))
h02 |>
autoplot(Cost)h02 |>
gg_season(Cost)h02 |>
gg_subseries(Cost)h02 |>
gg_lag(Cost)h02 |>
ACF(Cost) |>
autoplot()us_gasoline |>
autoplot(Barrels)us_gasoline |>
gg_season(Barrels)us_gasoline |>
gg_subseries(Barrels)us_gasoline |>
gg_lag(Barrels)us_gasoline |>
ACF(Barrels) |>
autoplot()