How Could I Find The Growth Rate of Gdp

How could I find the growth rate of GDP

You can also use the lag() fuction from dplyr. It gives the previous values in a vector. Here is an example

data <- data.frame(year = c(2003:2013),
gdp = c(7696034.9, 8690254.3, 9424601.9, 10520792.8,
11399472.2, 12256863.6, 12072541.6, 13266857.9,
14527336.9, 15599270.7, 16078959.8))
library(dplyr)
growth_rate <- function(x)(x/lag(x)-1)*100
data$growth_rate <- growth_rate(data$gdp)

How do I create a an annualized growth rate for GDP column in my dataframe?

You can use shift to access the previous row and vectorial operations for subtraction, division, power, multiplication:

df['Annualized_Growth_Rate'] = df['GDP'].div(df['GDP'].shift()).pow(4).sub(1).mul(100)

Output:

   Index  GDP  Annualized_Growth_Rate
0 0 100 NaN
1 1 101 4.060401
2 2 103 8.159184
3 3 107 16.462528

How to compute the growth rate of a variable for a varying time horizon in a panel?

Are you looking for something like this?

library(dplyr)

data <- data.frame(
state = rep(c('Aguascalientes', 'Jalisco'), each = 10),
year = 2011:2020,
population = 91:100,
gdp = 100:109
)

data
#> state year population gdp
#> 1 Aguascalientes 2011 91 100
#> 2 Aguascalientes 2012 92 101
#> 3 Aguascalientes 2013 93 102
#> 4 Aguascalientes 2014 94 103
#> 5 Aguascalientes 2015 95 104
#> 6 Aguascalientes 2016 96 105
#> 7 Aguascalientes 2017 97 106
#> 8 Aguascalientes 2018 98 107
#> 9 Aguascalientes 2019 99 108
#> 10 Aguascalientes 2020 100 109
#> 11 Jalisco 2011 91 100
#> 12 Jalisco 2012 92 101
#> 13 Jalisco 2013 93 102
#> 14 Jalisco 2014 94 103
#> 15 Jalisco 2015 95 104
#> 16 Jalisco 2016 96 105
#> 17 Jalisco 2017 97 106
#> 18 Jalisco 2018 98 107
#> 19 Jalisco 2019 99 108
#> 20 Jalisco 2020 100 109

data %>%
group_by(state) %>%
mutate(
across(c(gdp, population), ~(.[year == 2020] - .) / ., .names = '{col}_%change')
)
#> # A tibble: 20 x 6
#> # Groups: state [2]
#> state year population gdp `gdp_%change` `population_%change`
#> <chr> <int> <int> <int> <dbl> <dbl>
#> 1 Aguascalientes 2011 91 100 0.09 0.0989
#> 2 Aguascalientes 2012 92 101 0.0792 0.0870
#> 3 Aguascalientes 2013 93 102 0.0686 0.0753
#> 4 Aguascalientes 2014 94 103 0.0583 0.0638
#> 5 Aguascalientes 2015 95 104 0.0481 0.0526
#> 6 Aguascalientes 2016 96 105 0.0381 0.0417
#> 7 Aguascalientes 2017 97 106 0.0283 0.0309
#> 8 Aguascalientes 2018 98 107 0.0187 0.0204
#> 9 Aguascalientes 2019 99 108 0.00926 0.0101
#> 10 Aguascalientes 2020 100 109 0 0
#> 11 Jalisco 2011 91 100 0.09 0.0989
#> 12 Jalisco 2012 92 101 0.0792 0.0870
#> 13 Jalisco 2013 93 102 0.0686 0.0753
#> 14 Jalisco 2014 94 103 0.0583 0.0638
#> 15 Jalisco 2015 95 104 0.0481 0.0526
#> 16 Jalisco 2016 96 105 0.0381 0.0417
#> 17 Jalisco 2017 97 106 0.0283 0.0309
#> 18 Jalisco 2018 98 107 0.0187 0.0204
#> 19 Jalisco 2019 99 108 0.00926 0.0101
#> 20 Jalisco 2020 100 109 0 0

Created on 2022-03-28 by the reprex package (v2.0.1)

How to create a function and a loop to calculate growth rates of variables in a data frame in R

You can use data.table too. data.table is a very powerful data manipulation package. You can get started here.

library("data.table")
as.data.table(testdata)[, lapply(.SD, function(x)x/shift(x) - 1), .SDcols = 2:4]

gdp cpi_index rpi_index
1: NA NA NA
2: 0.006427064 0.0072281257 0.009296686
3: 0.007166400 0.0030245056 0.004805767
4: 0.004061822 0.0061020008 0.006377043
5: 0.006772674 0.0009282349 0.005544554
6: 0.007517419 0.0070574736 0.008270973
7: 0.006551699 0.0004058093 0.003515625
8: 0.005457465 0.0009439040 0.001946283

How to use growth rates with apply function instead of a loop in R

You do not need any apply() function in this case. This is just a simple geometric progression:

# simulate some data
country <- data.frame(year = 2000:2017,
GDP = rep(100, 18),
Population = rep(1000, 18))

country[19:23,1] <- seq(2018, by=1, length.out = 5)

# define gdp and pop parameters
hypo_gdp = 5
hypo_pop = 2

# define common ratios
b1 <- (1 + hypo_gdp/100)
b2 <- (1 + hypo_pop/100)

# calculate values for years 2018-2022
country[19:23,2] = country$GDP[18]* b1 ^ (1:5)
country[19:23,3] = country$Population[18]* b2 ^ (1:5)
tail(country)
# year GDP Population
# 18 2017 100.0000 1000.000
# 19 2018 105.0000 1020.000
# 20 2019 110.2500 1040.400
# 21 2020 115.7625 1061.208
# 22 2021 121.5506 1082.432
# 23 2022 127.6282 1104.081


Related Topics



Leave a reply



Submit