Search code examples
ranalysis

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


Here is the head of my dataset.

 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 stands for "Consumer Price Index", UEL is the "Unemployment Level", and CLF is the "Civilian Labor Force". The data is between the years 1948 and 2016. I was only given the Civilian Labor Force figures for months 1, 4, 7, and 10 of each year. I need to calculate the variables Inflation Rate and Unemployment Rate from these variables but I'm not sure how to do that in RStudio.


Solution

  • Actually your problem seems with ECONOMICS rather than programming in r. You have to first decide about strategies to calculate NAs in data (CLF) which can be

    • same as previous
    • increasing/decreasing proportionately

    Thereafter you can calculate unemployment rate in r.

    For calculation of inflation you can use lag/lead from dplyr.

    Something like this will work, if you want to fill blanks with previous values only.

    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%
    

    If you want results without formatting as 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