Adrian
Adrian

Reputation: 843

Calculating price adjusted variables when base year is not in the beginning

I have a large dataset that has variables including country, year, GDP and inflation. I would like to recalculate GDP adjusting for prices for each year, given a base year.

Say I have the following object:

library(tidyverse)

# Sample data for nominal GDP and inflation rates for two countries
set.seed(123) # For reproducibility
years <- 2008:2024
countries <- c("Country_A", "Country_B")

# Create a data frame with inflation and nominal GDP data
data <- expand.grid(year = years, country = countries) %>%
  mutate(
    # Generate example nominal GDP values
    GDP = ifelse(country == "Country_A",
                 100 * (1 + 0.05)^(year - 2008), # Starting from 100 for Country A
                 200 * (1 + 0.04)^(year - 2008)), # Starting from 200 for Country B
    # Generate example inflation rates
    inflation = ifelse(country == "Country_A",
                       runif(length(years), 0.01, 0.03), # Random inflation rates for Country A
                       runif(length(years), 0.015, 0.035)) # Random inflation rates for Country B
  )

Giving:

   year   country      GDP  inflation
1  2008 Country_A 100.0000 0.01575155
2  2009 Country_A 105.0000 0.02576610
3  2010 Country_A 110.2500 0.01817954
4  2011 Country_A 115.7625 0.02766035
5  2012 Country_A 121.5506 0.02880935
6  2013 Country_A 127.6282 0.01091113
7  2014 Country_A 134.0096 0.02056211
8  2015 Country_A 140.7100 0.02784838
9  2016 Country_A 147.7455 0.02102870
10 2017 Country_A 155.1328 0.01913229
11 2018 Country_A 162.8895 0.02913667
12 2019 Country_A 171.0339 0.01906668
13 2020 Country_A 179.5856 0.02355141
14 2021 Country_A 188.5649 0.02145267
15 2022 Country_A 197.9932 0.01205849
16 2023 Country_A 207.8928 0.02799650
17 2024 Country_A 218.2875 0.01492175
18 2008 Country_B 200.0000 0.01584119
19 2009 Country_B 208.0000 0.02155841
20 2010 Country_B 216.3200 0.03409007
21 2011 Country_B 224.9728 0.03279079
22 2012 Country_B 233.9717 0.02885607
23 2013 Country_B 243.3306 0.02781014
24 2014 Country_B 253.0638 0.03488540
25 2015 Country_B 263.1864 0.02811412
26 2016 Country_B 273.7138 0.02917061
27 2017 Country_B 284.6624 0.02588132
28 2018 Country_B 296.0489 0.02688284
29 2019 Country_B 307.8908 0.02078319
30 2020 Country_B 320.2064 0.01794227
31 2021 Country_B 333.0147 0.03426048
32 2022 Country_B 346.3353 0.03304598
33 2023 Country_B 360.1887 0.02881411
34 2024 Country_B 374.5962 0.03090935

I would like to recalculate the price-adjusted GDP using 2022 as the base year. I have tried using:

base_year <- 2022

data_adjusted <- inflation %>%
  group_by(country) %>%
  mutate(
    # Calculate the cumulative inflation factor relative to the base year
    base_inflation = inflation[year == base_year],
    cumulative_factor = base_inflation / inflation, # Cumulative factor for 2020 values
    `Adjusted GDP` = GDP * cumulative_factor # Adjust GVA for inflation
  ) %>%
  ungroup() # Remove grouping to return to a regular data frame

but the code gives considerable problems if inflation is zero or negative. Is there a way to do this efficiently for any value of inflation (zero or negative)?

Upvotes: 0

Views: 29

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

Here, I define a price level where 100 = starting value for the initial year for each country. Then the price level increases based on prior years' cumulative compounding inflation. The adjusted GDP relates the current price level to the reference price level.

I am interpreting inflation here to reflect the inflation rate over the course of the year, so it drives the price level for the next year.

base_year <- 2022
    
data |>
  mutate(start_price_level = lag(cumprod(1 + inflation), 1, 1), 
         GDP_adj = GDP * start_price_level[year == base_year] / start_price_level,
         .by = country)

Result

   year   country      GDP  inflation start_price_level  GDP_adj
1  2008 Country_A 100.0000 0.01575155          1.000000 135.7058 # inflated up
2  2009 Country_A 105.0000 0.02576610          1.015752 140.2814
3  2010 Country_A 110.2500 0.01817954          1.041924 143.5956
4  2011 Country_A 115.7625 0.02766035          1.060865 148.0833
5  2012 Country_A 121.5506 0.02880935          1.090209 151.3023
6  2013 Country_A 127.6282 0.01091113          1.121617 154.4188
7  2014 Country_A 134.0096 0.02056211          1.133855 160.3897
8  2015 Country_A 140.7100 0.02784838          1.157170 165.0161
9  2016 Country_A 147.7455 0.02102870          1.189395 168.5724
10 2017 Country_A 155.1328 0.01913229          1.214407 173.3556
11 2018 Country_A 162.8895 0.02913667          1.237641 178.6062
12 2019 Country_A 171.0339 0.01906668          1.273702 182.2270
13 2020 Country_A 179.5856 0.02355141          1.297987 187.7585
14 2021 Country_A 188.5649 0.02145267          1.328556 192.6101 # 188.5649 * 1.02145267
15 2022 Country_A 197.9932 0.01205849          1.357058 197.9932 # matches
16 2023 Country_A 207.8928 0.02799650          1.373422 205.4158
17 2024 Country_A 218.2875 0.01492175          1.411873 209.8126
18 2008 Country_B 200.0000 0.01584119          1.000000 290.5914
19 2009 Country_B 208.0000 0.02155841          1.015841 297.5023
20 2010 Country_B 216.3200 0.03409007          1.037741 302.8729
...

Upvotes: 0

Related Questions