-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-negative-glmers.R
436 lines (351 loc) · 11.4 KB
/
script-negative-glmers.R
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
# Start
# title: "Regression Analysis"
# author: "Raquel Baeta"
# date: "2024-07-25"
# Start Regression Analysis
#
# Setting up
#
# Set working directory
setwd("~/Desktop/working-sessions/models")
# Load data (Ensure the file format is correct)
data <- readRDS("~/Desktop/working-sessions/models/data.csv.rds")
print(data)
# Look for duplicates
any_duplicates <- any(duplicated(data))
print(any_duplicates)
#
# Aggregating data
#
# Cut into year_intervals
data$year_interval_3yr <- cut(
data$year,
breaks = seq(1996, 2019, by = 3),
include.lowest = TRUE,
labels = FALSE)
#
# Model 1: "any seizures"
#
# Group by 3-year intervals for binary
grouped_data_3yr_binary <- data %>%
group_by(region, country, code, year_interval_3yr) %>%
summarise(
any_seizures = max(seizures_binary),
any_UN = max(any_UN),
mean_log_adjusted_gdp_cap = mean(log_adjusted_gdp_cap),
mean_log_gdp = mean(log_gdp),
mean_milex_gdp = mean(milex_gdp),
mean_trade_ratio = mean(trade_ratio),
mean_CC.EST = mean(CC.EST),
mean_RQ.EST = mean(RQ.EST),
mean_RL.EST = mean(RL.EST),
mean_PV.EST = mean(PV.EST),
mean_GE.EST = mean(GE.EST),
mean_VA.EST = mean(VA.EST)
)
#
# Cleaning
#
# Replace NA values with a placeholder (Ensure this is appropriate)
grouped_data_3yr_binary$year_interval_3yr[is.na(grouped_data_3yr_binary$year_interval_3yr)] <- 8
# Verify the change
summary(grouped_data_3yr_binary$year_interval_3yr)
# Look for duplicates
any_duplicates <- any(duplicated(grouped_data_3yr_binary))
print(any_duplicates)
# Save data as a .rds
saveRDS(grouped_data_3yr_binary, "~/Desktop/working-sessions/models/grouped_data_3yr_binary.csv.rds")
#
# Logistic regression model with random effects
#
# Model for "any seizures" and "Governance Effectiveness"
model_glmer.GE <- glmer(
any_seizures ~ any_UN +
mean_log_adjusted_gdp_cap + mean_log_gdp +
mean_milex_gdp + mean_trade_ratio + mean_GE.EST +
(1 | region) +
(1 | year_interval_3yr),
data = grouped_data_3yr_binary,
family = binomial
)
# Print
summary(model_glmer.GE)
# Marginal effect of UN commitment on predicted seizures
ggeffect_glmer.GE <- ggeffect(model_glmer.GE, terms = "any_UN")
print(ggeffect_glmer.GE)
# Generate predicted probabilities
plot_obj_GE <- plot_model(
model_glmer.GE,
type = "pred",
terms = c("any_UN", "mean_GE.EST"),
.plot = FALSE)
# Plot predicted probabilities
plot_obj_GE <- plot_obj_GE +
theme_minimal() +
labs(
title = "Predicted Seizures Based on UN Commitment and GE",
x = "UN Commitment",
y = "Predicted Probability of Seizures") +
theme(axis.text.x = element_text(hjust = 1))
print(plot_obj_GE)
# Save the plot as a PDF/PNG
ggsave("predicted_probabilities_seizures_ge_plot.pdf", plot = plot_obj_GE, width = 8, height = 6)
ggsave("predicted_probabilities_seizures_ge_plot.png", plot = plot_obj_GE, width = 8, height = 6)
# Model for "any seizures" and "Rule of Law"
model_glmer.RL <- glmer(
any_seizures ~ any_UN +
mean_log_adjusted_gdp_cap + mean_log_gdp +
mean_milex_gdp + mean_trade_ratio + mean_RL.EST +
(1 | region) +
(1 | year_interval_3yr),
data = grouped_data_3yr_binary,
family = binomial
)
# Print
summary(model_glmer.RL)
# Marginal effect of UN commitment on predicted seizures
ggeffect_glmer.RL <- ggeffect(model_glmer.RL, terms = "any_UN")
print(ggeffect_glmer.RL)
# Generate predicted probabilities
plot_obj_RL <- plot_model(
model_glmer.RL,
type = "pred",
terms = c("any_UN", "mean_RL.EST"),
.plot = FALSE)
# Plot predicted probabilities
plot_obj_RL <- plot_obj_RL +
theme_minimal() +
labs(
title = "Predicted Seizures Based on UN Commitment and RL",
x = "UN Commitment",
y = "Predicted Probability of Seizures") +
theme(axis.text.x = element_text(hjust = 1))
print(plot_obj_RL)
# Save the plot as a PDF/PNG
ggsave("predicted_probabilities_seizures_rl_plot.pdf", plot = plot_obj_RL, width = 8, height = 6)
ggsave("predicted_probabilities_seizures_rl_plot.png", plot = plot_obj_RL, width = 8, height = 6)
# Model for "any seizures" and "PV"
model_glmer.PV <- glmer(
any_seizures ~ any_UN +
mean_log_adjusted_gdp_cap + mean_log_gdp +
mean_milex_gdp + mean_trade_ratio + mean_PV.EST +
(1 | region) +
(1 | year_interval_3yr),
data = grouped_data_3yr_binary,
family = binomial
)
# Print
summary(model_glmer.PV)
# Marginal effect of UN commitment on predicted seizures
ggeffect_glmer.PV <- ggeffect(model_glmer.PV, terms = "any_UN")
print(ggeffect_glmer.PV)
# Generate predicted probabilities
plot_obj_PV <- plot_model(
model_glmer.PV,
type = "pred",
terms = c("any_UN", "mean_PV.EST"),
.plot = FALSE)
# Plot predicted probabilities
plot_obj_PV <- plot_obj_PV +
theme_minimal() +
labs(
title = "Predicted Seizures Based on UN Commitment and PV",
x = "UN Commitment",
y = "Predicted Probability of Seizures") +
theme(axis.text.x = element_text(hjust = 1))
print(plot_obj_PV)
# Save the plot as a PDF/PNG
ggsave("predicted_probabilities_seizures_pv_plot.pdf", plot = plot_obj_PV, width = 8, height = 6)
ggsave("predicted_probabilities_seizures_pv_plot.png", plot = plot_obj_PV, width = 8, height = 6)
# Combine model plots
grid_arrangement_log_reg_rand_eff <- arrangeGrob(
plot_obj_GE, plot_obj_RL, plot_obj_PV,
ncol = 2,
nrow = 3
)
print(grid_arrangement_log_reg_rand_eff)
# Save the combined plot as a PDF/PNG
ggsave("grid_arrangement_log_reg_rand_eff.pdf", plot = grid_arrangement_log_reg_rand_eff, width = 12, height = 8)
ggsave("grid_arrangement_log_reg_rand_eff.png", plot = grid_arrangement_log_reg_rand_eff, width = 12, height = 8)
#
# Model 2: log(mean_seizures)
#
# Group by 3-year intervals for mean
grouped_data_3yr_mean <- data %>%
group_by(region, country, code, year_interval_3yr) %>%
summarise(
mean_seizures = mean(seizures),
any_UN = max(any_UN),
mean_log_adjusted_gdp_cap = mean(log_adjusted_gdp_cap),
mean_log_gdp = mean(log_gdp),
mean_milex_gdp = mean(milex_gdp),
mean_trade_ratio = mean(trade_ratio),
mean_CC.EST = mean(CC.EST),
mean_RQ.EST = mean(RQ.EST),
mean_RL.EST = mean(RL.EST),
mean_PV.EST = mean(PV.EST),
mean_GE.EST = mean(GE.EST),
mean_VA.EST = mean(VA.EST)
)
#
# Cleaning
#
# Replace NA values with a placeholder (Ensure this is appropriate)
grouped_data_3yr_mean$year_interval_3yr[is.na(grouped_data_3yr_mean$year_interval_3yr)] <- 8
# Verify the change
summary(grouped_data_3yr_mean$year_interval_3yr)
# Look for duplicates
any_duplicates <- any(duplicated(grouped_data_3yr_mean))
print(any_duplicates)
#
# Filtering
#
# Filter out "0"
grouped_data_3yr_mean <- grouped_data_3yr_mean %>%
filter(mean_seizures > 0)
# Save data set as a .rds
saveRDS(grouped_data_3yr_mean, "~/Desktop/working-sessions/models/grouped_data_3yr_mean.csv.rds")
#
# Linear mixed effects models
#
# Model for mean_seizures and GE
model_lmm.GE <- lmer(
log(mean_seizures) ~ any_UN +
mean_log_adjusted_gdp_cap + mean_log_gdp +
mean_milex_gdp + mean_trade_ratio + mean_GE.EST +
(1 | region) +
(1 | year_interval_3yr),
data = grouped_data_3yr_mean
)
# Print
summary(model_lmm.GE)
# Marginal effect of UN commitment on predicted seizures
ggeffect_lmm.GE <- ggeffect(model_lmm.GE, terms = "any_UN")
print(ggeffect_lmm.GE)
# Generate predicted values
plot_obj_lmm_GE <- plot_model(
model_lmm.GE,
type = "pred",
terms = c("any_UN", "mean_GE.EST"),
.plot = FALSE)
# Plot predicted values
plot_obj_lmm_GE <- plot_obj_lmm_GE +
theme_minimal() +
labs(
title = "Predicted Mean Seizures Based on UN Commitment and GE",
x = "UN Commitment",
y = "Predicted Mean Seizures") +
theme(axis.text.x = element_text(hjust = 1))
print(plot_obj_lmm_GE)
# Save the plot as a PDF/PNG
ggsave("predicted_mean_seizures_ge_plot.pdf", plot = plot_obj_lmm_GE, width = 8, height = 6)
ggsave("predicted_mean_seizures_ge_plot.png", plot = plot_obj_lmm_GE, width = 8, height = 6)
# Model for mean_seizures and RL
model_lmm.RL <- lmer(
log(mean_seizures) ~ any_UN +
mean_log_adjusted_gdp_cap + mean_log_gdp +
mean_milex_gdp + mean_trade_ratio + mean_RL.EST +
(1 | region) +
(1 | year_interval_3yr),
data = grouped_data_3yr_mean
)
# Print
summary(model_lmm.RL)
# Marginal effect of UN commitment on predicted seizures
ggeffect_lmm.RL <- ggeffect(model_lmm.RL, terms = "any_UN")
print(ggeffect_lmm.RL)
# Generate predicted values
plot_obj_lmm_RL <- plot_model(
model_lmm.RL,
type = "pred",
terms = c("any_UN", "mean_RL.EST"),
.plot = FALSE)
# Plot predicted values
plot_obj_lmm_RL <- plot_obj_lmm_RL +
theme_minimal() +
labs(
title = "Predicted Mean Seizures Based on UN Commitment and RL",
x = "UN Commitment",
y = "Predicted Mean Seizures") +
theme(axis.text.x = element_text(hjust = 1))
print(plot_obj_lmm_RL)
# Save the plot as a PDF/PNG
ggsave("predicted_mean_seizures_rl_plot.pdf", plot = plot_obj_lmm_RL,
width = 8, height = 6)
ggsave("predicted_mean_seizures_rl_plot.png", plot = plot_obj_lmm_RL,
width = 8, height = 6)
# Model for mean_seizures and PV
model_lmm.PV <- lmer(
log(mean_seizures) ~ any_UN +
mean_log_adjusted_gdp_cap + mean_log_gdp +
mean_milex_gdp + mean_trade_ratio + mean_PV.EST +
(1 | region) +
(1 | year_interval_3yr),
data = grouped_data_3yr_mean
)
# Print
summary(model_lmm.PV)
# Marginal effect of UN commitment on predicted seizures
ggeffect_lmm.PV <- ggeffect(model_lmm.PV, terms = "any_UN")
print(ggeffect_lmm.PV)
# Generate predicted values
plot_obj_lmm_PV <- plot_model(
model_lmm.PV,
type = "pred",
terms = c("any_UN", "mean_PV.EST"),
.plot = FALSE)
# Plot predicted values
plot_obj_lmm_PV <- plot_obj_lmm_PV +
theme_minimal() +
labs(
title = "Predicted Mean Seizures Based on UN Commitment and PV",
x = "UN Commitment",
y = "Predicted Mean Seizures") +
theme(axis.text.x = element_text(hjust = 1))
print(plot_obj_lmm_PV)
# Save the plot as a PDF/PNG
ggsave("predicted_mean_seizures_pv_plot.pdf", plot = plot_obj_lmm_PV, width = 8, height = 6)
ggsave("predicted_mean_seizures_pv_plot.png", plot = plot_obj_lmm_PV, width = 8, height = 6)
# Combine model plots
grid_arrangement_lmm_log_seizures <- arrangeGrob(
plot_obj_lmm_GE, plot_obj_lmm_RL, plot_obj_lmm_PV,
ncol = 2,
nrow = 3
)
print(grid_arrangement_lmm_log_seizures)
# Save the combined plot as a PDF/PNG
ggsave("grid_arrangement_lmm_log_seizures.pdf", plot = grid_arrangement_lmm_log_seizures, width = 12, height = 8)
ggsave("grid_arrangement_lmm_log_seizures.png", plot = grid_arrangement_lmm_log_seizures, width = 12, height = 8)
#
# Model summaries
#
# Logistic regression model summary
stargazer(
model_glmer.GE, model_glmer.RL, model_glmer.PV,
title = "Logistic Regression Models",
type = "text",
out = "logistic_regression_models_summary.txt")
# Linear mixed effects models summary
stargazer(
model_lmm.GE, model_lmm.RL, model_lmm.PV,
title = "Linear Mixed Effects Models",
type = "text",
out = "linear_mixed_effects_models_summary.txt")
#
# Check residuals for LMMs
#
# Check residuals for logistic regression models
qqnorm(residuals(model_glmer.GE))
qqline(residuals(model_glmer.GE))
qqnorm(residuals(model_glmer.RL))
qqline(residuals(model_glmer.RL))
qqnorm(residuals(model_glmer.PV))
qqline(residuals(model_glmer.PV))
# Check residuals for linear mixed effects models
qqnorm(residuals(model_lmm.GE))
qqline(residuals(model_lmm.GE))
qqnorm(residuals(model_lmm.RL))
qqline(residuals(model_lmm.RL))
qqnorm(residuals(model_lmm.PV))
qqline(residuals(model_lmm.PV))
# End