R Markdown

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

## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## ── Attaching packages ─────────────────────────────────────────────────────────────── fpp2 2.4 ──
## ✓ ggplot2   3.3.2     ✓ fma       2.4  
## ✓ forecast  8.13      ✓ expsmooth 2.3
## 

Gold: Daily morning gold prices in US dollars. 1 January 1985 – 31 March 198.
woolyrnq: Quarterly production of woollen yarn in Australia: tonnes. Mar 1965 – Sep 1994.
gas: Australian monthly gas production: 1956–1995.

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

require(gridExtra)
## Loading required package: gridExtra
plot1<-autoplot(gold)
plot2<-autoplot(woolyrnq)
plot3<-autoplot(gas)
grid.arrange(plot1, plot2, plot3, nrow=3)

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

Answer: The frequency for gold is daily, for woollen yarn quarterly, for gas monthly.

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

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

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

Answer: Observation number 770 returns 593.7 which is the price of gold when time value equals 770.

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)

b. Convert the data to time series

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

c. Construct time series plots of each of the three series. Check what happens when you don’t include facets=TRUE.

autoplot(mytimeseries, facets=TRUE)

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:

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

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

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

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

A3349335T = Turnover ; New South Wales ; Supermarket and grocery stores ;

c. Explore your chosen retail time series using the following functions:

autoplot(), ggseasonplot(), ggsubseriesplot(), gglagplot(), ggAcf()

autoplot(myts)

ggseasonplot(myts)

ggsubseriesplot(myts)

gglagplot(myts)

ggAcf(myts)

## Can you spot any seasonality, cyclicity and trend? What do you learn about the series? The series exhibits strong seasonality (dec & march months are good for Turnover ;New South Wales;Supermarket and grocery stores ;).

There is no obvious cyclicity. The series is upward trending.

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

Can you spot any seasonality, cyclicity and trend?

What do you learn about the series?

hsales

seasonality March, April and May,  cyclicity there appears to be a cycle , roughly 7.5 years ,
trend no visible trend , a mean reverting process.
What I learned even within the spring months there are material dips in sales (mid month dips)

autoplot(hsales)

ggseasonplot(hsales)

ggsubseriesplot(hsales)

gglagplot(hsales)

ggAcf(hsales)

usdeaths

seasonality summer months (Jun,Jul,Aug),
cyclicity no cycle ,
trend no visible trend.
What I learned I was “expecting” winter months to correlate more with the number of deaths (less daylight which affects mood)

autoplot(usdeaths)

ggseasonplot(usdeaths)

ggsubseriesplot(usdeaths)

gglagplot(usdeaths)

ggAcf(usdeaths)

bricksq

seasonality Q3,
cyclicity there appears to be a cycle in the latter part of the timeseries (after 1974) ,
trend visible trend up to mid 1974.
What I learned Interesting trend within each quarter. We start with a low production of clay at the beginning of the quarter only for it to increase towards the end.

autoplot(bricksq)

ggseasonplot(bricksq)

ggsubseriesplot(bricksq)

gglagplot(bricksq)

ggAcf(bricksq)

sunspotarea

seasonality no seasonality ,
cyclicity cycles are present, roughly every 10 years,
trend no visible trend ,
What I learned ACF shows meaningful positive autocorrel every 10 years, strong negative autocorrel every 5 years. annual data precents from detecting seasonality.

autoplot(sunspotarea)

gglagplot(sunspotarea)

ggAcf(sunspotarea)

gasoline

seasonality hard to say but second half of the year looks to have higher supply of gasoline from the acf graph we can see that 52th week tend to be positively auto-correlated,  cyclicity no clear cycle ,
trend upward trend to 2005 and then trending down to mind of 2012, from which point it started trending up again.
What I learned Interesting to note that gasoline supply tends to spike towards the end of the year.

autoplot(gasoline)

ggseasonplot(gasoline)

gglagplot(gasoline)

ggAcf(gasoline)