-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.R
479 lines (359 loc) · 19.7 KB
/
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
# Functions to analyse human suspicion responses from the cards task (Zheng et al., 2024)
# Created using R version 4.1.3.
# install.packages("data.table")
# install.packages("lme4")
# install.packages("lmerTest")
# install.packages("lm.beta")
# install.packages("Rmisc")
# install.packages("BayesFactor")
# install.packages("tidyr")
# install.packages("car")
# install.packages("ggpubr")
# install.packages("cowplot")
# install.packages("corrplot")
library(data.table)
library(lme4)
library(lmerTest)
library(lm.beta)
library(Rmisc)
library(BayesFactor)
library(tidyr)
library(car)
library(ggpubr)
library(cowplot)
library(corrplot)
INCLUDE <- c("PID", # unique subject ID
"normed_signed_e_v", # signed expectation violation, i.e., likelihood that the other player reported red or blue (continuous scale)
"normed_unsigned_e_v", # surprise, i.e., unsigned expectation violation (continuous scale)
"subject_lied", # whether the subject lied (TRUE) on the given trial or not (FALSE); Boolean var, will be treated as discrete variable
"subject_lost", # whether the subject won (-1), tied (0) or lost (1) the trial
"suspicion_rating", # human suspicion
"pp_lied") # perfect lie detector
PARAM_NAMES <- c("normed_signed_e_v",
"normed_unsigned_e_v",
"subject_liedTRUE",
"subject_lostTRUE",
"outcome_blueTRUE")
PARAM_BI_COMBIS <- list(c("normed_signed_e_v", "normed_unsigned_e_v"),
c("normed_signed_e_v", "subject_lied"),
c("normed_signed_e_v", "subject_lost"),
c("normed_unsigned_e_v", "subject_lied"),
c("normed_unsigned_e_v", "subject_lost"),
c("subject_lied", "subject_lost"))
COLNAMES_CORR_MATRIX <- sapply(PARAM_BI_COMBIS, function(x){paste(x[1], x[2], sep="_")})
MODELS_INDIV <- c( "suspicion_rating ~ normed_signed_e_v",
"suspicion_rating ~ normed_unsigned_e_v",
"suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v",
"suspicion_rating ~ subject_lied",
"suspicion_rating ~ normed_signed_e_v + subject_lied",
"suspicion_rating ~ normed_unsigned_e_v + subject_lied",
"suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v + subject_lied",
"suspicion_rating ~ subject_lost",
"suspicion_rating ~ normed_signed_e_v + subject_lost",
"suspicion_rating ~ normed_unsigned_e_v + subject_lost",
"suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v + subject_lost",
"suspicion_rating ~ subject_lied + subject_lost",
"suspicion_rating ~ normed_signed_e_v + subject_lied + subject_lost",
"suspicion_rating ~ normed_unsigned_e_v + subject_lied + subject_lost",
"suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v + subject_lied + subject_lost")
MODELS_PERFECT <- c( "pp_lied ~ normed_signed_e_v",
"pp_lied ~ normed_unsigned_e_v",
"pp_lied ~ normed_signed_e_v + normed_unsigned_e_v",
"pp_lied ~ subject_lied",
"pp_lied ~ normed_signed_e_v + subject_lied",
"pp_lied ~ normed_unsigned_e_v + subject_lied",
"pp_lied ~ normed_signed_e_v + normed_unsigned_e_v + subject_lied",
"pp_lied ~ subject_lost",
"pp_lied ~ normed_signed_e_v + subject_lost",
"pp_lied ~ normed_unsigned_e_v + subject_lost",
"pp_lied ~ normed_signed_e_v + normed_unsigned_e_v + subject_lost",
"pp_lied ~ subject_lied + subject_lost",
"pp_lied ~ normed_signed_e_v + subject_lied + subject_lost",
"pp_lied ~ normed_unsigned_e_v + subject_lied + subject_lost",
"pp_lied ~ normed_signed_e_v + normed_unsigned_e_v + subject_lied + subject_lost")
define_models <- function(params){## prepare binary grid to get all possible combinations of parameters
params_grid <- expand.grid(0:1, 0:1, 0:1, 0:1) # needs to correspond to number of unique parameters
names(params_grid) <- params # add parameters as column names
## check if every row has at least one parameter
rowSums(params_grid)
params_grid <- params_grid[-1, ] # first one has 0 parameters, so drop it
params_combi <- apply(params_grid, 1, function(i) which(i == 1)) # get list of parameters in each defined model
## write all model fomrulas in lmer format and add to list
models <- c()
for (combi in params_combi) {
model <- paste("suspicion_rating ~", paste0(names(combi), collapse=' + '), "+ (1 +", paste0(names(combi), collapse=' + '), "| PID)")
models <- c(models, model)
}
return(models)
}
lmer_fit <- function(data, models) {
# prepare empty data tables to append to
fit_estimates_std <- data.table()
fit_result <- data.table()
for (i in 1:length(models)){
model <- models[i] # get model formula from list of models
print(paste0(i, " ", model)) # to check how far into the process we are when running the function
tryCatch(
{
fit <- lmer(model, data = data, REML = FALSE, control=lmerControl(optimizer="bobyqa")) # fit linear mixed-effects model according to formula
},
warning=function(w) {
print(w)
}
)
# compute standardised parameter estimates
sdy <- sd(attr(fit, "resp")$y)
sdx <- apply(attr(fit, "pp")$X, 2, sd)
sc <- fixef(fit)*sdx/sdy
std_estimates <- data.frame(t(sc))
fit_estimates_std <- rbindlist(list(fit_estimates_std, std_estimates), fill = TRUE) # add the std. estimates to the data.table
# get overall fit indicators
r <- data.table(model=model,
BIC=BIC(fit),
AIC=AIC(fit))
fit_result <- rbindlist(list(fit_result, r))
}
return(cbind(fit_result, fit_estimates_std)) # combine all output before returning
}
BIC_weights <- function(BIC_array) {
tmp_delta_bics <- abs(BIC_array - min(BIC_array))
return(exp(-.5*tmp_delta_bics)/sum(exp(-.5*tmp_delta_bics)))
}
bayesian_lmer_95ci <- function(data, models, param_names){
n_models_w_parameter <- round(length(models) / 2)
lmer_fits <- lmer_fit(data, models)
lmer_fits$BIC_weights <- BIC_weights(lmer_fits$BIC)
# BIC MODEL PROBABILITY WEGIHTS #
tmp_BICweighted_params <- lmer_fits[, ..param_names] * lmer_fits$BIC_weights
weighted_factors <- apply(tmp_BICweighted_params, MARGIN = 2, function(x) { sum(x, na.rm = TRUE)})
weighted_factors_sd <- apply(tmp_BICweighted_params, MARGIN = 2, function(x) { sd(x, na.rm = TRUE)})
# 95%-CIs
ci_ub <- weighted_factors + 1.96 * (weighted_factors_sd / sqrt(n_models_w_parameter))
ci_lb <- weighted_factors - 1.96 * (weighted_factors_sd / sqrt(n_models_w_parameter))
human_CIs <- data.frame(mean=weighted_factors, sd=weighted_factors_sd, upper=ci_ub, lower=ci_lb)
# human_CIs$param <- c("signed expectation violation", "unsigned expectation violation", "lying oneself", "losing")
# human_CIs$param <- factor(human_CIs$param)
return(human_CIs)
}
lm_fit <- function(data, models) {
# prepare empty data tables to append to
fit_estimates_std <- data.table()
fit_result <- data.table()
for (i in 1:length(models)){
model <- models[i] # get model formula from list of models
print(paste0(i, " ", model)) # to check how far into the process we are when running the function
tryCatch(
{
fit <- lm(model, data = data)
}, warning=function(w) {
print(w)
}
)
# get standardised beta coefficients)
coefs <- t(data.frame(lm.beta(fit)$standardized.coefficients))
fit_estimates_std <- rbindlist(list(fit_estimates_std, data.table(coefs)), fill = T)
# get overall fit indicators
r <- data.table(model=model,
BIC=BIC(fit),
AIC=AIC(fit),
R2=summary(fit)$r.squared)
fit_result <- rbindlist(list(fit_result, r))
}
return(cbind(fit_result, fit_estimates_std)) # combine all output before returning ; fit_estimates_std ; fit_estimates_std
}
bayesian_lm_95ci <- function(data, models, param_names, n_models_w_parameter){
lm_fits <- lm_fit(data, models)
lm_fits$BIC_weights <- BIC_weights(lm_fits$BIC)
# BIC MODEL PROBABILITY WEGIHTS #
tmp_BICweighted_params <- lm_fits[, ..param_names] * lm_fits$BIC_weights
weighted_factors <- apply(tmp_BICweighted_params, MARGIN = 2, function(x) { sum(x, na.rm = TRUE)})
weighted_factors_sd <- apply(tmp_BICweighted_params, MARGIN = 2, function(x) { sd(x, na.rm = TRUE)})
weighted_factors_se <- weighted_factors_sd / sqrt(n_models_w_parameter)
# 95%-CIs
ci_ub <- weighted_factors + 1.96 * weighted_factors_se
ci_lb <- weighted_factors - 1.96 * weighted_factors_se
CIs <- data.frame(mean=weighted_factors, sd=weighted_factors_sd, se=weighted_factors_se, upper=ci_ub, lower=ci_lb)
# CIs$param <- c("signed expectation violation", "unsigned expectation violation", "lying oneself", "losing")
# CIs$param <- factor(CIs$param)
return(CIs)
}
lm_fit_indiv_std <- function(data, uuids) {
winning_formula <- "suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v + subject_lied + subject_lost"
winning_formula_noloss <- "suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v + subject_lied"
winning_formula_nolies <- "suspicion_rating ~ normed_signed_e_v + normed_unsigned_e_v + subject_lost"
result <- data.table()
for (i in 1:length(uuids)){
uuid <- uuids[i]
print(paste(i, uuid))
data_subj <- data[uuid]
formula <- ifelse(sum(data_subj$subject_lied) == 0, winning_formula_nolies,
ifelse(sum(data_subj$subject_lost) == 0, winning_formula_noloss, winning_formula))
data_subj$subject_lost <- as.factor(data_subj$subject_lost)
data_subj$subject_lied <- as.factor(data_subj$subject_lied)
fit <- lm(formula, data = data_subj) # fit winning model
coefs <- t(data.frame(lm.beta(fit)$standardized.coefficients)) # get standardised beta coefficients)
coefs <- data.table(coefs)
coefs$PID <- uuid
result <- rbindlist(list(result, coefs), fill = TRUE) # add to list of individual fit results
}
return(result)
}
cv_bayesian_avg_lmer <- function(data, uuids, models, n_models_w_parameter){
cv_bay_model_ci <- data.table()
for (i in 1:length(uuids)){
print(i)
uuid <- uuids[i]
tmp <- data[data$PID != uuid, ]
lmer_fits <- lmer_fit(tmp, models)
lmer_fits$BIC_weights <- BIC_weights(lmer_fits$BIC)
# BIC MODEL PROBABILITY WEGIHTS #
params <- c("normed_signed_e_v", "normed_unsigned_e_v", "subject_liedTRUE", "subject_lostTRUE")
weighted_params <- lmer_fits[, ..params] * lmer_fits$BIC_weights
weighted_factors_avg <- apply(weighted_params, MARGIN = 2, function(x) { sum(x, na.rm = TRUE)})
weighted_factors_sd <- apply(weighted_params, MARGIN = 2, function(x) { sd(x, na.rm = TRUE)})
# 95%-CIs
ci_ub <- weighted_factors_avg + 1.96 * (weighted_factors_sd / sqrt(n_models_w_parameter))
ci_lb <- weighted_factors_avg - 1.96 * (weighted_factors_sd / sqrt(n_models_w_parameter))
human_CIs <- data.frame(excluded=uuid, mean=weighted_factors_avg, upper=ci_ub, lower=ci_lb)
human_CIs$param <- c("signed expectation violation", "unsigned expectation violation", "lying oneself", "losing")
human_CIs$param <- factor(human_CIs$param)
cv_bay_model_ci <- rbindlist(list(human_CIs, cv_bay_model_ci))
print("========================================================================")
}
return(cv_bay_model_ci)
}
cv_bayesian_avg_perfect <- function(data, uuids, models, n_models_w_parameter){
cv_bay_model_ci_perfect <- data.table()
for (i in 1:length(uuids)){
print(i)
uuid <- uuids[i]
tmp <- data[data$PID != uuid, ]
perfect_detector_fit <- lm_fit(tmp, models)
perfect_detector_fit$BIC_weights <- BIC_weights(perfect_detector_fit$BIC)
params <- c("normed_signed_e_v", "normed_unsigned_e_v", "subject_liedTRUE", "subject_lostTRUE")
perfect_detector_weighted_factors <- perfect_detector_fit[, ..params] * perfect_detector_fit$BIC_weights
perfect_detector_weighted_factors_avg <- apply(perfect_detector_weighted_factors, MARGIN = 2, function(x) {sum(x, na.rm=T)})
perfect_detector_weighted_factors_sd <- apply(perfect_detector_weighted_factors, MARGIN = 2, function(x) {sd(x, na.rm=T)})
# 95%-CI perfect detector
perfect_ci_ub <- perfect_detector_weighted_factors_avg + 1.96 * (perfect_detector_weighted_factors_sd / sqrt(n_models_w_parameter))
perfect_ci_lb <- perfect_detector_weighted_factors_avg - 1.96 * (perfect_detector_weighted_factors_sd / sqrt(n_models_w_parameter))
perfect_CIs <- data.frame(excluded=uuid, mean=perfect_detector_weighted_factors_avg, upper=perfect_ci_ub, lower=perfect_ci_lb)
perfect_CIs$param <- c("signed expectation violation", "unsigned expectation violation", "lying oneself", "losing")
perfect_CIs$param <- factor(perfect_CIs$param)
cv_bay_model_ci_perfect <- rbindlist(list(perfect_CIs, cv_bay_model_ci_perfect))
print("========================================================================")
}
return(cv_bay_model_ci_perfect)
}
compute_sdt_metrics <- function(data, uuids){
df_sdt <- data.table()
for (uuid in uuids){
tmp <- data[uuid]
n_hit <- sum((tmp$suspicion_rating > .5) & (tmp$pp_lied == TRUE))
n_targets <- sum(tmp$pp_lied == TRUE)
hit_rate <- n_hit / n_targets
n_fa <- sum((tmp$suspicion_rating > .5) & (tmp$pp_lied == FALSE))
n_nolie <- sum(tmp$pp_lied == FALSE)
fa_rate <- n_fa / n_nolie
# dprime
dprime <- qnorm(hit_rate) - qnorm(fa_rate)
# beta
zhr <- qnorm(hit_rate)
zfar <- qnorm(fa_rate)
beta <- exp(-zhr * zhr / 2 + zfar * zfar / 2)
n_lied_self <- sum(tmp$subject_lied)
# overall accuracy
tmp$y_pred <- ifelse(tmp$suspicion_rating > .5, TRUE, FALSE)
acc <- sum(tmp$y_pred == tmp$pp_lied)/nrow(tmp)
sdt <- data.frame(uuid=uuid, dprime=dprime, beta=beta, hitrate=hit_rate, farate=fa_rate, acc=acc,
n_lied_self=n_lied_self)
df_sdt <- rbindlist(list(sdt, df_sdt))
}
setkey(df_sdt, 'uuid')
return(df_sdt)
}
perfect_model <- function(x, experiment_n){
if (experiment_n == '3') {
return(0.305 * x$normed_signed_e_v + 0.336 * x$normed_unsigned_e_v + 0.025 * x$subject_lost)
}
if (experiment_n == '2') {
return(0.329 * x$normed_signed_e_v + 0.351 * x$normed_unsigned_e_v)
}
else {
return(0.295 * x$normed_signed_e_v + 0.313 * x$normed_unsigned_e_v)
}
}
compute_perfect_sdt_metrics <- function(data, uuids, experiment_n){
df_sdt <- data.table()
for (uuid in uuids){
tmp <- data[uuid]
tmp$y_pred <- ifelse(perfect_model(tmp, experiment_n) >= .5, TRUE, FALSE)
n_hit <- sum((tmp$y_pred == TRUE) & (tmp$pp_lied == TRUE))
n_targets <- sum(tmp$pp_lied == TRUE)
hit_rate <- n_hit / n_targets
n_fa <- sum((tmp$y_pred == TRUE) & (tmp$pp_lied == FALSE))
n_nolie <- sum(tmp$pp_lied == FALSE)
fa_rate <- n_fa / n_nolie
# dprime
dprime <- qnorm(hit_rate) - qnorm(fa_rate)
# beta
zhr <- qnorm(hit_rate)
zfar <- qnorm(fa_rate)
beta <- exp(-zhr * zhr / 2 + zfar * zfar / 2)
# overall accuracy
acc <- sum(tmp$y_pred == tmp$pp_lied)/nrow(tmp)
sdt <- data.frame(uuid=uuid, dprime_art=dprime, beta_art=beta, hitrate_art=hit_rate, farate_art=fa_rate, acc_art=acc)
df_sdt <- rbindlist(list(sdt, df_sdt))
}
setkey(df_sdt, 'uuid')
return(df_sdt)
}
plot_partial <- function(lm1, lm2, lm1_dep, lm2_dep) {
resid.1 <- resid(lm1)
resid.2 <- resid(lm2)
tmp <- data.frame(resid.1, resid.2)
plot <- ggplot(data=tmp, aes(x=resid.2, y=resid.1)) +
geom_point() +
geom_smooth(method=lm, color="darkgoldenrod") +
theme_bw() + theme(text = element_text(size=28), panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
xlab(lm1_dep) +
ylab(lm2_dep)
return(plot)
}
avg_corr_matrix <- function(data, uuids, param_combis){
master_corrs <- data.table()
for (uuid in uuids) {
data_single <- data[uuid]
data_single$subject_lied <- as.numeric(data_single$subject_lied)
data_single$subject_lost <- as.numeric(data_single$subject_lost)
corrs <- c()
for (combi in param_combis) {
subset <- data_single[, ..combi]
corr <- cor.test(subset[,1][[1]], subset[,2][[1]])$estimate
corrs <- c(corrs, corr)
}
corrs <- data.frame(t(corrs))
corrs$PID <- uuid
master_corrs <- rbindlist(list(corrs, master_corrs))
}
return(master_corrs)
}
avg_corr_matrix_p <- function(data, uuids, param_combis){
master_corrs <- data.table()
for (uuid in uuids) {
data_single <- data[uuid]
data_single$subject_lied <- as.numeric(data_single$subject_lied)
data_single$subject_lost <- as.numeric(data_single$subject_lost)
corrs <- c()
for (combi in param_combis) {
subset <- data_single[, ..combi]
corr <- cor.test(subset[,1][[1]], subset[,2][[1]])$p.value
corrs <- c(corrs, corr)
}
corrs <- data.frame(t(corrs))
corrs$PID <- uuid
master_corrs <- rbindlist(list(corrs, master_corrs))
}
return(master_corrs)
}