File size: 7,606 Bytes
748dd7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
################################################################################
# chart_dhs.R:  charts and descriptive stats on dhs points and their intersection
# with projects by sector 
################################################################################
setwd(getOption("replication.root", default = getwd())); options(error = NULL)
library(dplyr)
library(tidyr)
library(ggplot2)

KeepObjectsAcrossAnalysisStrings <- get0("KeepObjectsAcrossAnalysisStrings", ifnotfound = character())
try(rm(list=ls()[!ls() %in% (Keeps <- c("t0",KeepObjectsAcrossAnalysisStrings))] ),T)

#get dhs treatment/control counts for actual (not estimated) DHS points
treat_control_actual_dhs_df <- read.csv("./data/interim/dhs_treat_control_3yr_actual_counts.csv") 

###########################################################
#calc dhs treatment/control counts for estimated DHS points
###########################################################
##### read confounder and treatment data from files
dhs_confounders_df <- read.csv("./data/interim/dhs_5k_confounders.csv") %>% 
  dplyr::select(-year)  #remove survey year column that could be confused with oda year

#get list of all dhs_id's and their iso3 for use below from confounder set
#since those without confounder data are not usable
dhs_iso3_df <- dhs_confounders_df %>% 
  distinct(dhs_id,iso3) 

#get treated for all funders and sectors
dhs_t_df <- read.csv("./data/interim/dhs_treated_sector_3yr.csv") %>% 
  #exclude DHS points where confounder data not available 
  inner_join(dhs_confounders_df %>% 
               dplyr::select(dhs_id, ID_adm2), by = join_by(dhs_id)) %>% 
  filter(year_group!="2014:2016")

##### calculate control points #############################
#identify countries where each funder is operating in each sector
funder_sector_iso3 <- dhs_t_df %>% 
  #join to dhs_confounders to get iso3 and limit to dhs points with confounder data
  inner_join(dhs_confounders_df, by="dhs_id") %>%  
  distinct(funder,sector,iso3)
  
#create a record for each year_group for panel data
year_group_v <- c('2002:2004', '2005:2007', '2008:2010', '2011:2013')

#generate dataframe of all dhs points for all year groups in operating countries 
all_t_c_df <- funder_sector_iso3 %>%
  #create a row for each year group
  crossing(year_group = year_group_v) %>% 
  #create a row for each dhs_id
  left_join(dhs_iso3_df,by="iso3",
            multiple = "all")

#remove treated funder/sector/dhs/year_group observations to construct controls 
dhs_c_df <- all_t_c_df %>% 
  #exclude dhs_points treated in each year_group
  anti_join(dhs_t_df,by=c("sector","funder","dhs_id","year_group"))

treat_dhs_count_df  <- dhs_t_df %>%   
  group_by(funder,sector) %>%
  summarize(treat_n = n()) %>% 
  ungroup()

control_dhs_count_df  <- dhs_c_df %>%   
  group_by(funder,sector) %>%
  summarize(control_n = n()) %>% 
  ungroup()

#join control and treated into a single dataframe
treat_control_est_dhs_df <- treat_dhs_count_df %>% 
  left_join(control_dhs_count_df, by=c("funder","sector")) %>% 
  rename(est_iwi_treat_n = treat_n,
         est_iwi_control_n = control_n)

#join with t/c counts from actual IWI DHS locations rather than estimates
t_c_est_act_df <- treat_control_est_dhs_df %>% 
  left_join(treat_control_actual_dhs_df, by=c("funder","sector")) %>% 
  rename(act_iwi_treat_n = treat_n,
         act_iwi_control_n = control_n) %>% 
  mutate(act_iwi_treat_n = ifelse(is.na(act_iwi_treat_n),0,act_iwi_treat_n),
         act_iwi_control_n = ifelse(is.na(act_iwi_control_n),0,act_iwi_control_n))

write.csv(t_c_est_act_df,"./tables/dhs_treat_control_est_act_compare.csv",row.names=FALSE)

#adjust for display
sector_names_df <- read.csv("./data/interim/sector_group_names.csv") %>% 
  mutate(sec_pre_name = paste0(ad_sector_names," (",ad_sector_codes,")")) %>% 
  dplyr::select(ad_sector_codes, sec_pre_name)

