开始使用免费开始使用

失业率的差分

除了为数据添加滞后项,生成该序列的差分也常常很有用。

要计算差分,直接使用 diff() 命令。该命令需要您指定原始数据对象、滞后期数(lag)以及差分阶数(differences)。

在本练习中,您将从另一个角度扩展 unemployment 数据,添加几个有用的差分度量。

本练习是课程的一部分

案例研究:在 R 中分析城市时间序列数据

查看课程

练习说明

  • 使用 diff() 构造美国失业率的一阶月度差分。在调用 diff() 时,请在 unemployment 中指定取值的列,并同时设置 lagdifferences 参数。不要另存为用于合并的新对象,而是将结果保存为 unemployment 中名为 us_monthlydiff 的新列。
  • 用类似的 diff() 调用构造美国失业率的「年度」差分。将结果保存到 unemployment$us_yearlydiff
  • 分别调用两次 plot.xts(),绘制美国失业率(unemployment$us)与年度变化(unemployment$us_yearlydiff)。在第二次调用 plot.xts() 时,保持 type 参数不变以生成柱状图。预先编写的 par() 命令可让您同时查看两幅图。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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")
编辑并运行代码