Time series plots
The first step in any data analysis task is to plot the data. Graphs enable you to visualize many features of the data, including patterns, unusual observations, changes over time, and relationships between variables. Just as the type of data determines which forecasting method to use, it also determines which graphs are appropriate.
You can use the autoplot()
function to produce a time plot of the data with or without facets, or panels that display different subsets of data:
> autoplot(usnim_2002, facets = FALSE)
The above method is one of the many taught in this course that accepts boolean arguments. Both T
and TRUE
mean "true", and F
and FALSE
mean "false", however, T
and F
can be overwritten in your code. Therefore, you should only rely on TRUE
and FALSE
to set your indicators for the remainder of the course.
You will use two more functions in this exercise, which.max()
and frequency()
.
which.max()
can be used to identify the smallest index of the maximum value
> x <- c(4, 5, 5)
> which.max(x)
[1] 2
To find the number of observations per unit time, use frequency()
. Recall the usnim_2002
data from the previous exercise:
> frequency(usnim_2002)
[1] 4
Because this course involves the use of the forecast
and ggplot2
packages, they have been loaded into your workspace for you, as well as myts
from the previous exercise and the following three series (available in the package forecast
):
gold
containing gold prices in US dollarswoolyrnq
containing information on the production of woollen yarn in Australiagas
containing Australian gas production
This exercise is part of the course
Forecasting in R
Exercise instructions
- Plot the data you stored as
myts
usingautoplot()
with facetting. - Plot the same data without facetting by setting the appropriate argument to
FALSE
. What happens? - Plot the
gold
,woolyrnq
, andgas
time series in separate plots. - Use
which.max()
to spot the outlier in thegold
series. Which observation was it? - Apply the
frequency()
function to each commodity to get the number of observations per unit time. This would return 52 for weekly data, for example.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Plot the data with facetting
autoplot(___, facets = ___)
# Plot the data without facetting
___
# Plot the three series
autoplot(___)
___
___
# Find the outlier in the gold series
goldoutlier <- ___(___)
# Look at the seasonal frequencies of the three series
frequency(___)
___
___