-
Notifications
You must be signed in to change notification settings - Fork 1
/
causal_inference.Rmd
365 lines (251 loc) · 7.61 KB
/
causal_inference.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
---
title: "Causal_inference_NatFood"
output: html_document
date: "2023-06-24"
author: Liya Weldegebriel
---
Description:
- imports daily rainfall, cultivation status and number of conflict incidences data using 10 by 10 km grid data
- calculates two-way fixed effect estimates of the impact of the number of conflict incidence on area of well-cultivated land with rainfall characterstic covariates
```{r}
library(dplyr)
library(lubridate)
library(ggplot2)
library(reshape2)
library(fixest)
```
#Import data
```{r}
files.location <- paste0('UPDATE WITH YOUR FILE LOCATION') #Update with local file location for all data
```
##Importing daily rainfall 2019 to 2021
```{r}
setwd(files.location)
read.csv("Precip_daily_Tigray_19to21_529grids.csv")-> daily.precip
```
##Importing Cultivatetion status datafrom pre-war (2019/2020) and post-war (2021) periods
```{r}
setwd(files.location)
read.csv("CultArea_cropgrass_1200_10k_2021.csv")%>%
mutate(., cult_area = X1, year = 2021) %>%
select(., c(id,cult_area,year)) ->stat_21
read.csv("CultArea_cropgrass_1200_10k_2020.csv")%>%
mutate(., cult_area = X1, year = 2020)%>%
select(., c(id,cult_area, year))->stat_20
rbind(stat_21, stat_20)%>%
mutate(., cult_area = cult_area/1E6)-> cult_stat
head(cult_stat)
```
##Importing conflict incidence data
```{r}
setwd(files.location)
read.csv("CI_count_10k.csv")%>%
mutate(., grid_size = "ten_k")%>%
group_by(id, grid_size)%>%
summarise(count = sum(n_CI))-> grid_CI_count
grid_CI_count%>%
mutate(., CI = count)%>%
select(., c("id","CI"))-> CI
head(CI)
```
```{r}
setwd(files.location)
read.csv("CI_count_10k.csv")-> CI_raw
with(CI_raw,
ifelse(month == "2020" ,'early',
ifelse(month == "jan" ,'early',
ifelse(month == "feb" ,'prep',
ifelse(month == "march" ,'prep',
ifelse(month == "april" ,'prep',
ifelse(month == "may" ,'prep',
ifelse(month == "june" ,'growing',
ifelse(month == "july" ,'growing',
ifelse(month == "aug" ,'growing',
ifelse(month == "sept" ,'growing',
ifelse(month == "oct" ,'growing', "NA"))))))))))))-> CI_raw$time.group
CI_raw%>%
filter(., time.group == "prep")%>%
mutate(., CI = n_CI)%>%
select(., c(id,CI))-> CI_prep
CI_raw%>%
filter(., time.group == "early")%>%
mutate(., CI = n_CI)%>%
select(., c(id,CI))-> CI_early
CI_raw%>%
filter(., time.group == "prep" | time.group == "growing" )%>%
mutate(., CI = n_CI)%>%
select(., c(id,CI))-> CI_prep_growing
CI_raw%>%
filter(., time.group == "growing" )%>%
mutate(., CI = n_CI)%>%
select(., c(id,CI))-> CI_growing
CI_prep
CI_early
CI_prep_growing
CI_growing
```
#Rainfall characterstics
```{r}
require(lubridate)
daily.precip%>%
mutate( day = date, Rain = precipitation )%>%
mutate('date' = make_date(year = year, month = month, day = date))%>%
select(., c("date","day","month","year", "id","Rain")) -> rf
head(rf)
```
```{r}
rf%>%
filter(month %in% unique(.$month)[c(6,7,8,9)]) -> Kiremt_rf #June to Sept
head(Kiremt_rf)
```
##Rainfall Frequency
Initializing a data frame
```{r}
frequency_initial <- data.frame(Rain_day=numeric(),
No_Rain_day=numeric(),
NA_s=numeric(),
frequency_RF=numeric(),
year = integer(),
id = integer(),
stringsAsFactors=FALSE)
temp_Rain <- data.frame(Rain_day=numeric(),
No_Rain_day=numeric(),
NA_s=numeric(),
frequency_RF=numeric(),
year = integer(),
id = integer(),
stringsAsFactors=FALSE)
```
Rainfall frequency function
```{r}
freq <- function(Stations_Data) {
yr_id <- c(2019,2020,2021)
id <- c(0:528)
for(i in yr_id){
for(j in id){
Stations_Data%>%
filter(id == j)%>%
filter(year == i) ->V
length(which(V$Rain!=0))-> frequency_initial[1,"Rain_day"]
length(which(V$Rain==0)) -> frequency_initial[1,"No_Rain_day"]
length(which(is.na(V$Rain))) -> frequency_initial[1,"NA_s"]
frequency_initial$year <- i
frequency_initial$id <- j
temp_Rain <- rbind(temp_Rain,frequency_initial)
}
}
return(temp_Rain)
}
```
```{r}
freq(Stations_Data = Kiremt_rf)-> Rainfall_Frequency_Kiremt
Rainfall_Frequency_Kiremt$Rain_day/Rainfall_Frequency_Kiremt$No_Rain_day-> Rainfall_Frequency_Kiremt$frequency_RF
Rainfall_Frequency_Kiremt
```
```{r}
Rainfall_Frequency_Kiremt%>%
group_by(year)%>%
summarise_at(c("frequency_RF"), mean, na.rm=TRUE)-> meanRFfreq
meanRFfreq$year <- as.factor(meanRFfreq$year)
```
##Mean rainfall
```{r}
mean_initials <- data.frame(Mean_Kiremt=numeric(),
year = integer(),
id = integer(),
stringsAsFactors=FALSE )
temp_MeanRains <- data.frame(Mean_Kiremt=numeric(),
year = integer(),
id = integer(),
stringsAsFactors=FALSE )
Mean_rain <- function(Stations_Data) {
yr_id <- c(2019,2020,2021)
id <- c(0:529)
for(i in yr_id){
for(j in id){
Stations_Data%>%
filter(id == j)%>%
filter(year == i)->V_kiremt
V_kiremt%>%
filter(., Rain!=0) -> V_kiremt
mean(V_kiremt$Rain, na.rm= TRUE) ->mean_initials[1,"Mean_Kiremt"]
mean_initials$year <- i
mean_initials$id <- j
temp_MeanRains <- rbind(temp_MeanRains,mean_initials)
}
}
return(temp_MeanRains)
}
```
Finding Means
```{r}
Mean_rain(Stations_Data =Kiremt_rf )-> Rainfall_Mean
Rainfall_Mean
```
##Cumulative rainfall
```{r}
Kiremt_rf%>%
dplyr::select(Rain, id, year)%>%
group_by(id, year)%>%
summarize(Rain_Cum=sum(Rain,na.rm=TRUE))-> Rainfall_cum_kiremt
Rainfall_cum_kiremt
rf%>%
dplyr::select(Rain, id, year)%>%
group_by(id, year)%>%
summarize(Rain_Cum=sum(Rain,na.rm=TRUE))-> Rainfall_cum_annual
Rainfall_cum_annual
Rainfall_cum_annual%>%
group_by(id) %>%
summarise_at(vars(-year), funs(mean(., na.rm=TRUE))) ->Mean_Annual_rain
head(Mean_Annual_rain)
```
##Summary of cumulative, mean and frequency of kiremt rainfall
```{r}
head(Rainfall_cum_kiremt)
head(Rainfall_Mean)
head(Rainfall_Frequency_Kiremt)
```
Combining RF characteristics
```{r}
Rainfall_Frequency_Kiremt %>%
select(., c(frequency_RF, year, id))%>%
merge(., Rainfall_Mean, by = c("year","id"))%>%
merge(.,Rainfall_cum_kiremt, by = c("year","id") ) -> rf_summary
head(rf_summary)
```
##RF summary
```{r}
rf_summary$year = ifelse(rf_summary$year <= 2020, 2020, 2021)
rf_summary%>%
group_by(id,year)%>%
summarise_at(c("frequency_RF", "Mean_Kiremt", "Rain_Cum"), mean, na.rm=TRUE)-> rf.data
head(rf.data)
```
#Causal inference analysis via TWFE
```{r}
twfe_function <- function(rf.data, cult.data, CI.data){
rf.data%>%
merge(., cult.data, by = c("id", "year"))%>%
merge(., CI.data, by = "id") -> rf_twfe
rf_twfe$CI = ifelse(rf_twfe$year == 2020, 0, rf_twfe$CI) #replace all year 2020 CI with 0
rf_twfe$id <- as.factor(rf_twfe$id)
rf_twfe$year <- as.factor(rf_twfe$year)
return(summary(feols(cult_area ~ CI + Rain_Cum + frequency_RF + Mean_Kiremt | id + year, data = rf_twfe)))
}
```
##Using All CIs
```{r}
twfe_function(rf.data, cult_stat, CI)
```
##Using Growing CIs
```{r}
twfe_function(rf.data, cult_stat, CI_growing)
```
##Using Prep CIs
```{r}
twfe_function(rf.data, cult_stat, CI_prep)
```
##Using Early CIs
```{r}
twfe_function(rf.data, cult_stat, CI_early)
```