t_c_est_act_display_df <- t_c_est_act_df %>% 
  left_join(sector_names_df,join_by(sector==ad_sector_codes)) %>% 
  pivot_longer(
    cols = c(est_iwi_treat_n, est_iwi_control_n, act_iwi_treat_n, act_iwi_control_n),
    names_to = "count_name",
    values_to = "count_obs"
  ) %>%
  unite("funder_count_name", funder, count_name, sep = "_") %>%
  pivot_wider(
    names_from = funder_count_name,
    values_from = count_obs
  ) %>% 
  dplyr::select(-sector)
  
#write display version
write.csv(t_c_est_act_display_df,"./tables/dhs_treat_control_est_act_display.csv",row.names=FALSE)

#################################################################################
#xy plots to compare treated and control n for actual and estimated wealth 
#################################################################################

#determine the limits of the plot
max_abs_value <- max(abs(t_c_est_act_df$est_iwi_treat_n),
                     abs(t_c_est_act_df$act_iwi_treat_n),
                     na.rm=T)

treated_est_act <- t_c_est_act_df %>% 
  ggplot(aes(x = act_iwi_treat_n, y = est_iwi_treat_n, color=funder, 
             label=sector)) +
  geom_point() +
  ggrepel::geom_text_repel(box.padding = .1,max.overlaps=Inf,show.legend=FALSE) +
  scale_color_manual(name = "Funder",
                     values = c("ch"="indianred1","wb"="mediumblue"),
                     labels = c("China","World Bank")) +
  geom_abline(intercept=0, slope=1, linetype="dashed",color="gray80") +
  labs(title = "Treated Observations: Actual DHS Versus Estimated Wealth Outcomes\nBy Funder and Sector Number",
       x = "Actual DHS Wealth Data Treated Number (Cross-Sectional Data)",
       y = "Estimated Wealth Data Treated Number (Panel Data)",
       legend = "Funder") +   
  coord_cartesian(xlim=c(0,max_abs_value),
                  ylim=c(0,max_abs_value)) +
  theme_bw()  +
  theme(panel.grid = element_blank())

#save
ggsave("./figures/xy_treated_n_est_act.pdf",
       treated_est_act,
       width=8, height = 6, dpi=300,
       bg="white", units="in")


#determine the limits of the plot
max_abs_value_tc <- max(abs(t_c_est_act_df$est_iwi_treat_n),
                     abs(t_c_est_act_df$act_iwi_treat_n),
                     abs(t_c_est_act_df$est_iwi_control_n),
                     abs(t_c_est_act_df$act_iwi_control_n),
                     na.rm=T)

t_c_est_act_fig <- t_c_est_act_df %>% 
  pivot_longer(cols=starts_with("est"),names_to="var_est",values_to="estimated_n") %>% 
  mutate(var=ifelse(grepl("_iwi_treat_n",var_est),"Treated","Control")) %>% 
  dplyr::select(-var_est) %>% 
  mutate(actual_n=ifelse(var=="Treated",act_iwi_treat_n,act_iwi_control_n)) %>% 
  dplyr::select(-act_iwi_treat_n,-act_iwi_control_n ) %>% 
  ggplot(aes(x = actual_n, y = estimated_n, color=funder, 
             label=sector)) +
  facet_wrap(var ~ funder,scales="free",
             labeller=labeller(funder=c("ch"="China","wb"="World Bank"))) + 
  geom_point(show.legend=FALSE) +
  ggrepel::geom_text_repel(box.padding = .1,max.overlaps=Inf,show.legend=FALSE) +
  scale_color_manual(values = c("ch"="indianred1","wb"="mediumblue"),
                     labels = c("China","World Bank")) +
  geom_abline(intercept=0, slope=1, linetype="dashed",color="gray80") +
  labs(title = "Observation Number With Actual DHS Versus Estimated Wealth Outcomes\nBy Funder and Sector Number",
       x = "Actual DHS Wealth Data Observations (Cross-Sectional Data)",
       y = "Estimated Wealth Data Observations (Panel Data)",
       legend = "Funder") +   
  theme_bw()  +
  theme(panel.grid = element_blank())

#save
ggsave("./figures/xy_cntrl_treated_n_est_act.pdf",
       t_c_est_act_fig,
       width=8, height = 8, dpi=300,
       bg="white", units="in")