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.
Deze oefening maakt deel uit van de cursus
Case Study: Analyzing City Time Series Data in R
Oefeninstructies
- Construct a first order monthly difference in US unemployment using
diff(). In your call todiff(), specify the column you are drawing from inunemploymentas well as thelaganddifferencesarguments. Rather than saving this to a new object for merging, save your data into a new column inunemploymentcalledus_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 thetypeargument 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.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# 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")