2.1

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

  1. Use autoplot() to plot some of the series in these data sets.
autoplot(gafa_stock, Open) +
  ggtitle("Daily Opening Price for Stocks Traded, 2014 - 2018")

It can be seen that the opening price increased over time, especially for Amazon and Google. The opening prices peaked a little after mid-2018 and then decreased.

PBS %>%
  summarise(TotalC = sum(Cost)) %>%
  autoplot(TotalC) +
  labs(title = "Total Costs of Scripts in Australia, July 1991 - June 2008",
       y = "Total Cost ($AUD)")

The total costs of all Medicare scripts in Australia increased over time. There also seems to be a seasonality, where the total cost takes a dip and then rises again every year.

autoplot(vic_elec, Demand) +
  ggtitle("Hald-hourly Electricity Demand for Victoria, Australia")

There seems to be a seasonality where electricity demands increase greatly during the summers, followed by a decrease and then a small increase midway into the year, around winter. That is followed by a decrease until summer.

autoplot(pelt, Lynx) +
  ggtitle("Canadaian Lync pelts traded 1845-1945")

There is a cycle that lasts about 10 years, in which the number of Canadian Lynx furs increase and then decrease again.The peaks also seem to alternate, where a higher peak is followed by a shorter peak the next cycle.

  1. What is the time interval of each series?

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)
## # A tsibble: 4 x 2 [!]
## # Key:       Symbol [4]
## # Groups:    Symbol [4]
##   Symbol Date      
##   <chr>  <date>    
## 1 AAPL   2018-10-03
## 2 AMZN   2018-09-04
## 3 FB     2018-07-25
## 4 GOOG   2018-07-26

The peak closing prices were hit between late July to early October for each of the four stocks.

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.

tute1 <- readr::read_csv("tute1.csv")
#View(tute1)

mytimeseries <- tute1 %>%
  mutate(Quarter = yearmonth(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") +
  ggtitle("facet_grid")

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

When you don’t include facet_grid, the horizontal scales are not aligned, nor, can you see the numbers on the scale properly. It creates a separate graph for each variable.

2.4

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

  1. Install the USgas package.
#install.packages("USgas")
library(USgas)
## Warning: package 'USgas' was built under R version 4.0.5
  1. Create a tsibble from us_total with year as the index and state as the key.
us_total <- us_total %>%
  as_tibble(key = state,
            index = year)
  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).
us_total %>%
  filter(state %in% c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut', 'Rhode Island')) %>%
  ggplot(aes(x = year, y = y, colour = state)) +
  geom_line() +
  facet_grid(state ~., scales = "free_y") +
  labs(title = "Annual Natural Gas Consumption in New England",
       y = "Consumption")

The annual natural has consumption follows an increasing trend for Connecticut, Massachusetts, and Vermont, and is decreasing in the remaining states.

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")
  1. Create a tsibble which is identical to the tourism tsibble from the tsibble package.
tourism_ts <- tourism %>%
  mutate(Quarter = yearquarter(Quarter)) %>%
  as_tsibble(key = c(Region, State, Purpose),
             index = Quarter)
  1. Find what combination of Region and Purpose had the maximum number of overnight trips on average.
tourism_ts %>%
  group_by(Region, Purpose) %>%
  mutate(Avg_Trips = mean(Trips)) %>%
  ungroup() %>%
  filter(Avg_Trips == max(Avg_Trips)) %>%
  distinct(Region, Purpose)
## # A tibble: 1 x 2
##   Region Purpose 
##   <chr>  <chr>   
## 1 Sydney Visiting

Syndey, Australia has the maximum number of overnight trips on average for Visiting.

  1. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
tourism %>%
  group_by(Quarter, State) %>%
  mutate(Quarter = yearquarter(Quarter),
         Total_Trips = sum(Trips)) %>%
  select(Quarter, State, Total_Trips) %>%
  distinct() %>%
  as_tsibble(index = Quarter,
             key = State)
## # A tsibble: 640 x 3 [1Q]
## # Key:       State [8]
## # Groups:    State @ Quarter [640]
##    Quarter State Total_Trips
##      <qtr> <chr>       <dbl>
##  1 1998 Q1 ACT          551.
##  2 1998 Q2 ACT          416.
##  3 1998 Q3 ACT          436.
##  4 1998 Q4 ACT          450.
##  5 1999 Q1 ACT          379.
##  6 1999 Q2 ACT          558.
##  7 1999 Q3 ACT          449.
##  8 1999 Q4 ACT          595.
##  9 2000 Q1 ACT          600.
## 10 2000 Q2 ACT          557.
## # ... with 630 more rows

2.8

Monthly Australian retail data is provided in aus_retail. Explore your chosen retail time series using the following functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() %>% autoplot().

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

myseries %>% gg_season(Turnover)

myseries %>% gg_subseries(Turnover)

myseries %>% gg_lag(Turnover, geom = "point")

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

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

There is a clear increasing trend in the retail trade turnover, with a cyclicity of a year. Every year seems to have similar seasonality as the previous year. The turnover seems to increase from November to December and then decreasing to February. There is also a small increase from June to July and a small decrease from October to November. From the series, it can be assumed that sales increase around the holiday season and then decrease afterwards.