-
Notifications
You must be signed in to change notification settings - Fork 0
/
foodloss_sensitivity.Rmd
306 lines (228 loc) · 11.7 KB
/
foodloss_sensitivity.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
---
title: "Daniel food loss revision"
author: "Johannes Piipponen"
date: "`Spring 2023"
output: html_document
---
Code written by Johannes Piipponen
# Get and explore data
```{r}
# Load necessary libraries
library(tidyverse)
library(fixest)
# Read the data
df_raw <- read_csv("data_johannes.csv")
# Add a new column ln_gni as the natural logarithm of gni
df_raw <- df_raw %>%
mutate(ln_gni = log(gni))
# Explore data: number of countries, crops and years
length(unique(df_raw$country)) # 81 countries
unique(df_raw$year) %>% sort() # years 2000-2020
# Group data by country and count the number of cases
df_raw %>%
group_by(country) %>%
summarise(Number_of_cases = n()) %>%
arrange(Number_of_cases)
# Create a list of countries with more than 80 observations
#my_cntrylist_over80 <-
my_countries_obs <-
df_raw %>%
group_by(country) %>%
summarise(Number_of_cases = n()) %>%
# filter(Number_of_cases > 80) %>%
arrange(Number_of_cases)# %>% pull(country)
```
# Sensitivity analysis
The following code performs a regression analysis using all available data and then explores the sensitivity of the model by excluding individual countries from the analysis.
When all countries are included in the model, it produces specific values for the intercept and beta1 coefficients. However, if we exclude a particular country (e.g., Finland), these coefficient values may change. The objective is to determine the magnitude of these changes.
The ultimate goal is to create a graph that visualizes how the regression results vary when we exclude one country at a time.
Explanation for f_exclude_countries function:
The f_exclude_countries function accepts the country to be excluded as an argument (country_tobe_excluded). It runs the feols model with the specified formula and filters the dataset so that the specified country is not included. After fitting the model, the function uses broom::tidy() to extract the regression results and selects the relevant columns (term, estimate, and p-value). Lastly, the function adds a new column, excluded_country, to indicate the country that was excluded in that particular model run.
```{r}
# Create a function to run feols excluding one country at a time
f_exclude_countries <- function(country_tobe_excluded) {
feols(loss_percentage1 ~ ln_gni + agriShr + agriEmp + msch +
electricityRural + phoneSub + polStab + export + regionalName + as.factor(year),
cluster = ~ regionalName,
data = df_raw %>% filter(country != country_tobe_excluded)) %>%
broom::tidy() %>%
dplyr::select(term, estimate, p.value) %>%
mutate(excluded_country = country_tobe_excluded)
}
# Example function calls
f_exclude_countries("Angola")
f_exclude_countries("Burundi")
# Use the function for all countries
my_countries <- unique(df_raw$country)
# Apply the function to all countries and store the results
results <- map_df(my_countries, f_exclude_countries)
# Widen the results and rename p-value columns ---> not sure if this is needed
results_wider <- results %>%
pivot_wider(names_from = term, values_from = c(estimate, p.value)) %>%
rename(
pval_intercept = `p.value_(Intercept)`,
pval_ln_gni = `p.value_ln_gni`,
pval_agriShr = `p.value_agriShr`,
pval_agriEmp = `p.value_agriEmp`,
pval_msch = `p.value_msch`,
pval_electricityRural = `p.value_electricityRural`,
pval_phoneSub = `p.value_phoneSub`,
pval_polStab = `p.value_polStab`,
pval_export = `p.value_export`
)
```
# Save the original regression
```{r}
library(flextable)
library(modelsummary)
model <- feols(loss_percentage1 ~ ln_gni + agriShr + agriEmp + msch +
electricityRural + phoneSub + polStab + export + regionalName + as.factor(year),
cluster = ~ regionalName,
data = df_raw)
# Create the table for the feols model
model_table <- modelsummary(model, stars = T, output = "flextable")
# uncomment this
#save_as_docx(model_table, path = ("selected_results.docx"))
```
# Plot the results
# Sensitivity like originally but when number of cases is used to define X axis
```{r}
results_arranged <- map_df(unique(my_countries_obs$country), f_exclude_countries)
results_wider_arranged <- results_arranged %>%
pivot_wider(names_from = term, values_from = c(estimate, p.value)) %>%
rename(
pval_intercept = `p.value_(Intercept)`,
pval_ln_gni = `p.value_ln_gni`,
pval_agriShr = `p.value_agriShr`,
pval_agriEmp = `p.value_agriEmp`,
pval_msch = `p.value_msch`,
pval_electricityRural = `p.value_electricityRural`,
pval_phoneSub = `p.value_phoneSub`,
pval_polStab = `p.value_polStab`,
pval_export = `p.value_export`
)
results_long_arranged <- results_wider_arranged %>%
pivot_longer(
cols = starts_with("estimate_"),
names_to = "variable",
values_to = "value"
) %>%
dplyr::select(excluded_country, variable, value) %>%
drop_na()
# Select only some variables
results_long_arranged <- results_long_arranged %>%
filter(variable %in% c("estimate_(Intercept)", "estimate_ln_gni",
"estimate_agriShr",
"estimate_agriEmp",
"estimate_msch",
"estimate_electricityRural",
"estimate_phoneSub",
"estimate_polStab",
"estimate_export" ))
# Draw a combined plot
library(scico)
plt_combined_arranged <- ggplot(results_long_arranged, aes(x = factor(excluded_country, levels = unique(excluded_country)), y = value, color = variable, group = variable)) +
geom_line(size = 1.2) +
facet_grid(variable ~ .,
scales = "free_y",
labeller = as_labeller(c("estimate_(Intercept)" = "Intercept",
estimate_ln_gni = "Log GNI",
estimate_agriShr = "Agri Share",
estimate_agriEmp = "Agri Emp",
estimate_msch = "Mean Sch",
estimate_electricityRural = "Elec Acc",
estimate_phoneSub = "Phone Sub",
estimate_polStab = "Pol Stab",
estimate_export = "Exports"))) +
theme_gray() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 6),
axis.text.y = element_text(size = 8),
strip.text = element_text(size = 8),
legend.position = "none") +
scale_color_scico_d(palette = "roma") +
labs(title = "Impact of excluding a country on regression output",
x = "Excluded country countries with fewer observations countries with more observations",
y = "Estimate")
plt_combined_arranged
# uncomment this
# ggsave("sensitivity_plot_x_axis_based_on_N_obs.png", plot = plt_combined_arranged, dpi = 300, width = 300, height = 225, units = "mm")
#
# ggsave("sensitivity_plot_x_axis_based_on_N_obs.pdf", plot = plt_combined_arranged)
```
# Same but regional variables
11] "estimate_regionalNameCentral & South Asia"
[12] "estimate_regionalNameEast & Southeast Asia"
[13] "estimate_regionalNameEurope & North America"
[14] "estimate_regionalNameLatin America & the Caribbean"
[15] "estimate_regionalNameMiddle East & North Africa"
[16] "estimate_regionalNameSub-Saharan Africa"
```{r}
# Prepare data for plotting
results_long_reg <- results_wider %>%
pivot_longer(
cols = starts_with("estimate_regional"),
names_to = "variable",
values_to = "value"
) %>%
dplyr::select(excluded_country, variable, value)
# Draw a combined plot
plt_combined_reg <- ggplot(results_long_reg, aes(x = excluded_country, y = value, color = variable)) +
geom_point() +
facet_grid(variable ~ .,
scales = "free_y",
labeller = as_labeller(c("estimate_regionalNameCentral & South Asia" = "Cent & S Asia",
"estimate_regionalNameEast & Southeast Asia" = "E & SE Asia",
"estimate_regionalNameEurope & North America" = "Eur & N Am",
"estimate_regionalNameLatin America & the Caribbean" = "Lat Am & Car",
"estimate_regionalNameMiddle East & North Africa" = "MEast & N Af",
"estimate_regionalNameSub-Saharan Africa" = "Sub-Sah Africa"))) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none") +
labs(title = "Impact of excluding a country on regression output",
x = "Excluded Country",
y = "Estimate")
plt_combined_reg
# ggsave("sensitivity_plot_reg.png", plot = plt_combined_reg, dpi = 300, width = 300, height = 225, units = "mm")
```
# Sensitivity plot for years
```{r}
results_long_year <- results_wider %>%
pivot_longer(
cols = starts_with("estimate_as.factor"),
names_to = "variable",
values_to = "value"
) %>%
dplyr::select(excluded_country, variable, value)
plt_combined_year <- ggplot(results_long_year, aes(x = excluded_country, y = value, color = variable)) +
geom_point() +
facet_grid(variable ~ ., scales = "free_y", labeller = as_labeller(c("estimate_as.factor(year)2001" = "2001",
"estimate_as.factor(year)2002" = "2002",
"estimate_as.factor(year)2003" = "2003",
"estimate_as.factor(year)2004" = "2004",
"estimate_as.factor(year)2005" = "2005",
"estimate_as.factor(year)2006" = "2006",
"estimate_as.factor(year)2007" = "2007",
"estimate_as.factor(year)2008" = "2008",
"estimate_as.factor(year)2009" = "2009",
"estimate_as.factor(year)2010" = "2010",
"estimate_as.factor(year)2011" = "2011",
"estimate_as.factor(year)2012" = "2012",
"estimate_as.factor(year)2013" = "2013",
"estimate_as.factor(year)2014" = "2014",
"estimate_as.factor(year)2015" = "2015",
"estimate_as.factor(year)2016" = "2016",
"estimate_as.factor(year)2017" = "2017",
"estimate_as.factor(year)2018" = "2018",
"estimate_as.factor(year)2019" = "2019",
"estimate_as.factor(year)2020" = "2020"))) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none") +
labs(title = "Impact of excluding a country on regression output",
x = "Excluded Country",
y = "Estimate")
plt_combined_year
# ggsave("sensitivity_plot_year.png", plot = plt_combined_year, dpi = 300, width = 300, height = 225, units = "mm")
```
#