-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.Rmd
627 lines (397 loc) · 19.7 KB
/
main.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
614
615
616
617
618
619
620
621
622
623
---
title: "Severity Scores: BGD Pilot"
author: "REACH BGD"
date: "2 July 2019"
output:
html_document:
code_folding: hide
toc: true
---
```{r setup, include=FALSE, warning = FALSE, message=FALSE}
group<-c("host","refugee")[1]
knitr::opts_chunk$set(echo = TRUE,
message=FALSE,
warning = FALSE)
library(dplyr)
library(hypegrammaR)
library(kableExtra)
library(msni19)
library(glue)
library(stringr)
library(rgdal)
library(ggmosaic)
library(sp)
library(sf)
library(ggrepel)
source("functions/utilities.R")
source("functions/loading_inputs.R")
source("functions/severity_recoding_from_combination_table.R")
source("functions/severity_recoding_host.R")
source("functions/severity_recoding_refugee.R")
source("functions/plots.R")
source("./functions/sensitivity_utilities.R")
source("./functions/sensitivity_indicator_inclusion.R")
source("./functions/prioritisation_compare.R")
source("./functions/vulnerability_recoding_host.R")
source("./functions/vulnerability_recoding_refugee.R")
source("./functions/gis.R")
source('./functions/colours.R')
# READ INPUT FILES
assessment<-msna18_severity_pilot_load_assessment(group)
if(group=="host") {
severity_compositions <- host_severity_bgd_msna18
vulnerability_compositions <- host_vulnerability_bgd_msna18
}
if(group == "refugee") {
severity_compositions <- refugee_severity_bgd_msna18
vulnerability_compositions <- refugee_vulnerability_bgd_msna18
}
assessment$data %<>% remove_non_consent
assessment$severity <- severity_compositions(hh = assessment$data,
ind = assessment$loops$individuals)
assessment$vulnerability <- vulnerability_compositions(hh = assessment$data,
ind = assessment$loops$individuals)
if(group == "refugee"){
combination_tables<-read_all_csvs("./input_public/threshold_definitions/refugees/")
}else{
combination_tables<-read_all_csvs("./input_public/threshold_definitions/host/")
}
subpillar_scores <- subpillar_scores_bgd(list_of_combination_tables = combination_tables,
data = assessment$severity)
assessment$severity <- c(assessment$severity,subpillar_scores) %>% as_tibble
# impact & capacity score guessed
assessment$severity$impact <- 4
# msni not existent yet so randomising
assessment$data$msni<-with(assessment$severity,{
msni19::msni(education_lsg = edu,
fsl_lsg = fsl,
health_lsg = health,
protection_lsg = protection,
wash_lsg = wash,
shelter_lsg = shelter,
impact = impact,
capacity_gaps = capacity)
})
# everything below happening inside "assessment", so attaching for brevity:
attach(assessment)
```
# `r ifelse(group=="host","Host Community", "Refugees")`
## Summary
- The index
## Results
Estimated distribution of severity:
```{r,fig.height = 20,fig.width = 5}
faceted_density_plot(assessment$data,
x_variable_name = "msni",
default_disaggregation)
```
The estimated severity by location:
```{r}
type_of_analysis<-map_to_case("group_difference",
dependent.var.type = "categorical",
independent.var.type = "categorical")
severity_disaggregated <- map_to_result(data,dependent.var = "msni",
independent.var = default_disaggregation,
case = type_of_analysis,
weighting = weighting)
severity_disaggregated %>%
map_to_labeled(questionnaire) %>%
map_to_table %>% kable %>% kable_styling()
```
```{r,fig.height=10}
severity_disaggregated %>%
(function(x){x$summary.statistic<-x$summary.statistic %>% arrange(independent.var.value,numbers);x}) %>%
map_to_labeled(questionnaire) %>% (hypegrammaR:::grouped_barchart_percent) %>% .$ggplot + coord_flip()+ylab("% population per MSNI score")
```
### Map Severities
```{r, message=FALSE, warning = FALSE}
type_of_analysis<-map_to_case("direct_reporting",
dependent.var.type = "numerical",
independent.var.type = "categorical")
severity_disaggregated <- map_to_result(data,dependent.var = "msni",
independent.var = default_disaggregation,
case = type_of_analysis,
weighting = weighting)
severity_disaggregated %>% result_disaggregated_by_match_id_as_map(assessment)
type_of_analysis<-map_to_case("group_difference",
dependent.var.type = "categorical",
independent.var.type = "categorical")
severity_disaggregated4 <- map_to_result(data,dependent.var = "msni",
independent.var = default_disaggregation,
case = type_of_analysis,
weighting = weighting)
severity_disaggregated3 <- map_to_result(data,dependent.var = "msni",
independent.var = default_disaggregation,
case = type_of_analysis,
weighting = weighting)
severity_disaggregated3_4 <- map_to_result(data,dependent.var = "msni",
independent.var = default_disaggregation,
case = type_of_analysis,
weighting = weighting)
severity_disaggregated4$summary.statistic<-severity_disaggregated4$summary.statistic %>%
filter(as.character(dependent.var.value)=="4")
severity_disaggregated3$summary.statistic<-severity_disaggregated3$summary.statistic %>%
filter(as.character(dependent.var.value)=="3")
severity_disaggregated3_4$summary.statistic<-severity_disaggregated3_4$summary.statistic %>%
filter(as.character(dependent.var.value)=="3"| as.character(dependent.var.value)=="4") %>%
group_by(independent.var.value) %>%
summarise(numbers=sum(numbers))
# mutate(percent=round(numbers,2)) %>%
# select(camp_name= independent.var.value,percent)
#
severity_disaggregated3 %>% result_disaggregated_by_match_id_as_map(assessment,
color_scale_name = "% Population - Severity 3")
severity_disaggregated4 %>% result_disaggregated_by_match_id_as_map(assessment,
color_scale_name = "% Population - Severity 4")
severity_disaggregated3_4 %>% result_disaggregated_by_match_id_as_map(assessment,
color_scale_name = "% Population - Severity >= 3")
# #score of
# sum_stats<-severity_disaggregated$summary.statistic %>%
# filter(as.character(dependent.var.value)=="4") %>%
# mutate(percent=round(numbers,2)) %>%
# select(camp_name= independent.var.value,percent)
#
# sum_stats$camp_name<-str_standardize_cxb_camps(sum_stats$camp_name)
#
# sfdf <- st_as_sf(cmp_data)
#
# sfdf <- sfdf %>%
# mutate(
# lon = purrr::map_dbl(geometry, ~ st_centroid(.x)[[1]]),
# lat = purrr::map_dbl(geometry, ~ st_centroid(.x)[[2]])
# )
#
# limits<-c(min(sfdf$percent,na.rm = T),max(sfdf$percent,na.rm = T))
#
# plots<-sfdf %>% split(sfdf$camp_setting) %>% purrr::map(function(x){
# plot<-ggplot(x)+
# geom_sf(aes(fill=percent),color = 'white')+
# theme_minimal()+
# scale_fill_continuous(limits=limits, name= "Percent Severity Score of 4")+
# theme(axis.text.x = element_blank(),
# axis.text.y = element_blank(),
# axis.ticks =element_blank())+
# geom_text_repel(aes(x = lon, y = lat, label = camp_name))
# print(plot)
# })
#
```
### Distribution - How 'varied' are the scores?
```{r}
design<-map_to_design(data,weighting_function = weighting)
standard_deviation <- round(sqrt(survey::svyvar(~msni,design,na.rm = T)[1]),2) %>% unname()
```
The overall standard deviation was <b> `r standard_deviation` </b>
```{r}
ecdf <- weighted_ecdf(data$msni, weighting(data))
ggplot(ecdf, aes(x, cum.pct)) +
geom_line() +
theme_minimal() +
ylab("Cummulative probability") +
xlab("Score")
```
## Methodology
....
## Applicability for Prioritisation
In the pilot dataset, the scores generally do not vary enough to identify singificant differences (with p< 0.01) at a disaggregation level sampled for 90% confidence with a 10% error margin. We tested for differences treating the Severity score as numerical and as categorical (nominal)
### Differences between locations (severity numerical)
```{r}
comparisons <- compare_each(data,
dependent.variable = "msni",
dependent.variable.type = "numerical",
independent.variable = default_disaggregation,
weighting = weighting)
kable_comparison_result(comparisons,independent.variable.title = "Location")
```
### Differences between locations (severity categorical)
```{r}
comparisons <- compare_each(data,
dependent.variable = "msni",
dependent.variable.type = "categorical",
independent.variable = default_disaggregation,
weighting = weighting)
comparisons %>% kable_comparison_result_categorical("Location")
```
### Differences based on Vulnerability Indicators
```{r by vulnerability,results='asis'}
for(i in names(vulnerability)){
data[[i]]<-vulnerability[[i]]
}
vulnerability_columns<-names(data) %>% grep("^vi\\.",.,value = T)
vulnerability_columns<- vulnerability_columns[-1]
for(vi in vulnerability_columns){
cat("\n\n**",vi,"**\n\n")
if(length(unique(data[[vi]]))>1){
comparisons <- compare_each(data,
dependent.variable = "msni",
dependent.variable.type = "categorical",
independent.variable = vi,
weighting = weighting)
comparisons %>% kable_comparison_result_categorical(vi) %>% cat
}
}
```
...
## Exploratory Analysis: Profiles of Households with severe needs
...
## Sensitivity and Robustness
### Missing values in composite indicators
```{r}
percent_na_sev<-severity[,-1] %>% lapply(percent_na) %>% unlist
data.frame(`composite variable` = names(severity[,-1]),
`percent NA` = percent_na_sev,
check.names = FALSE) %>%
kable %>%
kable_styling()
```
### Sensitivity to Included Indicators
```{r}
# create variations and calculate msni
msni_varied_indicators<-msni_variations(combination_tables,
assessment$severity,
variation_function = vary_combination_tables_indicator_inclusion)
```
```{r, results='asis'}
cat(if(group == "refugee"){
"We show that overall, the index is not overly affected by removing (and by extension adding) individual indicators from the calculation, with the exception of three indicators whos importance warrants a stronger impact."
} else if( group == "host"){
"We show that overall, the index is not overly affected by removing (and by extension adding) individual indicators from the calculation."
}
)
cat(paste("We test how sensitive the index is with regards to which indicators are included; The overall score should ideally not be completely changed if specific indicators are added or removed; at the same time we should expect at least some variation based on which indicators are included. Prior to analysis, we choose a threshold of **a difference of 0.5 on the 1-4 scale that should ideally not be surpassed** if an individual indicator is removed (or added). We recalculate the score after removing each of the",ncol(msni_varied_indicators),"sub-indicators , and test for each one whether the difference to the complete index exceeds 0.5"))
```
```{r}
# t-test: differences smaller 0.5 on average?
# new dataframe: differences & stratum
msni_original<- tibble(msni = assessment$data$msni)
sensitivity_test_table<- sensitivity_variation_test_result_table(msni_original = msni_original,
msni_varied = msni_varied_indicators,
threshold = 0.5,
strata_data_name = default_disaggregation,
strata_data_values = data[[default_disaggregation]])
sensitivity_test_table %>% kable(escape = FALSE) %>% kable_styling()
```
```{r,results = 'asis'}
cat(if(group == "refugee"){
"The overall index changes more than 0.5 points when removing the indicators for _drinking water source_ (WASH), _child marriage to reduce economic burden_ (Capacity Gaps) and _water access quantity_ (WASH). In the composite index, these indicators were consiously selected to have strong impact. As such, it is expected that their removal would have a strong impact on the results.
The index is robust towards all other indicators within the threshold of 0.5 points on the scale."
} else if( group == "host"){
"None of the indicators led to a change of more than 0.5 in the average score when removed. This suggests that the index is robust towards inclusion / exclusion of indicators"
}
)
```
```{r mosaic plot}
# Example of how the formula is built
#
# weight = 1
# x = product(Y, X)
# fill = W
# conds = product(Z)
#
# These aesthetics set up the formula for the distribution:
#
# Formula: 1 ~ W + X + Y | Z
stratum_tibble<-tibble(stratum=assessment$data[[default_disaggregation]])
colnames(stratum_tibble)<-default_disaggregation
msni_varied_and_original<-c(msni_varied_indicators,
msni_original,
stratum_tibble) %>% as_tibble
msni_varied_and_original<-gather_(msni_varied_and_original,key = "key",value = 'value',gather_cols = names(msni_varied_and_original)[!(names(msni_varied_and_original) %in% c("msni",default_disaggregation))])
msni_varied_and_original$weights<-weighting(msni_varied_and_original)
msni_varied_and_original$value<-round(msni_varied_and_original$value,2)
msni_varied_and_original<-msni_varied_and_original[!is.na(msni_varied_and_original),]
ggplot(msni_varied_and_original)+
geom_mosaic(aes(weight=weights,
x=product(msni,value),fill = (msni)))+scale_fill_brewer(name = "actual MSNI score")+theme_minimal()+theme(axis.text.y = element_blank())+xlab("MSNI score with one indicator removed")+ylab("")
```
### Sensitivity to Thresholds
The methodology relies heavily on normative judgement from sector as well is inter-sectoral experts.
As such, it needs to be robust with regards to variations in those judgements.
We add random noise of varying strength to the expert decision of how the indicators are combined, and compare the results to the original msni needs score.
```{r}
# create variations and calculate msni
msni_varied_thresholds<-msni_variations(combination_tables,
assessment$severity,
variation_function = vary_combination_tables_thresholds)
```
```{r}
# t-test: differences smaller 0.5 on average?
# new dataframe: differences & stratum
msni_original<- tibble(msni = assessment$data$msni)
sensitivity_test_table<- sensitivity_variation_test_result_table(msni_original = msni_original,
msni_varied = msni_varied_thresholds,
threshold = 0.5,
strata_data_name = default_disaggregation,
strata_data_values = data[[default_disaggregation]])
sensitivity_test_table %>% kable(escape = FALSE) %>% kable_styling()
```
```{r}
msni_varied_thresholds_with_original<-msni_varied_thresholds
msni_varied_thresholds_with_original$original<-assessment$data$msni
msni_varied_thresholds_long <- msni_varied_thresholds_with_original %>% gather("key","value", -original)
msni_varied_thresholds_long$key<-gsub("\\.","",msni_varied_thresholds_long$key) %>% gsub("[0-9]","",.)
ggplot(msni_varied_thresholds_long)+geom_bar(aes(x = value,fill = factor(original)),position = "dodge")+xlab("value with varied thresholds")+hypegrammaR:::scale_fill_reach_categorical(4,name = "original Score")+theme_minimal()
diffs<-lapply(msni_varied_thresholds_with_original,function(x){x-unlist(msni_original)}) %>% as_tibble %>% gather("key","value",-original)
diffs$key<-gsub("\\.","",msni_varied_thresholds_long$key) %>% gsub("[0-9]","",.)
ggplot(diffs)+geom_bar(aes(x=value,fill=key))+hypegrammaR:::scale_fill_reach_categorical(8,name = "sub index Score")+theme_minimal()+xlab("Difference between original score and score with varied thresholds")
```
```{r mosaic plot2}
# Example of how the formula is built
#
# weight = 1
# x = product(Y, X)
# fill = W
# conds = product(Z)
#
# These aesthetics set up the formula for the distribution:
#
# Formula: 1 ~ W + X + Y | Z
stratum_tibble<-tibble(stratum=assessment$data[[default_disaggregation]])
colnames(stratum_tibble)<-default_disaggregation
msni_varied_and_original<-c(msni_varied_thresholds,
msni_original,
stratum_tibble) %>% as_tibble
msni_varied_and_original<-gather_(msni_varied_and_original,key = "key",value = 'value',gather_cols = names(msni_varied_and_original)[!(names(msni_varied_and_original) %in% c("msni",default_disaggregation))])
msni_varied_and_original$weights<-weighting(msni_varied_and_original)
msni_varied_and_original$value<-round(msni_varied_and_original$value,2)
msni_varied_and_original<-msni_varied_and_original[!is.na(msni_varied_and_original),]
ggplot(msni_varied_and_original)+
geom_mosaic(aes(weight=weights,
x=product(msni,value),fill = (msni)))+scale_fill_brewer(name = "actual MSNI score")+theme_minimal()+theme(axis.text.y = element_blank())+xlab("MSNI score with one subpillar changed randomly (+/- 1)")+ylab("")
```
```{r, eval=FALSE}
assessment$vulnerability<-host_vulnerability_bgd_msna18(assessment$data,
assessment$loops$individuals)
#
# assessment$vulnerability<-refugee_vulnerability_bgd_msna18(assessment$data,
# assessment$loops$individuals)
#
# data_w_vulnerability<-c(data,assessment$vulnerability) %>% as.data.frame(stringsAsFactors = FALSE)
#
# data_w_vulnerability<-data_w_vulnerability[data_w_vulnerability$msni>=3,]
#
# vars_for_factor_analysis<-data_w_vulnerability %>% names %>% grep("^vi.",.,value = TRUE) #
#
# data_w_vulnerability <- data_w_vulnerability[complete.cases(data_w_vulnerability[,c(vars_for_factor_analysis,default_disaggregation)]),]
#
# data_w_vulnerability[,vars_for_factor_analysis]<-lapply(data_w_vulnerability[,vars_for_factor_analysis],as.numeric) %>% as.data.frame(stringsAsFactors=F)
#
# design<-map_to_design(data_w_vulnerability %>% as.data.frame,weighting_function = weighting)
#
# formula<-vars_for_factor_analysis[-1] %>% paste0(.,collapse = "+") %>% paste0("~",.) %>% formula()
#
# library(FactoMineR)
# FactoMineR::MFA(data_w_vulnerability[,vars_for_factor_analysis],)
#
# fact<-factanal(formula,
# design = data_w_vulnerability,
# factors = 1)
#
# library(factoextra)
#
# print(fit, digits=2, cutoff=.3, sort=TRUE)
# # plot factor 1 by factor 2
# load <- fit$loadings[,1:2]
# plot(load,type="n") # set up plot
# text(load,labels=names(mydata),cex=.7) # add variable names
```