if (!require('fpp2')) (install.packages('fpp2'))
if (!require('magrittr')) (install.packages('magrittr'))
if (!require('dplyr')) (install.packages('dplyr'))
if (!require('readxl')) (install.packages('readxl'))
if (!require('RCurl')) (install.packages('RCurl'))
Exercise 2.10.1
Use the help function to explore what the series gold, woolyrng, and gas represent.
#help(gold)
#help(woolyrnq)
#help(gas)
a. Use autoplot() to plot each of these in separate plots.
# Set consistent formatting for plots
formatting <- theme(plot.title = element_text(hjust = .5)) +
theme(text = element_text(size = 10))
# Plot gold
autoplot(gold, main = 'Daily morning gold prices') +
formatting

# Plot woolyrnq
autoplot(woolyrnq, main = 'Quarterly production of woollen yarn in Australia') +
formatting

# Plot gas
autoplot(gas, main = 'Australian monthly gas production') +
formatting

b. What is the frequency of each series? Hint: apply the frequency() function.
frequency(gold) # same as forecast::findfrequency()
frequency(woolyrnq)
frequency(gas)
The frequency of gold is 1 (annual), that of woolyrnq is 4 (quarterly), and that of gas is 12 (monthly).
c. Use which.max() to splot the outlier in the gold series. Which observation was it?
# Define [x, y] coordinates for highest gold price
max_x = which.max(gold)
max_y = gold[max_x]
# Calculate the date of that price based on a 1/1/1985 start of the ts
gold_max_date <- as.Date("1985-01-01") + max_x
gold_max_dayofweek = strftime(gold_max_date,'%A')
gold_max_caption <- sprintf('Observation %s, or %s (%s)', max_x, gold_max_date, gold_max_dayofweek)
# Highlight highest price point and corresponding date on plot
autoplot(gold, main = 'Daily morning gold prices') +
formatting +
## geom_vline(xintercept = 770, color = 'red') +
geom_point(aes(x = max_x, y = max_y), size = 5, shape = 1, color = 'red') +
annotate(geom = 'text', x = max_x, y = max_y, label = gold_max_caption, color = 'red', hjust = 1.05, vjust = .25)

Exercise 2.10.3
Download some monthly Australian retail data from the book website. These represent retail sales in various categories for different Australian states, and are stored in a MS-Excel file.
a. Read the data into R.
temp.file <- paste(tempfile(), ".xlsx", sep = "")
download.file('https://otexts.com/fpp2/extrafiles/retail.xlsx', temp.file, mode = 'wb')
retaildata <- readxl::read_excel(path = temp.file, skip = 1)
# str(retaildata)
b. Select one of the time series.
myts <- ts(retaildata[,'A3349508C'], frequency = 12, start = c(1982,4))
# frequency(myts)
c. Explore that time series using the following functions: autoplot(), ggseasonplot(), ggsubseriesplot(), gglagplot(), ggAcf(). Can you spot any seasonality, cyclicity, and trend? What do you learn about the series?
autoplot(myts, main = 'A3349508C Series, 1982-2013') +
formatting

There is a clear long-term trend of growth in this monthly data series; during the 1980s it measured in the teens, while by the 2010s it ranged between 60 and 100.
ggseasonplot(myts, polar = TRUE, main = 'A3349508C Series, 1982-2013') +
formatting

There is evidence of a seasonal pattern in the ellipsoid shape of the polar plot, with December peaking above other months - presumably for the Christmas holiday.
ggsubseriesplot(myts, main = 'A3349508C Series, 1982-2013') +
formatting

The seasonal subseries plot highlights the peak in December in months.
gglagplot(myts, main = 'A3349508C Series, 1982-2013') +
formatting

The lag plot doesn’t offer much in the way of insight for this series.
ggAcf(myts)

Consistent with long-term trend observed in the first plot, autocorrelations are large and positive, and gradually decrease as lags increase. The impact of December peaks can also be observed but it is small.
## R version 3.5.2 (2018-12-20)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] RCurl_1.95-4.12 bitops_1.0-6 readxl_1.3.1 dplyr_0.8.0.1
## [5] magrittr_1.5 fpp2_2.3 expsmooth_2.3 fma_2.3
## [9] forecast_8.7 ggplot2_3.1.0
##
## loaded via a namespace (and not attached):
## [1] zoo_1.8-5 tidyselect_0.2.5 xfun_0.5
## [4] purrr_0.3.0 urca_1.3-0 lattice_0.20-38
## [7] colorspace_1.4-0 htmltools_0.3.6 yaml_2.2.0
## [10] rlang_0.3.1 pillar_1.3.1 glue_1.3.0
## [13] withr_2.1.2 TTR_0.23-4 plyr_1.8.4
## [16] quantmod_0.4-14 stringr_1.4.0 timeDate_3043.102
## [19] munsell_0.5.0 gtable_0.2.0 cellranger_1.1.0
## [22] evaluate_0.13 labeling_0.3 knitr_1.21
## [25] tseries_0.10-46 lmtest_0.9-36 parallel_3.5.2
## [28] curl_3.3 xts_0.11-2 Rcpp_1.0.1
## [31] scales_1.0.0 fracdiff_1.4-2 digest_0.6.18
## [34] stringi_1.3.1 grid_3.5.2 quadprog_1.5-7
## [37] tools_3.5.2 lazyeval_0.2.1 tibble_2.0.1
## [40] crayon_1.3.4 pkgconfig_2.0.2 assertthat_0.2.0
## [43] rmarkdown_1.11 R6_2.4.0 nnet_7.3-12
## [46] nlme_3.1-137 compiler_3.5.2