2.1

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

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

#PBS Monthly Medicare Australia prescription data 
?PBS

# Half-hourly electricity demand for Victoria, Australia
?vic_elec

# Pelt trading records annual values
?pelt

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

autoplot(gafa_stock, Volume)+ggtitle("GAFA Stock Adj Close")

head(PBS,10)
## # A tsibble: 10 x 9 [1M]
## # Key:       Concession, Type, ATC1, ATC2 [1]
##       Month Concession  Type   ATC1  ATC1_desc   ATC2  ATC2_desc   Scripts  Cost
##       <mth> <chr>       <chr>  <chr> <chr>       <chr> <chr>         <dbl> <dbl>
##  1 1991 Jul Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   18228 67877
##  2 1991 Aug Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   15327 57011
##  3 1991 Sep Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   14775 55020
##  4 1991 Oct Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   15380 57222
##  5 1991 Nov Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   14371 52120
##  6 1991 Dec Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   15028 54299
##  7 1992 Jan Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   11040 39753
##  8 1992 Feb Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   15165 54405
##  9 1992 Mar Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   16898 61108
## 10 1992 Apr Concession… Co-pa… A     Alimentary… A01   STOMATOLOG…   18141 65356
autoplot(PBS, Cost)

autoplot(vic_elec)+ggtitle("Half-hourly Electricity Demand For Victoria, Australia")
## Plot variable not specified, automatically selected `.vars = Demand`

autoplot(pelt)+ggtitle("Pelt Trading Records")
## Plot variable not specified, automatically selected `.vars = Hare`

What is the time interval of each series?

gafa_stock is daily PBS is monthly vic_elec is half haurly pelt is annually

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

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.

  1. You can read the data into R with the following script:
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.
datatable(tute1)
  1. Convert the data to time series
mytimeseries <- tute1 %>%
  mutate(Quarter = yearmonth(Quarter)) %>%
  as_tsibble(index = Quarter)

datatable(mytimeseries)
  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")

Check what happens when you don’t include facet_grid().

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

2.4

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

#install.packages("USgas")
library(USgas)

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

us_total1 <- us_total %>% as_tsibble(index = year, key = state)
head(us_total1)
## # A tsibble: 6 x 3 [1Y]
## # Key:       state [1]
##    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

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

myus_total <- us_total %>% 
  as_tsibble(key=state, index = year) %>%
  filter(state %in% c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut' ,'Rhode Island'))

autoplot(myus_total, y)

2.5

  1. Download tourism.xlsx from the book website and read it into R using readxl::read_excel().
tourism <- readxl::read_excel("tourism.xlsx")

datatable(tourism)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html
  1. Create a tsibble which is identical to the tourism tsibble from the tsibble package.
tourism1 <- tourism %>% 
              mutate(Quarter = yearquarter(Quarter)) %>%
              as_tsibble(key = c("Region", "State", "Purpose"),index = "Quarter")

datatable(tourism1)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html
  1. Find what combination of Region and Purpose had the maximum number of overnight trips on average.
tourism2 <- tourism %>% 
              group_by(Region,Purpose) %>%
              summarise (mean = mean(Trips))%>% ungroup() %>% filter (mean == max(mean))
## `summarise()` has grouped output by 'Region'. You can override using the `.groups` argument.
datatable(tourism2)
  1. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
tourism3 <- tourism %>% 
  group_by(State) %>% 
  summarise(Trips = sum(Trips)) %>%
  ungroup()
datatable(tourism3)

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(12345678)
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()

autoplot(myseries,Turnover)

gg_season(myseries, Turnover)

gg_subseries(myseries, Turnover)

gg_lag(myseries, Turnover)

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

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