CommencerCommencer gratuitement

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

Afficher le cours

Instructions

  • Construct a first order monthly difference in US unemployment using diff(). In your call to diff(), specify the column you are drawing from in unemployment as well as the lag and differences arguments. Rather than saving this to a new object for merging, save your data into a new column in unemployment called us_monthlydiff.
  • Use a similar call to diff() to construct an annual difference in US unemployment. Save this to unemployment$us_yearlydiff.
  • Use two calls to plot.xts() to generate plots of US unemployment (unemployment$us) and annual change (unemployment$us_yearlydiff), respectively. Leave the type argument as is in your second call to plot.xts() to produce a barplot. The pre-written par() 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")
Modifier et exécuter le code