-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpindex.Rmd
615 lines (474 loc) · 15.6 KB
/
mpindex.Rmd
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
---
title: "Introduction to mpindex"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to mpindex}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
This guide presents an illustration of a simple application of `mpindex` package for computing Multidimensional Poverty Index (MPI) using the Alkire-Foster (AF) counting method developed by Sabina Alkire and James Foster.
## 1. Installation
To install the `mpindex` package from CRAN:
```{r, eval=F}
install.packages('mpindex')
```
If you want to get the latest development version of `mpindex`, install it from [GitHub](https://github.com/yng-me/mpindex). Note that you may need to install `devtools`.
```{r, eval=F}
# install.packages("devtools")
devtools::install_github('yng-me/mpindex')
```
Load the package once you have successfully completed the installation.
```{r setup}
library(mpindex)
```
## 2. MPI specifications
The initial step is to prepare an MPI specification file which will serve as references in the computation as well as generation of output in the later part of the process. This file should contain information about MPI dimensions, indicators and their corresponding weights.
This file should also be easy to create using the most common and accessible file types such as `.xlsx` (Excel), `.json`, `.csv`, or `.txt` (TSV).
### Built-in specification files
For convenience, `mpindex` has included built-in specification files (in different formats). Each file contains dimensions, indicators, weight, and other relevant information of the Global MPI.
To see the list of files available:
```{r}
system.file("extdata", package = "mpindex") |> list.files()
```
To use a built-in specification file, say the `.csv` file, use below script to first get the full path of the file.
```{r}
specs_file <- system.file("extdata", "global-mpi-specs.csv", package = "mpindex")
```
```{r, echo=F}
read.csv(specs_file) |>
gt::gt() |>
gt::tab_header(
title = 'Global MPI – Dimensions, Indicators, Deprivation Cutoffs, and Weights'
) |>
gt::tab_options(
table.width = '100%',
table.font.size = 12,
) |>
gt::tab_footnote('Source: Alkire, S., Kanagaratnam, U. and Suppa, N. (2020). ‘The global Multidimensional Poverty Index (MPI): 2020 revision’, OPHI MPI Methodological Note 49, Oxford Poverty and Human Development Initiative, University of Oxford.') |>
gt::fmt_number(
columns = 4,
decimals = 3
)
```
### User-defined specification file
You can also define and create your own specification file if you prefer to or if you happen to use different sets of dimensions and indicators. At the minimum, however, this file should contain the following columns/variables:
- `Dimension`,
- `Indicator`,
- `Variable`,
- `Weight`,
- `Description` (optional).
Note that the order in which you put these columns does not matter and also the names are not case sensitive, but make sure to spell the column names correctly.
You may download the template here if you do not want to start from scratch: [MPI specification file sample](https://github.com/yng-me/mpindex/blob/main/inst/extdata/global-mpi-specs.csv).
### Using `define_mpi_specs`
Once you have prepared the specification file, load it using the `define_mpi_specs` function (type `?define_mpi_specs` for a help text).
For demonstration purposes, we will use the built-in specification file, discussed above.
```{r}
specs_file <- system.file("extdata", "global-mpi-specs.csv", package = "mpindex")
define_mpi_specs(specs_file)
```
`define_mpi_specs` requires a specification file as its first argument. The default poverty cutoff is set to `1/3` (based on Global MPI). You can also define a list of poverty cutoffs by specifying in the `.poverty_cutoffs` argument to achieve gradient MPIs.
```{r, eval=F}
define_mpi_specs(
.mpi_specs_file = specs_file,
.poverty_cutoffs = c(1/3, 0.2, 0.8)
)
```
If your dataset contains unique ID, like `uuid`, it is recommended to define it here using the `.uid` argument.
```{r, eval=F}
define_mpi_specs(
.mpi_specs_file = specs_file,
.uid = 'uuid'
)
```
You can also set the aggregation level. Make sure it corresponds to the column name present in your dataset (see `?df_househod` and more below).
```{r, eval=F}
define_mpi_specs(
.mpi_specs_file = specs_file,
.poverty_cutoffs = c(1/3, 0.2, 0.8),
.uid = 'uuid',
.aggregation = 'class'
)
```
`.unit_of_analysis`, `.source_of_data`, and `.names_separator` are merely used for auto labels when generating the output later.
Note: `define_mpi_specs` returns a data frame of MPI specs defined in the specs file. By default, it saves a global option named `mpi_specs` which can be accessed using `getOption('mpi_specs')`.
For our demonstration, we will use the `use_global_mpi_specs()` wrapper function, which yields the same result as calling `define_mpi_specs` and passing the built-in specs file.
```{r, echo=F, include=F}
use_global_mpi_specs(
.uid = 'uuid',
.aggregation = 'class'
)
```
```{r, eval=F}
use_global_mpi_specs(
.uid = 'uuid',
.aggregation = 'class'
)
```
## 3. Data preparation
The user of `mpindex` is assumed to have basic familiarity with the concept of tidy data as well as able to perform data wrangling and transformation using the `tidyverse` ecosystem. Under the hood, `mpindex` uses `dplyr` verbs to perform data manipulation.
We also assume that your dataset is already tidy and ready for analysis. See [R for Data Science](https://r4ds.had.co.nz/tidy-data.html) by Hadley Wickham and Garrett Grolemund if you need a refresher.
### Dataset
For this demonstration, we will use two (2) synthetic datasets available within the package:
- `df_household` household-level data (type `?df_household` for more info)
- `df_household_roster` individual-level data (type `?df_household_roster` for more info)
```{r, warning=F, message=F}
library(dplyr)
glimpse(df_household)
```
```{r}
glimpse(df_household_roster)
```
## 4. Creating deprivation profile
### Using `define_deprivation`
First, we need to create an empty list, and name it `deprivation_profile` (but feel free to name it whatever you like).
```{r}
deprivation_profile <- list()
```
To create a deprivation profile for each indicator, we use the `define_deprivation` function (see `?define_deprivation`) and add to the `deprivation_profile` list we created above. Make sure that the deprivation profile for each indicator matches the variable name declared in the specification file.
### 1. Heath dimension
#### 1.1. Nutrition
For this indicator, we use the `df_household_roster` dataset. By default, `define_deprivation` sets the `.collapse = FALSE`. Since we need to collapse it to the household level, we need to set `.collapse = TRUE`.
```{r}
deprivation_profile$nutrition <- df_household_roster |>
define_deprivation(
.indicator = nutrition,
.cutoff = undernourished == 1 & age < 70,
.collapse = TRUE
)
```
#### 1.2. Child mortality
For child mortality, we use the `df_household` dataset. But unlike in `nutrition`, we do not need to provide the `.collapse` argument since it is not applicable here.
```{r}
deprivation_profile$child_mortality <- df_household |>
define_deprivation(
.indicator = child_mortality,
.cutoff = with_child_died == 1
)
```
### 2. Education dimension
#### 2.1. Years of schooling
```{r}
deprivation_profile$year_schooling <- df_household_roster |>
define_deprivation(
.indicator = year_schooling,
.cutoff = completed_6yrs_schooling == 2,
.collapse = TRUE
)
```
#### 2.2. School attendance
```{r}
deprivation_profile$school_attendance <- df_household_roster |>
define_deprivation(
.indicator = school_attendance,
.cutoff = attending_school == 2 & age %in% c(5:24),
.collapse = TRUE
)
```
### 3. Living standards dimension
#### 3.1. Cooking fuel
```{r}
deprivation_profile$cooking_fuel <- df_household |>
define_deprivation(
.indicator = cooking_fuel,
.cutoff = cooking_fuel %in% c(4:6, 9)
)
```
#### 3.2. Sanitation
```{r}
deprivation_profile$sanitation <- df_household |>
define_deprivation(
.indicator = sanitation,
.cutoff = toilet > 1
)
```
#### 3.3. Drinking water
```{r}
deprivation_profile$drinking_water <- df_household |>
define_deprivation(
.indicator = drinking_water,
.cutoff = drinking_water == 2
)
```
#### 3.4. Electricity
```{r}
deprivation_profile$electricity <- df_household |>
define_deprivation(
.indicator = electricity,
.cutoff = electricity == 2
)
```
#### 3.5. Housing
```{r}
deprivation_profile$housing <- df_household |>
define_deprivation(
.indicator = housing,
.cutoff = roof %in% c(5, 7, 9) | walls %in% c(5, 8, 9, 99) == 2 | floor %in% c(5, 6, 9)
)
```
#### 3.6. Assets
For this indicator, we need additional transformation.
```{r}
deprivation_profile$assets <- df_household |>
mutate_at(vars(starts_with('asset_')), ~ if_else(. > 0, 1L, 0L)) |>
mutate(
asset_phone = if_else(
(asset_telephone + asset_mobile_phone) > 0,
1L,
0L
)
) |>
mutate(
with_hh_conveniences = (
asset_tv + asset_phone + asset_computer +
asset_animal_cart + asset_bicycle +
asset_motorcycle + asset_refrigerator) > 1,
with_mobility_assets = (asset_car + asset_truck) > 0
) |>
define_deprivation(
.indicator = assets,
.cutoff = !(with_hh_conveniences & with_mobility_assets)
)
```
## 5. Computing the MPI
### Using `compute_mpi`
After completing the deprivation profile, use the `compute_mpi` function and pass the `deprivation_profile` list as the first argument.
```{r}
mpi_result <- df_household |>
compute_mpi(deprivation_profile)
names(mpi_result)
```
### Outputs
#### 1. The MPI
```{r, eval=F}
mpi_result$index
```
```{r, echo=F}
mpi_result$index |>
rename(Class = 1) |>
gt::gt() |>
gt::tab_header(
title = 'MPI Results using 33% Poverty Cutoff'
) |>
gt::fmt_number(
columns = 3:5,
decimals = 3
) |>
gt::tab_options(
table.width = '100%',
table.font.size = 12,
)
```
#### 2. Contribution by dimension
```{r, eval=F}
mpi_result$contribution
```
```{r, echo=F}
gtx <- function(.gt, .decimals = 1, .offset = 0) {
d01_cp <- 3:4 + .offset
d02_cp <- 5:6 + .offset
d03_cp <- 7:12 + .offset
.gt |>
gt::tab_spanner(
label = "Health",
columns = d01_cp
) |>
gt::tab_spanner(
label = "Education",
columns = d02_cp
) |>
gt::tab_spanner(
label = "Living Standards",
columns = d03_cp
) |>
gt::fmt_number(
columns = c(d01_cp, d02_cp, d03_cp),
decimals = .decimals
) |>
gt::tab_options(
table.font.size = 12,
)
}
mpi_result$contribution |>
gt::gt() |>
gt::tab_header(
title = 'Contribution by Dimenstion and Indicator to MPI using 33% Poverty Cutoff'
) |>
gtx()
```
#### 3. Headcount ratio
```{r, eval=F}
mpi_result$headcount_ratio$uncensored
```
```{r, echo=F}
mpi_result$headcount_ratio$uncensored |>
ungroup() |>
gt::gt() |>
gt::tab_header(
title = 'Uncensored Headcount Ratio'
) |>
gtx(.decimals = 3)
```
```{r, eval=F}
mpi_result$headcount_ratio$censored
```
```{r, echo=F}
mpi_result$headcount_ratio$censored |>
ungroup() |>
gt::gt() |>
gt::tab_header(
title = 'Censored Headcount Ratio using 33% Poverty Cutoff'
) |>
gtx(.decimals = 3)
```
#### 4. Deprivation matrix (first 6 observations)
```{r, eval=F}
mpi_result$deprivation_matrix$uncensored |> head()
```
```{r, echo=F}
mpi_result$deprivation_matrix$uncensored |>
ungroup() |>
head() |>
rename_all(~ stringr::str_remove(., '^(Health|Education|Living Standards)>')) |>
gt::gt() |>
gt::tab_header(
title = 'Uncensored Deprivation Matrix '
) |>
gtx(.decimals = 0, .offset = 1) |>
gt::fmt_number(
columns = 3,
decimals = 3
)
```
```{r, eval=F}
mpi_result$deprivation_matrix$censored |> head()
```
```{r, echo=F}
mpi_result$deprivation_matrix$censored |>
ungroup() |>
head() |>
rename_all(~ stringr::str_remove(., '^(Health|Education|Living Standards)>')) |>
gt::gt() |>
gt::tab_header(
title = 'Censored Deprivation Matrix using 33% Poverty Cutoff'
) |>
gtx(.decimals = 0, .offset = 1) |>
gt::fmt_number(
columns = 3,
decimals = 3
)
```
Note: Deprivation matrices are included by default when you run `compute_mpi`. If you want to exclude these in your output, set `.include_deprivation_matrix` equal to `FALSE`.
## 6. Saving output
You may also save your output into an Excel file. You may choose to format the output or retain its tidy format by setting the `formatted_output` parameter appropriately.
```{r, eval=F}
# Formatted output
save_mpi(mpi_result, .filename = 'MPI Sample Output')
# Not formatted
save_mpi(mpi_result, .filename = 'MPI Sample Output (no format)', .formatted_output = FALSE)
```
## Full script
```{r, eval=F}
# ----------------------------------
# Load MPI specs from the built-in specs file
use_global_mpi_specs(
.uid = 'uuid',
.aggregation = 'class'
)
# ----------------------------------
# Create an empty list to store deprivation profile for each indicator
deprivation_profile <- list()
deprivation_profile$nutrition <- df_household_roster |>
define_deprivation(
.indicator = nutrition,
.cutoff = undernourished == 1 & age < 70,
.collapse = TRUE
)
deprivation_profile$child_mortality <- df_household |>
define_deprivation(
.indicator = child_mortality,
.cutoff = with_child_died == 1
)
deprivation_profile$year_schooling <- df_household_roster |>
define_deprivation(
.indicator = year_schooling,
.cutoff = completed_6yrs_schooling == 2,
.collapse = TRUE
)
deprivation_profile$school_attendance <- df_household_roster |>
define_deprivation(
.indicator = school_attendance,
.cutoff = attending_school == 2 & age %in% c(5:24),
.collapse = TRUE
)
deprivation_profile$cooking_fuel <- df_household |>
define_deprivation(
.indicator = cooking_fuel,
.cutoff = cooking_fuel %in% c(4:6, 9)
)
deprivation_profile$sanitation <- df_household |>
define_deprivation(
.indicator = sanitation,
.cutoff = toilet > 1
)
deprivation_profile$drinking_water <- df_household |>
define_deprivation(
.indicator = drinking_water,
.cutoff = drinking_water == 2
)
deprivation_profile$electricity <- df_household |>
define_deprivation(
.indicator = electricity,
.cutoff = electricity == 2
)
deprivation_profile$housing <- df_household |>
define_deprivation(
.indicator = housing,
.cutoff = roof %in% c(5, 7, 9) |
walls %in% c(5, 8, 9, 99) == 2 |
floor %in% c(5, 6, 9)
)
deprivation_profile$assets <- df_household |>
dplyr::mutate_at(
dplyr::vars(dplyr::starts_with('asset_')),
~ dplyr::if_else(. > 0, 1L, 0L)
) |>
dplyr::mutate(
asset_phone = dplyr::if_else(
(asset_telephone + asset_mobile_phone) > 0,
1L,
0L
)
) |>
dplyr::mutate(
with_hh_conveniences = (
asset_tv + asset_phone + asset_computer +
asset_animal_cart + asset_bicycle +
asset_motorcycle + asset_refrigerator) > 1,
with_mobility_assets = (asset_car + asset_truck) > 0
) |>
define_deprivation(
.indicator = assets,
.cutoff = !(with_hh_conveniences & with_mobility_assets)
)
# ----------------------------------
# Compute the MPI
mpi_result <- df_household |>
compute_mpi(deprivation_profile)
# ----------------------------------
# You may also save your output into an Excel file
# Formatted output
# save_mpi(mpi_result, .filename = 'MPI Sample Output', .include_specs = T)
# Not formatted
save_mpi(
mpi_result,
.filename = 'MPI Sample Output (no format)',
.formatted_output = FALSE,
.include_specs = TRUE
)
```