Bắt đầu ngayBắt đầu miễn phí

Add a continuous rolling average to unemployment data

In addition to discrete measures such as year-to-date sums, you may be interested in adding a rolling sum or average to your time series data.

To do so, let's return to your monthly unemployment data. While you may be interested in static levels of unemployment in any given month, a broader picture of the economic environment might call for rolling indicators over several months.

To do this, you'll use the rollapply() command, which takes a time series object, a window size width, and a FUN argument to apply to each rolling window.

Bài tập này là một phần của khóa học

Case Study: Analyzing City Time Series Data in R

Xem khóa học

Hướng dẫn bài tập

  • Use rollapply() to calculate the rolling yearly average US unemployment. Be sure to specify the us column of your unemployment data, set the width argument to the appropriate number of monthly periods, and set the FUN argument to mean. Save your rolling average into your unemployment object as year_avg.
  • Plot your two indicators of US unemployment (us and year_avg) using plot.zoo(). Set the plot.type argument to "single" to place both measures in the same panel.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# Use rollapply to calculate the rolling yearly average US unemployment
unemployment$year_avg <- rollapply(___$___, width = ___, FUN = ___)

# Plot all columns of US unemployment data
plot.zoo(unemployment[, c("___", "___")], plot.type = "___", lty = lty, lwd = lwd)
Chỉnh sửa và Chạy Mã