-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrps_visualisation.R
More file actions
280 lines (227 loc) · 16.4 KB
/
crps_visualisation.R
File metadata and controls
280 lines (227 loc) · 16.4 KB
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
## clear environment
rm(list = ls())
## set working directory
current_path <- rstudioapi::getActiveDocumentContext()$path # get the path of your current open file
setwd(dirname(current_path))
## load packages
library(rstudioapi)
library(arrow)
library(dplyr)
library(scoringRules)
library(tidyverse)
library(ggplot2)
# read-in predictive samples
# renv::snapshot()
# renv::update()
# renv::restore()
## -----
# read-in predictive samples from benchmark models and submitted forecasts
## -----
benchmark_names <- c("boot_240", "conflictology", "last", "zero")
submissions_names <- c("bodentien_rueter_negbin", "bodentien_rueter_neuralnet", "conflictforecast_v2", "Neg_Bin_GAM", "Neg_Bin_GLMM",
"P_GAM", "P_GLMM", "quantile_forecast", "ShapeFinder", "submission_final_gpcmm", "submission_final_hpmm",
"submission_final_omm", "submission_muchlinski_thornhill", "tft", "TW_GAM", "TW_GLMM", "unito_transformer")
model_names <- c(benchmark_names, submissions_names)
n_models <- length(model_names)
appendix_names <- paste0("_cm_Y20", c(18, 19, 20, 21, 22, 23, 24))
predictive_samples <- list()
for (m in 1:length(model_names)) {
model_files <- paste0("../Data/predictions/", model_names[m], "/", model_names[m], appendix_names, ".parquet")
predictive_samples[[m]] <- lapply(model_files, arrow::read_parquet)
predictive_samples[[m]] <- bind_rows(predictive_samples[[m]])
}
names(predictive_samples) <- model_names
## -----
# read-in actual observations
## -----
observations_files <- paste0("../Data/observations/cm_actuals_20", c(18, 19, 20, 21, 22, 23, 24), ".parquet")
observations <- lapply(observations_files, arrow::read_parquet)
# names(observations) <- paste0("Y20", c(18, 19, 20, 21, 22, 23, 24))
# create one dataframe from the list
observations <- bind_rows(observations)
## -----
# compute crps on benchmark models and submitted forecasts for each country-month pair for 2018 to 2023
## -----
country_ids <- unique(observations$country_id)
actuals_ids <- 457:528 # month_ids for 01-2018, 02-2018, ..., 12-2023
cm_pairs <- cbind(rep(country_ids, each = length(actuals_ids)),
rep(actuals_ids, length(country_ids)))
models_crps <- list()
# for (m in 1:n_models) {
# crps_m <- apply(cm_pairs, 1, function(cm_pair) {
# print(paste0("Benchmark/model (", m, "/", n_models, "): ", model_names[m], ", country: ", cm_pair[1], ", month: ", cm_pair[2]))
# true_observation <- observations %>%
# filter(country_id == cm_pair[1] & month_id == cm_pair[2]) %>%
# select(outcome)
# pred_sample <- predictive_samples[[m]] %>%
# filter(country_id == cm_pair[1] & month_id == cm_pair[2]) %>%
# select(outcome)
# crps_sample(y = unlist(true_observation),
# dat = unlist(pred_sample))
# })
# models_crps[[m]] <- data.frame("country_id" = cm_pairs[,1],
# "month_id" = cm_pairs[,2],
# "crps" = crps_m)
# }
# names(models_crps) <- model_names
# save(models_crps, file = "output/models_crps.RData")
load("output/models_crps.RData")
## -----
# compute Brier score on benchmark models and submitted forecasts for each country-month pair for 2018 to 2023
## -----
# compute empirical probabilities
models_predictive_probabilities <- lapply(predictive_samples, function(pred_sample) {
pred_sample %>%
mutate("predicted_conflict" = outcome > 0) %>%
group_by(country_id, month_id) %>%
summarise(predictive_probability = mean(predicted_conflict))
})
# merge with observations and compute summands of brier score for each model, month and country
models_brier <- lapply(models_predictive_probabilities, function(pred_probs) {
observations %>% inner_join(pred_probs,
by=c("country_id"="country_id", "month_id"="month_id")) %>%
mutate("actual_conflict" = outcome > 0) %>%
mutate("brier" = (actual_conflict - predictive_probability)^2) %>%
select("country_id", "month_id", "outcome", "actual_conflict", "predictive_probability", "brier")
})
# save(models_brier, file = "output/models_brier.RData")
# load("output/models_brier.RData")
## -----
# label observations as either peace ("peace"), conflict onset ("onset"), ongoing conflict ("conflict") or end of conflict ("deescalation")
# VARIANT 1: reference period is previous month
# VARIANT 2: reference period is previous year (previous 12 months)
## -----
# additionally read in all (previous) observations from 01-2017 to 12-2017 for defining the conflict situation of respective countries
# since month 457 is 01-2018, we keep months 445:456 (01-2017 to 12-2017)
observations_previous <- arrow::read_parquet("../Data/observations/cm_features.parquet") %>%
select(month_id, country_id, ged_sb) %>%
filter(month_id %in% 445:456) %>%
rename(outcome = ged_sb)
observations_all <- rbind(observations_previous[,colnames(observations)], observations) %>%
arrange(country_id, month_id)
actual_conflict <- observations_all %>%
filter(country_id %in% country_ids) %>%
filter(month_id %in% actuals_ids) %>%
mutate(conflict = outcome>0)
prev_conflict <- list_cbind(lapply(1:12, function(prev_month) {
observations_all %>%
filter(country_id %in% country_ids) %>%
filter(month_id %in% (actuals_ids - prev_month)) %>%
transmute(conflict = outcome>0)
}))
names(prev_conflict) <- paste0("lag_", 1:12)
conflict_prev_month <- prev_conflict$lag_1
conflict_prev_year <- rowSums(prev_conflict)>0 # label previous period as conflict period, if at least one month had >0 fatalities
situation_month <- ifelse(!actual_conflict$conflict & !conflict_prev_month, "peace", # no conflict, no conflict
ifelse(actual_conflict$conflict & !conflict_prev_month, "onset", # no conflict, conflict
ifelse(actual_conflict$conflict & conflict_prev_month, "conflict", # conflict, conflict
"deescalation"))) # conflict, no conflict
situation_year <- ifelse(!actual_conflict$conflict & !conflict_prev_year, "peace",
ifelse(actual_conflict$conflict & !conflict_prev_year, "onset",
ifelse(actual_conflict$conflict & conflict_prev_year, "conflict",
"deescalation")))
sum(situation_month != situation_year)
conflict_situations <- data.frame(actual_conflict[,c(2,3)], "situation_month" = as.vector(situation_month), "situation_year" = as.vector(situation_year))
## -----
## Plot CRPS values by conflict situation
## -----
models_crps_conflict <- models_crps %>% reduce(left_join, c("country_id", "month_id"))
names(models_crps_conflict) <- c("country_id", "month_id", model_names)
models_crps_conflict <- list(models_crps_conflict, conflict_situations) %>% reduce(left_join, c("country_id", "month_id"))
models_crps_conflict_month <- models_crps_conflict %>% select(!c("country_id", "month_id", "situation_year")) %>% group_by(situation_month) %>% summarise_all(sum)
models_crps_conflict_year <- models_crps_conflict %>% select(!c("country_id", "month_id", "situation_month")) %>% group_by(situation_year) %>% summarise_all(sum)
models_crps_conflict_month[,2:ncol(models_crps_conflict_month)] <- models_crps_conflict_month[,2:ncol(models_crps_conflict_month)] / nrow(models_crps_conflict) # compute contributions to average CRPS
models_crps_conflict_year[,2:ncol(models_crps_conflict_year)] <- models_crps_conflict_year[,2:ncol(models_crps_conflict_year)] / nrow(models_crps_conflict) # compute contributions to average CRPS
colSums(models_crps_conflict_month[,2:ncol(models_crps_conflict_month)])
# create ggplot data frames
crps_month <- data.frame("CRPS" = unlist(c(models_crps_conflict_month[,2:ncol(models_crps_conflict_month)])),
"Situation" = rep(models_crps_conflict_month$situation_month, ncol(models_crps_conflict_month)-1),
"Model" = rep(names(models_crps_conflict_month)[2:ncol(models_crps_conflict_month)], each = 4))
crps_year <- data.frame("CRPS" = unlist(c(models_crps_conflict_year[,2:ncol(models_crps_conflict_year)])),
"Situation" = rep(models_crps_conflict_year$situation_year, ncol(models_crps_conflict_year)-1),
"Model" = rep(names(models_crps_conflict_year)[2:ncol(models_crps_conflict_year)], each = 4))
# exclude 3 largest models with insane CRPS values "Neg_Bin_GAM", "P_GAM", "TW_GAM"
ggplot(crps_month[-which(crps_month$Model %in% c("Neg_Bin_GAM", "P_GAM", "TW_GAM")), ], aes(fill = Situation, y = Model, x = CRPS)) +
geom_bar(position = "stack", stat = "identity") +
# xlim(0.0, 1) +
scale_fill_manual("legend", values = c("conflict" = "#a22223", "deescalation" = "#009682", "onset" = "#df9b1b", "peace" = "#4664aa")) +
ggtitle("Contribution to average CRPS (01-2018 to 12-2023, all countries) per conflict situation, reference: previous month") +
xlab("Contribution to average CRPS per conflict situation")
ggplot(crps_year[-which(crps_year$Model %in% c("Neg_Bin_GAM", "P_GAM", "TW_GAM")), ], aes(fill = Situation, y = Model, x = CRPS)) +
geom_bar(position = "stack", stat = "identity") +
# xlim(0.0, 1) +
scale_fill_manual("legend", values = c("conflict" = "#a22223", "deescalation" = "#009682", "onset" = "#df9b1b", "peace" = "#4664aa")) +
ggtitle("Contribution to average CRPS (01-2018 to 12-2023, all countries) per conflict situation, reference: previous year") +
xlab("Contribution to average CRPS per conflict situation")
## -----
## Plot Brier values by conflict situation
## -----
models_brier_conflict <- lapply(models_brier, function(m) m %>% select("country_id", "month_id", "brier")) %>%
reduce(left_join, c("country_id", "month_id"))
names(models_brier_conflict) <- c("country_id", "month_id", model_names)
models_brier_conflict <- list(models_brier_conflict, conflict_situations) %>% reduce(left_join, c("country_id", "month_id"))
## -----
# a) create ggplots of contributions to average Brier scores for all conflict situations
## -----
models_brier_conflict_month <- models_brier_conflict %>% select(!c("country_id", "month_id", "situation_year")) %>% group_by(situation_month) %>% summarise_all(sum)
models_brier_conflict_year <- models_brier_conflict %>% select(!c("country_id", "month_id", "situation_month")) %>% group_by(situation_year) %>% summarise_all(sum)
models_brier_conflict_month[,2:ncol(models_brier_conflict_month)] <- models_brier_conflict_month[,2:ncol(models_brier_conflict_month)] / nrow(models_brier_conflict) # compute contributions to average brier
models_brier_conflict_year[,2:ncol(models_brier_conflict_year)] <- models_brier_conflict_year[,2:ncol(models_brier_conflict_year)] / nrow(models_brier_conflict) # compute contributions to average brier
brier_month <- data.frame("Brier" = unlist(c(models_brier_conflict_month[,2:ncol(models_brier_conflict_month)])),
"Situation" = rep(models_brier_conflict_month$situation_month, ncol(models_brier_conflict_month)-1),
"Model" = rep(names(models_brier_conflict_month)[2:ncol(models_brier_conflict_month)], each = 4))
brier_year <- data.frame("Brier" = unlist(c(models_brier_conflict_year[,2:ncol(models_brier_conflict_year)])),
"Situation" = rep(models_brier_conflict_year$situation_year, ncol(models_brier_conflict_year)-1),
"Model" = rep(names(models_brier_conflict_year)[2:ncol(models_brier_conflict_year)], each = 4))
# exclude 3 largest models with insane Brier values "Neg_Bin_GAM", "P_GAM", "TW_GAM"
ggplot(brier_month[-which(brier_month$Model %in% c("Neg_Bin_GAM", "P_GAM", "TW_GAM")), ], aes(fill = Situation, y = Model, x = Brier)) +
geom_bar(position = "stack", stat = "identity") +
xlim(0.0, 1) +
scale_fill_manual("legend", values = c("conflict" = "#a22223", "deescalation" = "#009682", "onset" = "#df9b1b", "peace" = "#4664aa")) +
ggtitle("Contribution to average Brier score (01-2018 to 12-2023, all countries) per conflict situation, reference: previous month") +
xlab("Contribution to average Brier score per conflict situation")
ggplot(brier_year[-which(brier_year$Model %in% c("Neg_Bin_GAM", "P_GAM", "TW_GAM")), ], aes(fill = Situation, y = Model, x = Brier)) +
geom_bar(position = "stack", stat = "identity") +
xlim(0.0, 1) +
ggtitle("Contribution to average Brier score (01-2018 to 12-2023, all countries) per conflict situation, reference: previous year") +
scale_fill_manual("legend", values = c("conflict" = "#a22223", "deescalation" = "#009682", "onset" = "#df9b1b", "peace" = "#4664aa")) +
xlab("Contribution to average Brier score per conflict situation")
## -----
# b) create ggplots of contributions to average Brier scores for previously no conflict, i.e. "peace" and "onset"
## -----
models_brier_prev_peace_month <- models_brier_conflict %>% select(!c("country_id", "month_id", "situation_year")) %>% group_by(situation_month) %>% summarise_all(sum) %>% filter(situation_month %in% c("peace", "onset"))
models_brier_prev_peace_year <- models_brier_conflict %>% select(!c("country_id", "month_id", "situation_month")) %>% group_by(situation_year) %>% summarise_all(sum) %>% filter(situation_year %in% c("peace", "onset"))
models_brier_prev_peace_month[,2:ncol(models_brier_prev_peace_month)] <- models_brier_prev_peace_month[,2:ncol(models_brier_prev_peace_month)] / nrow(models_brier_conflict %>% filter(situation_month %in% c("peace", "onset"))) # compute contributions to average brier
models_brier_prev_peace_year[,2:ncol(models_brier_prev_peace_year)] <- models_brier_prev_peace_year[,2:ncol(models_brier_conflict_year)] / nrow(models_brier_conflict %>% filter(situation_year %in% c("peace", "onset"))) # compute contributions to average brier
brier_prev_peace_month <- data.frame("Brier" = unlist(c(models_brier_prev_peace_month[,2:ncol(models_brier_prev_peace_month)])),
"Situation" = rep(models_brier_prev_peace_month$situation_month, ncol(models_brier_prev_peace_month)-1),
"Model" = rep(names(models_brier_prev_peace_month)[2:ncol(models_brier_prev_peace_month)], each = 2))
brier_prev_peace_year <- data.frame("Brier" = unlist(c(models_brier_prev_peace_year[,2:ncol(models_brier_prev_peace_year)])),
"Situation" = rep(models_brier_prev_peace_year$situation_year, ncol(models_brier_prev_peace_year)-1),
"Model" = rep(names(models_brier_prev_peace_year)[2:ncol(models_brier_prev_peace_year)], each = 2))
# exclude 3 largest models with insane Brier values "Neg_Bin_GAM", "P_GAM", "TW_GAM"
ggplot(brier_prev_peace_month[-which(brier_prev_peace_month$Model %in% c("Neg_Bin_GAM", "P_GAM", "TW_GAM")), ], aes(fill = Situation, y = Model, x = Brier)) +
geom_bar(position = "stack", stat = "identity") +
xlim(0.0, 1) +
scale_fill_manual("legend", values = c("conflict" = "#a22223", "deescalation" = "#009682", "onset" = "#df9b1b", "peace" = "#4664aa")) +
ggtitle("Contribution to average Brier score (01-2018 to 12-2023, all countries) in case of previous peace, reference: previous month") +
xlab("Contribution to average Brier score per conflict situation")
ggplot(brier_prev_peace_year[-which(brier_prev_peace_year$Model %in% c("Neg_Bin_GAM", "P_GAM", "TW_GAM")), ], aes(fill = Situation, y = Model, x = Brier)) +
geom_bar(position = "stack", stat = "identity") +
xlim(0.0, 1) +
ggtitle("Contribution to average Brier score (01-2018 to 12-2023, all countries) in case of previous peace, reference: previous year") +
scale_fill_manual("legend", values = c("conflict" = "#a22223", "deescalation" = "#009682", "onset" = "#df9b1b", "peace" = "#4664aa")) +
xlab("Contribution to average Brier score per conflict situation")
## -----
# MANUAL CHECKS
## -----
# Brier score values for previous peace
sort(colSums(models_brier_prev_peace_month[,2:ncol(models_brier_prev_peace_month)]))
sort(colSums(models_brier_prev_peace_year[,2:ncol(models_brier_prev_peace_year)]))
# Poisson baseline: Where do differing Brier scores compared to zero benchmark come from?
last_comb <- observations %>% mutate("pois_param" = outcome) %>% mutate("pois_month" = month_id+1) %>%
select(c("country_id", "pois_month", "pois_param")) %>%
inner_join(predictive_samples$last, by=c("country_id"="country_id", "pois_month"="month_id")) %>%
filter(pois_param == 0)
sum(last_comb$outcome>0, decreasing = TRUE) # some values are much greater than 0 although 0 should be the parameter...
sum(last_comb$outcome==0, decreasing = TRUE) # some values are much greater than 0 although 0 should be the parameter...