Exercise 2.1

Use the help function to explore what the series gafa_stock, PBS, vic_elec and pelt represent.

gafa_stock Series Description

help(gafa_stock)

Historical stock prices from 2014-2018 for Google, Amazon, Facebook and Apple. All prices are in $USD.

PBS Series Description

help(PBS)

PBS is a monthly tsibble with two values:

Scripts: Total number of scripts

Cost: Cost of the scripts in $AUD

vic_elec Series Description

help(vic_elec)

vic_elec is a half-hourly tsibble with three values:

Demand: Total electricity demand in MW.

Temperature: Temperature of Melbourne (BOM site 086071).

Holiday: Indicator for if that day is a public holiday.

pelt Series Description

help(pelt)

Hudson Bay Company trading records for Snowshoe Hare and Canadian Lynx furs from 1845 to 1935. This data contains trade records for all areas of the company.

 

2.1 A. Use autoplot() to plot some of the series in these data sets.

pelt Series

autoplot(pelt)

vic_elec Series

vic_elec %>% autoplot(Demand)

2.1 B. What is the time interval of each series?.

   

Exercise 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))
## # 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

   

Exercise 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.

2.3 A. You can read the data into R with the following script:

tute1 <- readr::read_csv("tute1.csv")
# For readability purposes, use Data Tables to display the data rather than View().
datatable(tute1)

2.3 B. Convert the data to time series

mytimeseries <- tute1 %>%
  mutate(Quarter = yearmonth(Quarter)) %>%
  as_tsibble(index = Quarter)

2.3 C. 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().

mytimeseries %>%
  pivot_longer(-Quarter) %>%
  ggplot(aes(x = Quarter, y = value, colour = name)) +
  geom_line()

Answer: When facet_grid() is removed, The results are displayed in one graph rather than 3 seperate graphs.

   

Exercise 2.4

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

2.4 A. Install the USgas package.

library(USgas)

2.4 B. Create a tsibble from us_total with year as the index and state as the key.

gas_tsibble <- us_total %>% as_tsibble(key = state, index = year)
gas_tsibble
## # 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
## # … with 1,256 more rows

2.4 C. 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).

# Disable scientific numbers for readability purposes.
options(scipen = 999)

# Select New England states from the dataset.
annual_gas_consumption <- gas_tsibble %>%
  filter(state == c("Maine", "Vermont",
                    "New Hampshire", "Massachusetts",
                    "Connecticut", "Rhode Island"))

# Create the plot.
autoplot(annual_gas_consumption, y) +
  labs(title = "New England Annual Natural Gas Consumption by State",
       y = "Gas Consumption (Million Cubic Feet)",
       x = "Year")

   

Exercise 2.5

2.5 A. Download tourism.xlsx from the book website and read it into R using readxl::read_excel().

tourism <- readxl::read_excel("tourism.xlsx")

2.5 B. Create a tsibble which is identical to the tourism tsibble from the tsibble package.

tourism_tsibble <- tourism %>%
  mutate(Quarter = yearquarter(Quarter) ) %>%
  as_tsibble(index = Quarter, key = c(Region, State, Purpose))

tourism_tsibble
## # 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.
## # … with 24,310 more rows

2.5 C. Find what combination of Region and Purpose had the maximum number of overnight trips on average.

tourism_tsibble %>% 
  group_by(Region, Purpose) %>%
  summarise(Trips = mean(Trips)) %>%
  ungroup() %>%
  filter(Trips == max(Trips))
## # A tsibble: 1 x 4 [1Q]
## # Key:       Region, Purpose [1]
##   Region    Purpose  Quarter Trips
##   <chr>     <chr>      <qtr> <dbl>
## 1 Melbourne Visiting 2017 Q4  985.

2.5 D. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.

total_trips_tsibble <- tourism_tsibble %>%
  group_by(State) %>%
  summarise(Trips = round(sum(Trips), 2)) %>%
  ungroup()

total_trips_tsibble
## # 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.
## # … with 630 more rows

   

Exercise 2.8

Monthly Australian retail data is provided in aus_retail. Select one of the time series as follows (but choose your own seed value):

set.seed(7777777)
myseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1))

Explore your chosen retail time series using the following functions:

autoplot(), gg_season(), gg_subseries(), gg_lag(),

ACF() %>% autoplot()

Using autoplot()

myseries %>% autoplot(Turnover)

Using gg_season()

myseries %>% gg_season(Turnover)

Using gg_subseries()

myseries %>% gg_subseries(Turnover)

Using gg_lag()

myseries %>% gg_lag(Turnover)

Using ACF() %>% autoplot()

myseries %>% ACF(Turnover) %>%
 autoplot()

Can you spot any seasonality, cyclicity and trend? What do you learn about the series?