-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrp_wbpip.R
204 lines (158 loc) · 6.66 KB
/
wrp_wbpip.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Project: Wrappers for wbpip functions
# Author: Diana C. Garcia Rojas
# Dependencies: The World Bank
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Creation Date: June 2024
# References:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## 1. wrp_md_dist_stats -----
##
## Objective: Wrapper for wbpip function `wbpip::md_compute_dist_stats`
## (Diana comment: If this works it could be implemented in wbpip)
wrp_md_dist_stats <- function(welfare,
weight,
mean = NULL,
nbins = 10,
lorenz = NULL,
n_quantile = 10) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# computations ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (is.null(mean)) {
mean <- fmean(x = welfare,
w = weight)
}
if (is.null(lorenz)) {
lorenz <- wbpip:::md_compute_lorenz(welfare = welfare,
weight = weight,
nbins = nbins)
}
share_quant <- wbpip:::md_compute_quantiles_share(welfare = welfare,
weight = weight,
n_quantile = n_quantile,
lorenz = lorenz)
names(share_quant) <- paste0("decile",1:n_quantile)
median <- wbpip:::md_compute_median(welfare = welfare,
weight = weight,
lorenz = lorenz)
gini <- wbpip:::md_compute_gini(welfare = welfare,
weight = weight)
mld <- wbpip:::md_compute_mld(welfare = welfare,
weight = weight,
mean = mean)
polarization <- wbpip:::md_compute_polarization(welfare = welfare,
weight = weight,
gini = gini,
mean = mean,
median = median)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Return ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return(c(list(
mean = mean,
median = median,
gini = gini,
polarization = polarization,
mld = mld),
share_quant))
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## 2. wrp_gd_dist_stats -----
##
## Objective: Wrapper for wbpip function `wbpip::gd_compute_dist_stats`
## (Diana comment: If this works it could be implemented in wbpip)
wrp_gd_dist_stats <- function(welfare,
population,
mean,
p0 = 0.5) {
# Apply Lorenz quadratic fit ----------------------------------------------
# STEP 1: Prep data to fit functional form
prepped_data <- wbpip::create_functional_form_lq(
welfare = welfare, population = population
)
# STEP 2: Estimate regression coefficients using LQ parameterization
reg_results_lq <- wbpip::regres(prepped_data, is_lq = TRUE)
A <- reg_results_lq$coef[1]
B <- reg_results_lq$coef[2]
C <- reg_results_lq$coef[3]
kv <- wbpip:::gd_lq_key_values(A, B, C)
# STEP 3: Compute Sum of Squared Error
reg_results_lq[["sse"]] <- wbpip:::gd_compute_dist_fit_lq(welfare = welfare,
population = population,
A = A,
B = B,
C = C,
key_values = kv)
# STEP 3: Calculate distributional stats
# Compute key numbers from Lorenz quadratic form
results_lq <- wbpip:::gd_estimate_dist_stats_lq(mean = mean,
p0 = p0,
A = A,
B = B,
C = C,
key_values = kv)
results_lq <- append(results_lq, reg_results_lq)
# Apply Lorenz beta fit ---------------------------------------------------
# STEP 1: Prep data to fit functional form
prepped_data <- wbpip::create_functional_form_lb(
welfare = welfare, population = population
)
# STEP 2: Estimate regression coefficients using LB parameterization
reg_results_lb <- wbpip::regres(prepped_data, is_lq = FALSE)
A <- reg_results_lb$coef[1]
B <- reg_results_lb$coef[2]
C <- reg_results_lb$coef[3]
# STEP 3: Compute Sum of Squared Error
reg_results_lb[["sse"]] <- wbpip:::gd_compute_dist_fit_lb(welfare = welfare,
population = population,
A = A,
B = B,
C = C)
# STEP 3: Calculate distributional stats
results_lb <- wbpip:::gd_estimate_dist_stats_lb(mean = mean,
p0 = p0,
A = A,
B = B,
C = C)
results_lb <- append(results_lb, reg_results_lb)
# Apply selection rules -----------------------------------------------
# STEP 4: Select best fit
out <- wbpip:::gd_select_lorenz_dist(
lq = results_lq, lb = results_lb
)
# Deciles change -------
# deciles <- unlist2d(out["deciles"], idcols = FALSE)
# names(deciles) <- paste0("decile",1:10)
#
# deciles <- unlist(out["deciles"])
# Return only subset of variables
out <- c(out[c(
"mean",
"median",
"gini",
"mld",
"polarization"
)], unlist(out["deciles"]))
return(out)
}
## 2.1 safe wrp_gd_dist_stats ----
# Safe GD estimation
safe_wrp_gd_dist_stats <- function(welfare, population, mean, cache_id) {
tryCatch(
# Run function:
expr = {
res <- wrp_gd_dist_stats(welfare = welfare,
population = population,
mean = mean)
# Return if it works
return(res)
},
# If error:
error = function(e) {
rlang::warn("Distributional statistics caluclation failed. Returning NULL.")
return(NULL)
}
)
}