-
Notifications
You must be signed in to change notification settings - Fork 0
/
99_utils.R
executable file
·577 lines (534 loc) · 17.6 KB
/
99_utils.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# required packages -------------------------------------------------------
pacman::p_load(
broom,
broom.mixed,
marginaleffects,
tidyverse
)
# simplified progress bar function for verbose code.
pbn <- function(n){
progress::progress_bar$new(
format = paste0(" (:spin) [:bar] :percent | :current / :total | elapsed: :elapsed | eta: :eta"),
total = n, clear = FALSE, width= 100)
}
# data comparing functions ------------------------------------------------
# function to detect duplicates after joining / merging two data frames
identicals_df <- function(x) {
identicals_col <- function(x) {
all(lapply(x, identical, x[[1]]))
}
identicals_group <- function(x) {
x %>% apply(2, identicals_col)
}
groupVARS <- groups(x) %>% as.character()
x %>%
dplyr::mutate(ndups = n()) %>%
dplyr::filter(ndups > 1) %>%
group_split() %>%
map_dfr(~ {
res <- identicals_group(.x)
ids <-
# dplyr::select(.x, one_of(groupVARS)) %>% dplyr::distinct() %>% paste0(collapse = " | ")
dplyr::select(.x, one_of(groupVARS), ndups) %>%
dplyr::distinct() %>%
dplyr::mutate(
ndups = ndups,
col_diffvals = paste0(names(res)[res == F], collapse = ", ")
)
})
}
# data wrangling ----------------------------------------------------------
# define outliers
outliers <- function(x, times = 1.5){ # 1.5 is default
# Inter quartile range
IQR <- (quantile(x, 0.75, na.rm = T)[[1]] - quantile(x, 0.25, na.rm = T)[[1]])
q50 <- quantile(x, 0.5, na.rm = T)[[1]]
return(x < q50 - IQR * times | x > q50 + IQR * times)
}
# define function for z-standardising variables
scale2 <- function(x, center = T, scale = T, name = deparse(substitute(x))) {
if (is.numeric(x)) {
if (center) {
x <- x - mean(x, na.rm = T)
}
if (scale) {
x <- x / sd(x, na.rm = T)
}
return(x)
} else {
warning(paste0(name, " is not numeric and will be skipped."))
return(x)
}
}
# data visuailsations ---------------------------------------------------------
# Quickly plot the distribution of groups as a barplot
bars_by_group <- function(data = xdf, y = NA, group = NA, clr = NA, order = T, lab = T) {
data %>%
dplyr::group_by({{group}}) %>%
dplyr::summarise(
"{{y}}" := mean ({{y}}, na.rm = T)
) %>%
dplyr::ungroup() -> x
if(order) {
x <- x %>%
dplyr::mutate(
"{{group}}" := {{group}} %>%
as.character() %>%
fct_reorder({{y}})
)
}
temp <- x %>%
dplyr::filter(complete.cases(.)) %>%
ggplot(aes(x = {{group}}, y = {{y}}
)) +
coord_flip() +
theme_minimal(base_family = "CMU Serif")
# ad own color scheme?
if(missing(clr)) temp <- temp + geom_col(fill = rgb(25/255,40/255,100/255))
else temp <- temp + geom_col(fill = clr) %>% return()
# ad labels?
if(lab)
temp <-
temp +
geom_text(
aes(label = round({{y}},2)),
stat = "unique",
family = "CMU Serif", size = 3,
vjust = "bottom",
position = position_nudge(y = max(x %>% select({{y}}))*0.05)
)
return(temp)
}
bars_by_group(iris, Sepal.Length, Species) + labs(title = "Example Plot")
# two-way scatterplot with a regression line plotted across the point cloud
scatterandsmooth <- function(data = xdf, x = NA, y = NA, colrs = NA){
if(missing(colrs)){
data %>%
ggplot(aes(x = {{x}}, y = {{y}})) +
theme_minimal(base_family = "CMU Serif") +
geom_smooth(method = "lm") +
geom_point() %>%
return()
} else {
data %>%
ggplot(aes(x = {{x}}, y = {{y}}, colour = {{colrs}})) +
theme_minimal(base_family = "CMU Serif") +
geom_smooth(method = "lm") +
geom_point() %>%
return()
}
}
scatterandsmooth(iris, Sepal.Length, Sepal.Width, Petal.Length) + labs(title = "Example Plot")
# Quick plot save - Save ggplot with standardised settings
save_plot <- function(plot, file, scale = 0.8, width = 225, height = 100){
ggsave(filename = file, plot = plot, device = "png",
path = NULL, scale = scale, width = width, height = height, units = "mm",
dpi = 300)
}
# Insert a ggplot layer at a certain location
insertLayer <- function(P, after=0, ...) {
# P : Plot object
# after : Position where to insert new layers, relative to existing layers
# ... : additional layers, separated by commas (,) instead of plus sign (+)
if (after < 0)
after <- after + length(P$layers)
if (!length(P$layers))
P$layers <- list(...)
else
P$layers <- append(P$layers, list(...), after)
return(P)
}
# ├ visualisations ------------------------------------------------------
## re = object of class ranef.mer
# https://github.com/jonkeane/mocapGrip/blob/master/R/ggCaterpillar.R
ggCaterpillar <- function(re, QQ=TRUE, likeDotplot=TRUE) {
require(ggplot2)
f <- function(x) {
pv <- attr(x, "postVar")
cols <- 1:(dim(pv)[1])
se <- unlist(lapply(cols, function(i) sqrt(pv[i, i, ])))
ord <- unlist(lapply(x, order)) + rep((0:(ncol(x) - 1)) * nrow(x), each=nrow(x))
pDf <- data.frame(y=unlist(x)[ord],
ci=1.96*se[ord],
nQQ=rep(qnorm(ppoints(nrow(x))), ncol(x)),
ID=factor(rep(rownames(x), ncol(x))[ord], levels=rownames(x)[ord]),
ind=gl(ncol(x), nrow(x), labels=names(x)))
if(QQ) { ## normal QQ-plot
p <- ggplot(pDf, aes(nQQ, y))
p <- p + facet_wrap(~ ind, scales="free")
p <- p + xlab("Standard normal quantiles") + ylab("Random effect quantiles")
} else { ## caterpillar dotplot
p <- ggplot(pDf, aes(ID, y)) + coord_flip()
if(likeDotplot) { ## imitate dotplot() -> same scales for random effects
p <- p + facet_wrap(~ ind)
} else { ## different scales for random effects
p <- p + facet_grid(ind ~ ., scales="free_y")
}
p <- p + xlab("Levels") + ylab("Random effects")
}
p <- p + geom_hline(yintercept=0)
p <- p + geom_errorbar(aes(ymin=y-ci, ymax=y+ci), width=0, colour="black")
p <- p + geom_point(size = 2, colour="darkred", shape = "square")
p <- p + theme_minimal(base_family = "CMU Serif")
p <- p + theme(legend.position="none")
return(p)
}
lapply(re, f)
}
# Quick model plotting function => enter summary object!
pltmymodel <- function(x){
if(isS4(x)){
x <- summary(x)
}
x$coefficients %>% {.->>x} %>% as_tibble %>% mutate(terms=rownames(x)) %>%
filter(!terms=="(Intercept)") %>%
ggplot(.,aes(y=Estimate, x=terms)) +
geom_hline(aes(yintercept = 0),color="gray") +
geom_point() + coord_flip() +
labs(x="Coefficients") +
theme_minimal()
}
# Combine several forest plots into one.
pltmymodels <- function(..., mnames=NULL,rm.int=T){
x <- list(...)
for (i in seq_along(x)){
if(isS4(x[[i]])){
x[[i]] <- summary(x[[i]])
x[[i]] <-
x[[i]]$coefficients %>%
as_tibble %>%
mutate(terms = rownames(x[[i]]$coefficients))
if(is.null(mnames)) {x[[i]]$model <- glue("m_{i}")
} else {x[[i]]$model <- mnames[i]}
}
}
x <- data.table::rbindlist(x)
if(rm.int){x <- x %>% filter(!terms=="(Intercept)")}
x %>% as_tibble %>%
rename(p.value=`Pr(>|t|)`) %>%
ggplot(.,aes(y=Estimate, x=terms, color = model)) +
geom_hline(aes(yintercept = 0),color="gray") +
geom_point(aes(alpha = p.value<0.01), shape=15, size = 3, position = position_dodge(1)) +
coord_flip() +
labs(x="Coefficients") +
scale_alpha_manual(values=c(0.5,1)) +
theme_minimal()
}
# Change the position of the interplot histogram
set_interp_hist_to <- function(intp, newymin = 0, histo_height = 0.3) {
rect_data <- layer_data(intp, 1)
rect_data$ymax <- (rect_data$ymax - rect_data$ymin) / (max(rect_data$ymax) - min(rect_data$ymin)) * histo_height + newymin
rect_data$ymin <- rect_data$ymin - rect_data$ymin + newymin
intp$layers[[1]] <- NULL
p <- intp +
geom_rect(
data = rect_data,
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
alpha = 0,
colour = "gray50"
)
p$layers <- c(geom_hline(yintercept = 0, colour = "gray50"), p$layers)
return(p)
}
# function to create a combined interplot
plot_inter_combo_lin <- function(m_a,
m_b,
var1a,
var1b,
var2,
var1alab = "Negativity",
var1blab = "Incivility",
ymin,
ymax,
histo_height = 0.3,
ylab = "Average marginal effect of campaign\ndimension on ptv for sponsor",
xlab = "Distance to closest competitor",
newymin) {
# create interplots
ma <- interplot::interplot(
m = m_a,
var1 = var1a,
var2 = var2,
hist = T,
)
mb <- interplot::interplot(
m = m_b,
var1 = var1b,
var2 = var2,
hist = T,
)
# compile the plots into one grid
gridExtra::grid.arrange(
(ma %>% set_interp_hist_to(ymin, histo_height = histo_height)) + ylim(ymin, ymax) + labs(title = var1alab),
(mb %>% set_interp_hist_to(ymin, histo_height = histo_height)) + ylim(ymin, ymax) + labs(title = var1blab),
nrow = 1,
left = textGrob(
ylab,
rot = 90,
vjust = 1,
gp = gpar(fontfamily = FONT, fontsize = 9)
),
bottom = textGrob(
xlab,
gp = gpar(fontfamily = FONT)
)
)
}
# Visualisation
get_ran_slopes <- function(
m1, m2, # models 1 & 2
t1, t2, # titles for 1 & 2
l1, l2 # labels of slope for 1 & 2
) {
# Getting the plotting data by dimension
# First we need to estimate the random effects + CIs for both models
ranefplot_n <- ranef(m1) %>% ggCaterpillar(QQ = FALSE)
ranefplot_i <- ranef(m2) %>% ggCaterpillar(QQ = FALSE)
temp <-
as_tibble(ranefplot_n$cntry_short$data) %>%
full_join(as_tibble(ranefplot_i$cntry_short$data)) %>%
filter(!ind == "(Intercept)") %>%
mutate(
conf.low = y - ci,
conf.high = y + ci,
)
# Ad fixed intercept and slope
temp2 <-
broom.mixed::tidy(m1, conf.int = T) %>%
full_join(broom.mixed::tidy(m2, conf.int = T)) %>%
filter(term %in% c(t1, t2)) %>%
mutate(
ID = "Fixed Effect" %>% as.factor(),
y = estimate,
) %>%
select(ID, conf.low, conf.high, ind = term, y, estimate) %>%
full_join(temp) %>%
mutate(
ind = ifelse(ind == t1, l1, l2),
)
# order by slopes not intercepts
# first negativity, then incivility
temp2 <-
temp2 %>%
mutate(
ID = as.character(ID),
# this is a workaround so as to be able to truly let the countries be ordered separately by facet of the plot below
indID = ifelse(as.character(ind) == l1, paste(ID, " "), ID),
ind = ind %>% fct_relevel(l1),
) %>%
mutate(
indID = indID %>% fct_reorder(y),
indID = indID %>% fct_relevel("Fixed Effect", "Fixed Effect ", after = Inf)
) %>%
arrange(indID)
ranplot <-
temp2 %>%
mutate(
ID = ID %>%
fct_relevel(
levels(temp2$ID) %[out~% "Fixed",
"Fixed Effect"
)
) %>%
# remove FE intercept
mutate(
y = ifelse(ind %in~% "Intercept" & ID == "Fixed Effect", NA, y),
estimate = ifelse(ind %in~% "Intercept" & ID == "Fixed Effect", NA, estimate),
conf.low = ifelse(ind %in~% "Intercept" & ID == "Fixed Effect", NA, conf.low),
conf.high = ifelse(ind %in~% "Intercept" & ID == "Fixed Effect", NA, conf.high),
) %>%
ggplot(aes(x = indID, y = y)) +
geom_hline(aes(yintercept = estimate), colour = "darkred", linetype = "dashed", alpha = 0.6) +
geom_hline(aes(yintercept = 0)) +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0) +
geom_point(shape = "square", colour = "darkred") +
coord_flip() +
facet_wrap(~ind, scales = "free_y") +
labs(x = "", y = "Random Intercepts")
return(ranplot)
}
plot_marg_pred <- function(model,
terms,
xlab = "Overall negativity of a party's campaign",
ylab = "Predicted PTV for sponsor",
collab = "Availability of alternatives") {
marg_preds <-
ggeffects::ggpredict(model,
terms = terms
)
marg_preds %>%
ggplot(aes(x = x, group = group)) +
geom_ribbon(aes(ymin = conf.low, ymax = conf.high),
alpha = 0.7,
fill = "gray"
) +
geom_line(aes(colour = group, y = predicted)) +
scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, 2), ) +
theme(legend.position = "top") +
labs(
x = xlab,
y = ylab,
colour = collab
)
}
plot_interplot <- function(model, var1, var2, hist = F, xlab, ylab) {
pind <- interplot::interplot(
m = model,
var1 = var1,
var2 = var2,
hist = hist
)
pindp <-
pind$data %>%
mutate(x = ifelse(
fake == 0,
"Better alternative(s) available",
"Sponsor is best alternative"
)) %>%
ggplot(aes(x = x, y = coef1)) +
geom_hline(aes(yintercept = 0), colour = "darkgray") +
geom_point() +
geom_errorbar(aes(ymin = lb, ymax = ub), width = 0.1) +
labs(
x = xlab,
y = ylab
)
}
# Function for a combined model Interplot
plot_interplot_combo <- function(model1,
model2,
var1a,
var1b,
var2,
dim1,
dim2,
histos = F,
xlab,
ylab,
collab) {
pind_a <- interplot::interplot(
m = model1,
var1 = var1a,
var2 = var2,
hist = histos
)
pind_b <- interplot::interplot(
m = model2,
var1 = var1b,
var2 = var2,
hist = histos
)
temp_df <- dplyr::bind_rows(
pind_a$data %>% mutate(dim = dim1),
pind_b$data %>% mutate(dim = dim2)
)
pindp <-
temp_df %>%
mutate(
x = ifelse(
fake == 0,
"Better alternative(s) available",
"Sponsor is best alternative"
) %>% factor(., levels = c(
"Sponsor is best alternative",
"Better alternative(s) available"
)),
dim = dim %>% fct_relevel(dim1)
) %>%
ggplot(aes(
x = dim,
y = coef1,
fill = x,
colour = x
)) +
geom_hline(aes(yintercept = 0), colour = "darkgray") +
geom_point(aes(shape = x), position = position_dodge(0.5)) +
scale_shape_manual(values = c("square", "triangle")) +
geom_errorbar(aes(ymin = lb, ymax = ub),
width = 0.1,
position = position_dodge(0.5)
) +
labs(
x = xlab,
y = ylab,
fill = collab,
colour = collab,
shape = collab
)
}
plot_interplot_combo <- compiler::cmpfun(plot_interplot_combo)
# ├ tables ------------------------------------------------------------------
# Save the variance at the respective levels as table
getvariances <- function(model, lvlss = NULL){
#lvlss <- c("cntry", "cntry", "outparty_code", "inparty_code", "id_unique", "Residual")
m0sum <- summary(model)
variance <- m0sum$varcor %>% as_tibble() %>% rename(level=grp) %>%
full_join(tibble(level = names(m0sum$ngrps), ngroups = m0sum$ngrps))
variance$ngroups[variance$level=="Residual"] <-nobs(m0)
variance <-
variance %>%
arrange(ngroups) %>%
select(
level, ngroups, vcov,
) %>%
mutate(
pct = paste0(format(round(vcov/sum(vcov)*100,2), nsmall=2), "%"),
)
if(exists("lvlss")) variance$level <- variance$level %>% fct_relevel(lvlss)
variance %>% arrange(level) %>%
mutate(
level = recode(level,
cntr="Country",
inparty_code="In-party",
outparty_code="Out-party",
id_unique = "Individuals",
Residual = "Dyads"
)
) %>%
rename('Levels of aggregation' = level, 'Observations' = ngroups, 'Variance' = vcov, 'Proportion of overall variance' = pct)
}
# Function for Regression table output
nicelyformatted_table <- function(hlinepos = 10,
models,
hlinepos1,
hlinepos2,
output,
FONT = "EB Garamond",
...) {
modelsummary(
models = models,
stars = T,
fmt = function(x) format(round(x, 3), nsmall = 3),
# title = "Effect of NC on respondent's propensity to vote for the attacker",
shape = group + term ~ model,
estimate = "{estimate}{stars}{ifelse(conf.low %in~% 'NA','',paste0(' [',conf.low,',',conf.high,']'))}",
statistic = NULL,
output = "flextable",
gof_map = gm,
...
) %>%
flextable::font(
fontname = FONT,
part = "all"
) %>%
flextable::fontsize(9) %>%
flextable::autofit() %>%
hline(hlinepos1) %>%
hline(hlinepos2) %>%
fit_to_width(10) %>%
align(i = NULL, j = NULL, align = "center", part = "body") %>%
align(i = 1, j = NULL, align = "center", part = "header") %>%
align(i = NULL, j = 1:2, align = "left", part = "body") %>%
flextable::save_as_docx(
path = output,
pr_section = prop_section(
page_size = page_size(
orient = "landscape",
width = 8.3, height = 11.7
),
type = "continuous",
page_margins = page_mar()
)
)
beepr::beep(10)
}