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

The gafa_stock time series represents historical stock prices from 2014-2018 for Google, Amazon, Facebook and Apple. All prices are in $USD.

The PBS time series represents monthly Medicare Australia prescription data. It comprises data for total number of scripts and the cost of the scripts in $AUD.

The vic_elec time series represents half-hourly electricity demand for Victoria, Australia. It comprises data for the total electricity demand in MW, the temperature in Melbourne and an indicator denoting if that day was a public holiday.

The pelt time series represents Hudson Bay Company trading records for the number Snowshoe Hare and Canadian Lynx furs traded 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.

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

## <interval[1]>
## [1] !
## [1] FALSE
## <interval[1]>
## [1] 1M
## [1] TRUE
## [1] 12
## <interval[1]>
## [1] 30m
## [1] TRUE
## [1] 48
## <interval[1]>
## [1] 1Y
## [1] TRUE
## [1] 1

The time interval for the gafa_stock time series is daily, but it is an irregular one, because the stock prices from a stock exchange are available for business days only i.e. not for weekends and public holidays.

The PBS time series is a monthly time series since it is the monthly Australian prescription data.

The vic_elec time series is of half-hourly intervals.

The pelt time series is an annual time series.

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: 1 x 3 [!]
## # Key:       Symbol [1]
##   Symbol Date       Close
##   <chr>  <date>     <dbl>
## 1 GOOG   2018-07-26 1268.
## # A tsibble: 1 x 3 [!]
## # Key:       Symbol [1]
##   Symbol Date       Close
##   <chr>  <date>     <dbl>
## 1 AAPL   2018-10-03  232.
## # A tsibble: 1 x 3 [!]
## # Key:       Symbol [1]
##   Symbol Date       Close
##   <chr>  <date>     <dbl>
## 1 FB     2018-07-25  218.
## # A tsibble: 1 x 3 [!]
## # Key:       Symbol [1]
##   Symbol Date       Close
##   <chr>  <date>     <dbl>
## 1 AMZN   2018-09-04 2040.

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

##    Quarter  Sales AdBudget   GDP
## 1 Mar 1981 1020.2    659.2 251.8
## 2 Jun 1981  889.2    589.0 290.9
## 3 Sep 1981  795.0    512.5 290.8
## 4 Dec 1981 1003.9    614.1 292.4
## 5 Mar 1982 1057.7    647.2 279.1
## 6 Jun 1982  944.4    602.0 254.0

Convert the data to time series

Construct time series plots of each of the three series

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

When you don’t include the facet_grid() option, it draws the plot on a common scale (common y-axis). It is very likely that the GDP time-series is denominated in a different scale i.e. it should be an order to magnitude higher than the Sales of an individual company. Yet, when drawn on a common scale, it gives an incorrect picture - as if the GDP is lower than the sales and ad budget of the company.

Excluding facet_grade also makes it harder to see the patterns in the individual time-series.

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

Install the USgas package.

##   year   state      y
## 1 1997 Alabama 324158
## 2 1998 Alabama 329134
## 3 1999 Alabama 337270
## 4 2000 Alabama 353614
## 5 2001 Alabama 332693
## 6 2002 Alabama 379343

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

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

Check what this dataset contains.

## # 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.
## [1] 24320     5

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

## # 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.
## [1] "list"
## [[1]]
## Region
## 
## [[2]]
## State
## 
## [[3]]
## Purpose
## # 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.

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

## # A tibble: 1 x 3
##   Region Purpose  MeanTrips
##   <chr>  <chr>        <dbl>
## 1 Sydney Visiting      747.

We can see from the above that trips to sydney for general visitng has the maximum number of overnight trips on average.

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

## # A tsibble: 10 x 3 [1Q]
## # Key:       State [1]
##    State Quarter TotalTrips
##    <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.

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

## # A tsibble: 6 x 5 [1M]
## # Key:       State, Industry [1]
##   State                        Industry            `Series ID`    Month Turnover
##   <chr>                        <chr>               <chr>          <mth>    <dbl>
## 1 Australian Capital Territory Cafes, restaurants~ A3349849A   1982 Apr      4.4
## 2 Australian Capital Territory Cafes, restaurants~ A3349849A   1982 May      3.4
## 3 Australian Capital Territory Cafes, restaurants~ A3349849A   1982 Jun      3.6
## 4 Australian Capital Territory Cafes, restaurants~ A3349849A   1982 Jul      4  
## 5 Australian Capital Territory Cafes, restaurants~ A3349849A   1982 Aug      3.6
## 6 Australian Capital Territory Cafes, restaurants~ A3349849A   1982 Sep      4.2
help(aus_retail)

Explore your chosen retail time series using the following functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF()%>%autoplot()

while the retail Turnover time-series shows an overall upward trend over the period shown, it seems to have undergone a significant increase in the slope of the trend post 1999 i.e. in the new century. There is a dip in the 2010-2013 period probably as a result of the impact of recession in the US and other parts of the world.

The seasonal plot above shows a sharp increase in the holiday months leading into Christmas and the new year, as would be expected from a retail business.

The sub-series plot that breaks out the data by month shows a similar increase in the retail turnover value as we travserse the decades from the 1990s to the 2010s, with a decline in the few years post 2010 co-inciding with the recession. The plot for December shows elevated values compared to other months, and the annual average for December (blue horizontal line) is higher than the corresponding averages of previous months, as can be expected.

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

The ACF plot for lags upto month 26, shows a spike in autocorrelation for lags 1, 12, 24 months, which indicates seasonality. Even while this is happening, there is an overall decreasing trend in autcorrelation, which indicates an upward trend in the underlying time series.