如何根据 R 中的数据集计算 inflation 率和失业率

How can I calculate inflation rate and unemployment rate from this data set in R

这是我的数据集的头部。

 Year Months   CPI  UEL   CLF
1 1948      1 23.68 2034 60230
2 1948      2 23.67 2328    NA
3 1948      3 23.50 2399    NA
4 1948      4 23.82 2386 60535
5 1948      5 24.01 2118    NA
6 1948      6 24.15 2214    NA

CPI代表“消费者物价指数”,UEL是“失业率”,CLF是“平民劳动力”。这些数据介于 1948 年和 2016 年之间。我只得到了每年第 1、4、7 和 10 个月的平民劳动力数据。我需要根据这些变量计算变量 Inflation 比率和失业率,但我不确定如何在 RStudio 中执行此操作。

实际上,您的问题似乎与 ECONOMICS 有关,而不是在 r 中编程。您必须首先决定在数据 (CLF) 中计算 NAs 的策略,可以是

  • 同上
  • increasing/decreasing按比例

之后你可以在 r.

中计算 unemployment rate

要计算 inflation,您可以使用 dplyr 中的 lag/lead。

如果您只想用以前的值填充空白,这样的方法会起作用。

library(tidyverse)

df %>% fill(CLF, .direction = "down") %>%
  mutate(inflation = paste0(formatC((CPI - lag(CPI))*100/lag(CPI), digits = 2), "%"),
         unemployment_rate = paste0(formatC(UEL*100/CLF, digits = 2), "%"))

  Year Months   CPI  UEL   CLF inflation unemployment_rate
1 1948      1 23.68 2034 60230       NA%              3.4%
2 1948      2 23.67 2328 60230   -0.042%              3.9%
3 1948      3 23.50 2399 60230    -0.72%                4%
4 1948      4 23.82 2386 60535      1.4%              3.9%
5 1948      5 24.01 2118 60535      0.8%              3.5%
6 1948      6 24.15 2214 60535     0.58%              3.7%

如果您想要不带格式的结果 percentages

df %>% fill(CLF, .direction = "down") %>%
  mutate(inflation = (CPI - lag(CPI))*100/lag(CPI),
         unemployment_rate = UEL*100/CLF)

  Year Months   CPI  UEL   CLF   inflation unemployment_rate
1 1948      1 23.68 2034 60230          NA          3.377055
2 1948      2 23.67 2328 60230 -0.04222973          3.865183
3 1948      3 23.50 2399 60230 -0.71820870          3.983065
4 1948      4 23.82 2386 60535  1.36170213          3.941521
5 1948      5 24.01 2118 60535  0.79764903          3.498802
6 1948      6 24.15 2214 60535  0.58309038          3.657388