Exercises 2.1-2.5 and 2.8
2.1. Use the help function to explore what the series gafa_stock, PBS, vic_elec and pelt represent. Use autoplot() to plot some of the series in these data sets. What is the time interval of each series?
The interval is a day - however, as it is stock prices, only weekdays appear in the time series.
## [1] "The time interval for gafa_stock : "
## <interval[1]>
## [1] !
## [1] "The time interval for vic_elec : "
## <interval[1]>
## [1] 30m
## [1] "The time interval for pelt : "
## <interval[1]>
## [1] 1Y
## # A tsibble: 6 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 Concessional Co-pa~ A Alimentary ~ A01 STOMATOLO~ 18228 67877
## 2 1991 Aug Concessional Co-pa~ A Alimentary ~ A01 STOMATOLO~ 15327 57011
## 3 1991 Sep Concessional Co-pa~ A Alimentary ~ A01 STOMATOLO~ 14775 55020
## 4 1991 Oct Concessional Co-pa~ A Alimentary ~ A01 STOMATOLO~ 15380 57222
## 5 1991 Nov Concessional Co-pa~ A Alimentary ~ A01 STOMATOLO~ 14371 52120
## 6 1991 Dec Concessional Co-pa~ A Alimentary ~ A01 STOMATOLO~ 15028 54299
## [1] "The time interval for PBS_A01 : "
## <interval[1]>
## [1] 1M
2.2. Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.
## # A tsibble: 4 x 3 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Close Date
## <chr> <dbl> <date>
## 1 AAPL 232. 2018-10-03
## 2 AMZN 2040. 2018-09-04
## 3 FB 218. 2018-07-25
## 4 GOOG 1268. 2018-07-26
2.3 Using the tute1 dataset from the book, compare the timeplots when you do and don’t include facet_grid().
Facet_grid allows each timeplot to have it’s own scale. The look is also tighter.
2.4 The USgas package contains data on the demand for natural gas in the US. Install the USgas package. Create a tsibble from us_total with year as the index and state as the key. 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).
The data here would be more interesting if calculated per capita.
2.5 Download tourism.xlsx from the book website and read it into R using readxl::read_excel(). Create a tsibble which is identical to the tourism tsibble from the tsibble package.
## # A tibble: 6 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.
## # A tsibble: 6 x 5 [1Q]
## # Key: Region, State, Purpose [1]
## 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.
2.5a. Find what combination of Region and Purpose had the maximum number of overnight trips on average.
Since we need to remove the index as part of the group, I turned the tsibble into a tibble for this analysis.
## # A tibble: 304 x 3
## # Groups: Region [76]
## Region Purpose AveTrips
## <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.
## # ... with 294 more rows
2.5b. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
Since the answer is to be a tsibble, I retained the Quarter even though the question did not explicitly ask for it.
## # 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
8. Monthly Australian retail data is provided in aus_retail. Select one of the time series as follows (but choose your own seed value):
Explore your chosen retail time series using the following functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() %>% autoplot(). Can you spot any seasonality, cyclicity and trend? What do you learn about the series?
Trend: There is a clear upward trend through the upward 2010s - after which the trend reverses briefly and then there appears to be white noise. This is evident both from the timeplot and from the acf (which shows a strong relationship decreasing over time). An acf plot from 2014 on shows much lower correlation (and a stronger seasonal trend).
Seasonality: There is a slight tendency to see higher turnover in the summer months. This tendency has become more pronounced in recent years. We can see from the seasonal subplots that average turnover shows a mild seasonal pattern, but when we look at peak monthly turnover the pattern is even stronger.
The trend is strong enough to pick up in the acf, but not in the lag plots.
Cyclicality: There is possibly a slight cyclical tendency every 10 years or so - we see a peak and then drop.