Differencing unemployment
In addition to adding lags to your data, you may find it helpful to generate a difference of the series.
To calculate a difference, simply use the diff() command. This command requires you to specify the original data object, the number of lags (lag
), and the order of the difference (differences
).
In this exercise, you'll expand your unemployment
data in a different direction by adding a few useful difference measures.
Cet exercice fait partie du cours
Case Study: Analyzing City Time Series Data in R
Instructions
- Construct a first order monthly difference in US unemployment using
diff()
. In your call todiff()
, specify the column you are drawing from inunemployment
as well as thelag
anddifferences
arguments. Rather than saving this to a new object for merging, save your data into a new column inunemployment
calledus_monthlydiff
. - Use a similar call to
diff()
to construct an annual difference in US unemployment. Save this tounemployment$us_yearlydiff
. - Use two calls to
plot.xts()
to generate plots of US unemployment (unemployment$us
) and annual change (unemployment$us_yearlydiff
), respectively. Leave thetype
argument as is in your second call toplot.xts()
to produce a barplot. The pre-writtenpar()
command allows you to view both plots at the same time.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Generate monthly difference in unemployment
unemployment$us_monthlydiff <- diff(___$___, lag = ___, differences = ___)
# Generate yearly difference in unemployment
unemployment$us_yearlydiff <-
# Plot US unemployment and annual difference
par(mfrow = c(2,1))
plot.xts(___)
plot.xts(___, type = "h")