Chapter 2 - Time series graphics

  1. Use the help menu to explore what the series gold, woolyrnq and gas represent. These are available in the forecast package.
str(gold)
##  Time-Series [1:1108] from 1 to 1108: 306 300 303 297 304 ...
str(woolyrnq)
##  Time-Series [1:119] from 1965 to 1994: 6172 6709 6633 6660 6786 ...
str(gas)
##  Time-Series [1:476] from 1956 to 1996: 1709 1646 1794 1878 2173 ...
  1. Use autoplot() to plot each of these in separate plots.
autoplot(gold)

autoplot(woolyrnq)

autoplot(gas)

writeLines("")
  1. What is the frequency of each series? Hint: apply the frequency() function.
cat("Gold Frequency: ", frequency(gold))
## Gold Frequency:  1
cat("Woolyrnq Frequency: ", frequency(woolyrnq))
## Woolyrnq Frequency:  4
cat("Gas Frequency: ", frequency(gas))
## Gas Frequency:  12
  1. Use which.max() to spot the outlier in the gold series. Which observation was it?
cat("When gold got maximum value? ", which.max(gold))
## When gold got maximum value?  770
cat("What was the gold's maximum value? ", gold[which.max(gold)])
## What was the gold's maximum value?  593.7


  1. Download the file tute1.csv from OTexts.org/fpp2/extrafiles/tute1.csv, 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 <- read.csv("tute1.csv", header=TRUE)

 View(tute1)

tute1 <- read.csv("tute1.csv", header=TRUE)
View(tute1)
  1. Convert the data to time series

 mytimeseries <- ts(tute1[,-1], start=1981, frequency=4)

(The [,-1] removes the first column which contains the quarters as we don’t need them now.)

mytimeseries <- ts(tute1[,-1], start=1981, frequency=4)
  1. Construct time series plots of each of the three series

 autoplot(mytimeseries, facets=TRUE)

Check what happens when you don’t include facets=TRUE.

autoplot(mytimeseries, facets=TRUE)

autoplot(mytimeseries)



  1. Download some monthly Australian retail data from OTexts.org/fpp2/extrafiles/retail.xlsx. These represent retail sales in various categories for different Australian states, and are stored in a MS-Excel file.
  1. You can read the data into R with the following script:

 retaildata <- readxl::read_excel("retail.xlsx", skip=1)

The second argument (skip=1) is required because the Excel sheet has two header rows.

retaildata <- readxl::read_excel("retail.xlsx", skip=1)
## readxl works best with a newer version of the tibble package.
## You currently have tibble v1.4.2.
## Falling back to column name repair from tibble <= v1.4.2.
## Message displays once per session.
  1. Select one of the time series as follows (but replace the column name with your own chosen column):

 myts <- ts(retaildata[,"A3349873A"], frequency=12, start=c(1982,4))

myts <- ts(retaildata[,"A3349873A"], frequency=12, start=c(1982,4))
  1. Explore your chosen retail 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)

ggseasonplot(myts)

ggsubseriesplot(myts)

gglagplot(myts, lags = 12)

ggAcf(myts)

cat("can see seasonality and trend of the data")
## can see seasonality and trend of the data


  1. Use the the following graphics functions: autoplot, ggseasonplot, ggsubseriesplot, gglagplot, ggAcf and explore features from the following time series: hsales, usdeaths, bricksq, sunspotarea, gasoline.

autoplot(hsales)

ggseasonplot(hsales)

ggsubseriesplot(hsales)

gglagplot(hsales)

ggAcf(hsales, lag.max = 400)

cat("can spot seasonality and cyclicity. The cycle period is about 4 years(100 months)")
## can spot seasonality and cyclicity. The cycle period is about 4 years(100 months)

autoplot(usdeaths)

ggseasonplot(usdeaths)

ggsubseriesplot(usdeaths)

gglagplot(usdeaths)

ggAcf(usdeaths, lag.max = 60)

cat("can spot seasonality")
## can spot seasonality

autoplot(bricksq)

ggseasonplot(bricksq)

ggsubseriesplot(bricksq)

gglagplot(bricksq)

ggAcf(bricksq, lag.max = 200)

cat("can spot little seasonality and strong trend")
## can spot little seasonality and strong trend

autoplot(sunspotarea)

# ggseasonplot(sunspotarea) 
# ggsubseriesplot(sunspotarea)
print("For ggseasonplot, not seasonal | can't draw it. For ggsubseriesplot, not seasonal | useless to draw it")
## [1] "For ggseasonplot, not seasonal | can't draw it. For ggsubseriesplot, not seasonal | useless to draw it"
gglagplot(sunspotarea)

ggAcf(sunspotarea, lag.max = 50)

cat("can spot strong cyclicity")
## can spot strong cyclicity

autoplot(gasoline)

ggseasonplot(gasoline)

# ggsubseriesplot(gasoline)
print("The number of weeks is 52 and it looked like it is too much for subseriesplot")
## [1] "The number of weeks is 52 and it looked like it is too much for subseriesplot"
gglagplot(gasoline)

ggAcf(gasoline, lag.max = 1000)

cat("can spot seasonality and trend")
## can spot seasonality and trend