Use the help function to explore what the series gold, woolyrnq and gas represent.
#help(data("gold"))
#help(data("woolyrnq"))
#help(data("gas"))
autoplot(gold) + ggtitle("Gold Series") +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(woolyrnq) + ggtitle("Woolyrnq Series") +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(gas) + ggtitle("Gas Series") +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
fgol <- frequency(gold)
fwoo <- frequency(woolyrnq)
fgas <- frequency(gas)
cat("Frequency of Gold series:", fgol, "\n")
## Frequency of Gold series: 1
cat("Frequency of Woolyrnq series:", fwoo, "\n")
## Frequency of Woolyrnq series: 4
cat("Frequency of Gas series:", fgas, "\n")
## Frequency of Gas series: 12
oi <- which.max(gold)
ov <- gold[oi]
# Print the result
cat("In the Gold time series, the", oi, "observation has a value of", ov, "and this value is considered an outlier.", "\n")
## In the Gold time series, the 770 observation has a value of 593.7 and this value is considered an outlier.
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)
autoplot(mytimeseries)
Check what happens when you don’t include facets=TRUE.
The Facet function uses categorical variables for graphs created with ggplot2 and draws subgraphs for each level of the variable in different panels.
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))
myts <- ts(retaildata[,"A3349397X"], 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?
# Explore the time series
autoplot(myts) + ggtitle("Retail Time Series")
# Seasonal plot
ggseasonplot(myts, year.labels=TRUE, year.labels.left=F) +
ggtitle("Seasonal Plot")
ggseasonplot(myts, polar=TRUE)+
geom_line(size=0.5)+
geom_point(size=1.5)+
theme_minimal() +
ggtitle("Polar seasonal plot")
# Subseries plot
ggsubseriesplot(myts) + ggtitle("Subseries Plot")
# Lag plot
gglagplot(myts, main = "Lag Plot")
# Autocorrelation plot
ggAcf(myts) + ggtitle("Autocorrelation Plot")
The price exhibits a noticeable upward trend over time, with November and December registering higher values than other months. Time series analysis is a method that involves examining data recorded over time. By scrutinizing values recorded at regular intervals, such as yearly, monthly, weekly, or even finer intervals like hour/minute/second, it becomes possible to analyze the patterns in measured values and predict their future movements.
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(data("bicoal"))
#help(data("chicken"))
#help(data("dole"))
#help(data("usdeaths))
#help(data("lynx"))
#help(data("goog"))
#help(data("writing"))
#help(data("a10"))
#help(data("fancy"))
#help(data("h02))
autoplot(bicoal) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(chicken) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(dole) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(usdeaths) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(lynx) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(goog) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(fancy) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(a10) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(h02) +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white"))
autoplot(goog) +
labs(title = "Google Stock Prices", x = "Time", y = "Stock Price ($)") +
theme(axis.text.x = element_text(angle = 0, hjust = 1),
panel.background = element_rect(fill = "white")) +
geom_point(shape = 21, size = 0.07, color = "grey25", fill = "yellow") +
geom_line(color = "grey25")
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?
Generally, as the years progress, the values tend to increase. Specifically focusing on the a10 and h02 datasets, there is a gap in the data after June of 2008. Unlike other datasets, the h02 data doesn’t consistently rise in value merely because the year advances from the 2000s. It exhibits a unique pattern compared to the rest.
#ggseasonplot(writing, year.labels=TRUE, year.labels.left=F) +
#labs(title = "Seasonal Plot")
ggseasonplot(writing, year.labels=TRUE, year.labels.left=F,
col=nord(palette='aurora'))+
geom_line(size=0.08 )+
geom_point(shape=21, size=2, fill='white', stroke=1.5)+
theme_minimal() +
labs(title = "Seasonal Plot")
ggsubseriesplot(writing) +
labs(title = "Subseries Plot")
ggseasonplot(fancy, year.labels=TRUE, year.labels.left=F,
col=nord(palette='aurora'))+
geom_line(size=0.08 )+
geom_point(shape=21, size=2, fill='white', stroke=1.5)+
theme_minimal() +
labs(title = "Seasonal Plot")
ggsubseriesplot(fancy) +
labs(title = "Subseries Plot")
ggseasonplot(a10, year.labels=TRUE, year.labels.left=F,
col=nord(palette='aurora'))+
geom_line(size=0.08 )+
geom_point(shape=21, size=2, fill='white', stroke=1.5)+
theme_minimal() +
labs(title = "Seasonal Plot")
ggsubseriesplot(a10) +
labs(title = "Subseries Plot")
ggseasonplot(h02, year.labels=TRUE, year.labels.left=F,
col=nord(palette='aurora'))+
geom_line(size=0.08 )+
geom_point(shape=21, size=2, fill='white', stroke=1.5)+
theme_minimal() +
labs(title = "Seasonal Plot")
ggsubseriesplot(h02) +
labs(title = "Subseries Plot")
str(h02)
## Time-Series [1:204] from 1992 to 2008: 0.43 0.401 0.432 0.493 0.502 ...
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.
1-B, 2-A, 3-D, 4-C