#install.packages('fpp2')
Question 1: Use the help function to explore what the series gold, woolyrnq and gas represent.
Use autoplot() to plot each of these in separate plots. What is the frequency of each series? Hint: apply the frequency() function. Use which.max() to spot the outlier in the gold series. Which observation was it?
library(fpp2)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching packages ────────────────────────────────────────────── fpp2 2.5 ──
## ✔ ggplot2 3.4.4 ✔ fma 2.5
## ✔ forecast 8.21.1 ✔ expsmooth 2.3
##
library(ggplot2)
#help("gold")
#help("woolyrnq")
#help("gas")
help("gold")# ->Daily morning gold prices in US dollars. 1 January 1985 – 31 March 1989.
## starting httpd help server ... done
autoplot(gold)
frequency(gold)# -> Frequency is 1.
## [1] 1
which.max(gold)# -> The outlier is observation 770
## [1] 770
help("woolyrnq") #-> Quarterly production of woollen yarn in Australia
autoplot(woolyrnq)
frequency(woolyrnq) #-> Frequency is 4.
## [1] 4
which.max(woolyrnq) #-> The outlier is observation 21
## [1] 21
help("gas") #-> Australian monthly gas production
autoplot(gas)
frequency(gas) #-> Frequency is 12.
## [1] 12
which.max(gas) #-> The outlier is observation 475
## [1] 475
Question 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.
You can read the data into R with the following script:
tute1 <- read.csv(“tute1.csv”, header=TRUE) View(tute1) 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.)
Construct time series plots of each of the three series
autoplot(mytimeseries, facets=TRUE) Check what happens when you don’t include facets=TRUE.
tute1 <- read.csv("tute1.csv", header=TRUE)
View(tute1)
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.)
autoplot(mytimeseries, facets=TRUE)
#Check what happens when you don’t include facets=TRUE.
autoplot(mytimeseries)
#Check what happens when you don’t include facets=TRUE.
# When removed, the y-axis is not seperated by the indivdual time series.
Question 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.
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.
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)) 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?
retaildata <- readxl::read_excel("retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349337W"],
frequency=12, start=c(1982,4))
autoplot(myts) # we see that there is a general trend going upwards
ggseasonplot(myts) # we see that during sep there is a sligt increase in the later years
ggsubseriesplot(myts) # overall increase in later months
gglagplot(myts)
ggAcf(myts)
Question 4: Create time plots of the following time series: bicoal,
chicken, dole, usdeaths, lynx, goog, writing, fancy, a10, h02.
Use help() to find out about the data in each series. For the goog plot, modify the axis labels and title.
help(bicoal)
help(chicken)
help(dole)
help(usdeaths)
help(lynx)
help(goog)
help(writing)
help(fancy)
help(a10)
help(h02)
autoplot(bicoal)
autoplot(chicken)
autoplot(dole)
autoplot(usdeaths)
autoplot(lynx)
autoplot(goog)
autoplot(writing)
autoplot(fancy)
autoplot(a10)
autoplot(h02)
help(goog)
autoplot(goog) +
ggtitle("Daily closing stock prices of Google Inc updated") +
xlab("Time updated") +
ylab("goog updated")
Question 5: Use the ggseasonplot() and ggsubseriesplot() functions to
explore the seasonal patterns in the following time series: writing,
fancy, a10, h02.
What can you say about the seasonal patterns? Can you identify any unusual years?
ggseasonplot(writing)
ggsubseriesplot(writing)# in august there is a drastic decrese in writing
ggseasonplot(fancy)
ggsubseriesplot(fancy) # starting from year 1992 there was a drastic increase
ggseasonplot(a10)
ggsubseriesplot(a10) # from jan to feb there is always a decrease
ggseasonplot(h02)
ggsubseriesplot(h02)# from jan to feb there is always a decrease
Question 8: The following time plots and ACF plots correspond to four different time series. Your task is to match each time plot in the first row with one of the ACF plots in the second row.
autoplot(cowtemp)
ggAcf(cowtemp)
#1-B
autoplot(usdeaths)
ggAcf(usdeaths)
#2-A
autoplot(AirPassengers)
ggAcf(AirPassengers)
#3_D
autoplot(mink)
ggAcf(mink)
#4-C