stevecondylios / priceR

Economics and Pricing in R
https://stevecondylios.github.io/priceR/
Other
59 stars 7 forks source link

adjust_for_inflation not suitable for deflating GDP? #25

Open turbanisch opened 3 years ago

turbanisch commented 3 years ago

I was trying to use priceR::adjust_for_inflation to convert US GDP values from current USD to constant 2010 USD. However, comparison with World Bank data shows that my results are way off. I wonder if this is an issue with the function or just me applying it in a way I shouldn't? I was able to manually deflate the GDP values though.

For reference, I downloaded the US GDP series in both current and constant 2010 USD from WDI. The World Bank uses GDP deflators rather than the CPI so I download both as well. Focusing on the US should eliminate any exchange rate complications.

library(tidyverse)
library(priceR)
# download data from World Bank
wdi <- WDI::WDI(
  country = "US",
  indicator = c("constant" = "NY.GDP.MKTP.KD", 
                "current" = "NY.GDP.MKTP.CD",
                "gdp_deflator" = "NY.GDP.DEFL.KD.ZG",
                "cpi_deflator" = "FP.CPI.TOTL.ZG"
  ),
  start = 1970,
  end = 2017) %>% 
  select(!iso2c:country) %>% 
  as_tibble() %>% 
  haven::zap_label()

Naively applying priceR::adjust_for_inflation does not seem to work here:

wdi %>% 
  mutate(adjusted = adjust_for_inflation(current, 2010, "US", year)) %>% 
  pivot_longer(cols = c(constant, current, adjusted), names_to = "series", values_to = "value") %>% 
  ggplot() +
    geom_line(aes(year, value, color = series))

naive

I then tried to exchange the inflation_dataframe (which apparently uses CPI) with GDP deflators but the results are almost unchanged:

# retrieve inflation data from adjust_for_inflation to modify 
inflation_dataframe <- retrieve_inflation_data("US")

# merge gdp deflator
df <- inflation_dataframe %>% 
  mutate(date = as.integer(date)) %>% 
  select(!value) %>%
  left_join(wdi %>% select(year, gdp_deflator), by = c("date" = "year")) %>% 
  rename(value = gdp_deflator)

# convert to 2010 dollars using both CPI and GDP deflators
wdi_adj <- wdi %>% 
  mutate(cpi_deflated = adjust_for_inflation(current, 2010, "US", year),
         gdp_deflated = adjust_for_inflation(current, 2010, "US", year, inflation_dataframe = df))

# plot
wdi_adj %>% 
  pivot_longer(cols = c(constant, current, cpi_deflated, gdp_deflated), names_to = "series", values_to = "value") %>% 
  ggplot(aes(year, value, color = series)) +
    geom_line()

gdp_deflated

Any help is greatly appreciated!