require(fpp2)
## Loading required package: fpp2
## Loading required package: ggplot2
## Loading required package: forecast
## Loading required package: fma
## Loading required package: expsmooth

2.1 Use the help function to explore what the series gold, woolyrnq, and gas represent.

a. Use autoplot() to plot each of these in separate plots.

gold is the daily morning gold prices in US dollars from 1 January 1985 - 31 March 1989

autoplot(gold) + ggtitle("Daily morning gold prices (1985 - 1989)") +
  xlab("Year") + ylab("Price")

woolyrnq is the quarterly production of woollen yarn in Australia (tonnes) from March 1965 - September 1994.

autoplot(woolyrnq) + ggtitle("Australian woollen yarn production (1965 - 1994)") +
  xlab("Year") + ylab("Tonnes")

gas is the Australian monthly gas production from 1956 - 1995.

autoplot(gas) + ggtitle("Australian gas production (1956 - 1995)") +
  xlab("Year") + ylab("Gas")

b. What is the frequency of each series? Hint: Apply the frequency() function

frequency(gold)
## [1] 1
frequency(woolyrnq)
## [1] 4
frequency(gas)
## [1] 12

The frequency of gold is annual. The frequency of woolyrnq is quarterly. The frequency of gas is monthly.

c. Use which.max() to spot the outlier in the gold series. Which observation was it?

which.max(gold)
## [1] 770

It was observation 770, which had a price of $593.70.

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

a. You can read the data into R with the following script:

tute1 <- read.csv("tute1.csv", header = TRUE)
View(tute1)

b. Convert the data to time series

# The [,-1] removes the first column which contains the quarters we don't need
mytimeseries <- ts(tute1[, -1], start = 1981, frequency = 4)

c. Construct time series plots of each of the three series.

autoplot(mytimeseries, facets = TRUE)

Without facets = TRUE:

autoplot(mytimeseries)

All the graphs are plotted on one plane, which makes reading all three a little difficult.

2.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. You can read the data into R with the following script:

# The second argument (skip = 1) is required because the Excel sheet has two header rows
retaildata <- readxl::read_excel("retail.xlsx", skip = 1)

b. Select one of the time series as follows (but replace the column name with your own chosen column):

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

c. 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) + ggtitle("A3349337W") +
  xlab("Year") + ylab("Sales")

The autoplot shows a strong seasonality to the data, as well as an upward trend. Though there is a brief dip from 1990-2000, there is no evidence that this is part of a cycle yet.

ggseasonplot(myts, year.labels = TRUE, year.labels.left = TRUE) +
  ylab("Sales") + ggtitle("Seasonal Plot of A3349337W")

The seasonal plot emphasizes the seasonality of the data. Sales start to rise in the fall before spiking sharply between November and December, then falling off after January, obviously coinciding with holiday shopping and sales for Christmas.

ggsubseriesplot(myts) + ylab("Sales") +
  ggtitle("Seasonal Subseries Plot of A3349337W")

Again, the subseries highlights the seasonality of the data, but paints it clearer than the seasonal plot. Though sales rise from September, the floor actually remains the same. The only real difference is in December, which not only has a higher ceiling, but a higher floor as well.

gglagplot(myts)

The data is not very readable in this lag series. We can see some negative relationships and some positive relationships, but the amount of graphs, and the fact that this is monthly, make it difficult to discern much.

ggAcf(myts)

The decrease in lags highlights the trend, while the scalloped shape shows the seasonality of the sales data.