-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBayesian-IRT.R
592 lines (492 loc) · 16.2 KB
/
Bayesian-IRT.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# This script requires brms version 2.11.5 or higher to fully run.
# The current release version of brms can be installed via
# install.packages("brms)
# The current developmental version of brms can be installed via
# remotes::install_github("paul-buerkner/brms")
# load required packages
library(tidyverse)
library(brms)
# for comparison with brms
library(lme4)
library(TAM)
# set ggplot theme
theme_set(bayesplot::theme_default())
# set rstan options
rstan::rstan_options(auto_write = TRUE)
options(mc.cores = 2)
# create a "models" folder in the current working directory
# to store fitted model objects for easier re-usage
if (!dir.exists("models")) {
dir.create("models")
}
# Although I set a seed for all models, the results are only exactly
# reproducible on the same operating system with the same C++ compiler
# and version. Thus, when you run the code below, it will not produce
# exactly the same results as shown in the paper.
# ----------- Code for Section 5.1 ------------
# Analysis of the VerbAgg data set using dichotomous IRT models
data("VerbAgg", package = "lme4")
# get an overview of the data
head(VerbAgg, 10)
# ---------- 1PL models ----------------------
# specify a 1PL model in brms
formula_va_1pl <- bf(r2 ~ 1 + (1 | item) + (1 | id))
# specify some weakly informative priors
prior_va_1pl <-
prior("normal(0, 3)", class = "sd", group = "id") +
prior("normal(0, 3)", class = "sd", group = "item")
# fit the 1PL model
fit_va_1pl <- brm(
formula = formula_va_1pl,
data = VerbAgg,
family = brmsfamily("bernoulli", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_1pl"
)
# obtain basic summaries
summary(fit_va_1pl)
plot(fit_va_1pl, ask = FALSE)
# extract person parameters
ranef_va_1pl <- ranef(fit_va_1pl)
(person_pars_va_1pl <- ranef_va_1pl$id)
# extract item parameters
(item_pars_va_1pl <- coef(fit_va_1pl)$item)
# plot item parameters
item_pars_va_1pl[, , "Intercept"] %>%
as_tibble() %>%
rownames_to_column() %>%
rename(item = "rowname") %>%
mutate(item = as.numeric(item)) %>%
ggplot(aes(item, Estimate, ymin = Q2.5, ymax = Q97.5)) +
geom_pointrange() +
coord_flip() +
labs(x = "Item Number")
# plot person parameters
person_pars_va_1pl[, , "Intercept"] %>%
as_tibble() %>%
rownames_to_column() %>%
arrange(Estimate) %>%
mutate(id = seq_len(n())) %>%
ggplot(aes(id, Estimate, ymin = Q2.5, ymax = Q97.5)) +
geom_pointrange(alpha = 0.7) +
coord_flip() +
labs(x = "Person Number (Sorted)")
# specify a 1PL model with lme4 for comparison
lme4_va_1pl <- glmer(
r2 ~ 1 + (1 | item) + (1 | id),
data = VerbAgg,
family = binomial()
)
summary(lme4_va_1pl)
# person and item parameters are similar to those obtained by brms
coef(lme4_va_1pl)$item
ranef(lme4_va_1pl)$id
# specify a 1PL model with TAM for comparison
# bring the data in wide structure first
VerbAgg_wide <- VerbAgg %>%
select(item, id, r2) %>%
mutate(r2 = ifelse(r2 == "Y", 1, 0)) %>%
spread(key = "item", value = "r2") %>%
select(-id)
# fit the model with TAM
tam_va_1pl <- tam(VerbAgg_wide, irtmodel = "1PL", verbose = FALSE)
# person and item parameters are similar to those obtained by brms
summary(tam_va_1pl)
IRT.factor.scores(tam_va_1pl)
# ---------- 2PL models ----------------------
## specify a 2PL model
formula_va_2pl <- bf(
r2 ~ exp(logalpha) * eta,
eta ~ 1 + (1 |i| item) + (1 | id),
logalpha ~ 1 + (1 |i| item),
nl = TRUE
)
# specify some weakly informative priors
prior_va_2pl <-
prior("normal(0, 5)", class = "b", nlpar = "eta") +
prior("normal(0, 1)", class = "b", nlpar = "logalpha") +
prior("constant(1)", class = "sd", group = "id", nlpar = "eta") +
prior("normal(0, 3)", class = "sd", group = "item", nlpar = "eta") +
prior("normal(0, 1)", class = "sd", group = "item", nlpar = "logalpha")
# fit the 2PL model
# this models throws some convergence warnings which are false
# positives and can be safely ignored
fit_va_2pl <- brm(
formula = formula_va_2pl,
data = VerbAgg,
family = brmsfamily("bernoulli", "logit"),
prior = prior_va_2pl,
seed = 1234,
file = "models/fit_va_2pl"
)
# obtain some basic summaries
summary(fit_va_2pl)
plot(fit_va_2pl, ask = FALSE)
# extract item parameters
(item_pars_va_2pl <- coef(fit_va_2pl)$item)
# plot item parameters
# difficulties
eta <- item_pars_va_1pl[, , "eta_Intercept"] %>%
as_tibble() %>%
rownames_to_column()
# discriminations
alpha <- item_pars_va_1pl[, , "logalpha_Intercept"] %>%
exp() %>%
as_tibble() %>%
rownames_to_column()
# plot difficulties and discrimination next to each other
bind_rows(eta, alpha, .id = "nlpar") %>%
rename(item = "rowname") %>%
mutate(item = as.numeric(item)) %>%
mutate(nlpar = factor(nlpar, labels = c("Easiness", "Discrimination"))) %>%
ggplot(aes(item, Estimate, ymin = Q2.5, ymax = Q97.5)) +
facet_wrap("nlpar", scales = "free_x") +
geom_pointrange() +
coord_flip() +
labs(x = "Item Number")
# extract person parameters
ranef_va_2pl <- ranef(fit_va_2pl)
(person_pars_va_2pl <- ranef_va_2pl$id)
# plot person parameters
person_pars_va_2pl[, , "eta_Intercept"] %>%
as_tibble() %>%
rownames_to_column() %>%
select(-Est.Error) %>%
arrange(Estimate) %>%
mutate(id = seq_len(n())) %>%
ggplot(aes(id, Estimate, ymin = Q2.5, ymax = Q97.5)) +
geom_pointrange(alpha = 0.7) +
coord_flip() +
labs(x = "Person Number (Sorted)")
# perform model comparison via approximate LOO-CV
loo_va_1pl <- loo(fit_va_1pl)
loo_va_2pl <- loo(fit_va_2pl)
loo_va_compare <- loo_compare(loo_va_1pl, loo_va_2pl)
print(loo_va_compare, simplify = FALSE)
# ---------- 1PL models with covariates ----------------------
# specify a model including item covariates
formula_va_1pl_cov1 <- bf(
r2 ~ btype + situ + mode + (1 | item) + (0 + mode | id)
)
fit_va_1pl_cov1 <- brm(
formula = formula_va_1pl_cov1,
data = VerbAgg,
family = brmsfamily("bernoulli", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_1pl_cov1"
)
summary(fit_va_1pl_cov1)
conditional_effects(fit_va_1pl_cov1, "mode")
# compare standard deviations
hyp <- "modedo - modewant > 0"
hypothesis(fit_va_1pl_cov1, hyp, class = "sd", group = "id")
# fit a more complex covariate model
formula_va_1pl_cov2 <- bf(
r2 ~ Anger + Gender + btype + situ + mode + mode:Gender +
(0 + Gender | item) + (0 + mode | id)
)
fit_va_1pl_cov2 <- brm(
formula = formula_va_1pl_cov2,
data = VerbAgg,
family = brmsfamily("bernoulli", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_1pl_cov2"
)
summary(fit_va_1pl_cov2)
plot(conditional_effects(fit_va_1pl_cov2, c("Anger", "mode:Gender")), ask = FALSE)
# perform explicit DIF analysis
# compute the DIF covariate
VerbAgg$dif <- as.numeric(with(
VerbAgg, Gender == "F" & mode == "do" & btype %in% c("curse", "scold")
))
# fit and summarize the DIF model
formula_va_1pl_dif1 <- bf(
r2 ~ Gender + dif + (1 | item) + (1 | id)
)
fit_va_1pl_dif1 <- brm(
formula = formula_va_1pl_dif1,
data = VerbAgg,
family = brmsfamily("bernoulli", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_1pl_dif1"
)
summary(fit_va_1pl_dif1)
# compare convergence of lme4 and brms for a complex covariate model
# does not converge well
glmer_va_1pl_cov_full <- lme4::glmer(
r2 ~ 1 + Anger + Gender + btype + situ + mode +
(1 + Anger + Gender | item) + (1 + btype + situ + mode | id),
data = VerbAgg, family = binomial("logit")
)
summary(glmer_va_1pl_cov_full)
# converges nicely and shows sensible results
fit_va_1pl_cov_full <- brm(
r2 ~ 1 + Anger + Gender + btype + situ + mode +
(1 + Anger + Gender | item) + (1 + btype + situ + mode | id),
data = VerbAgg,
family = brmsfamily("bernoulli", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_1pl_cov_full"
)
summary(fit_va_1pl_cov_full)
# ---------- 3PL models ----------------------
# 3PL model with known guessing parameter
formula_va_3pl <- bf(
r2 ~ 0.25 + 0.75 * inv_logit(exp(logalpha) * eta),
eta ~ 1 + (1 |i| item) + (1 | id),
logalpha ~ 1 + (1 |i| item),
nl = TRUE
)
family_va_3pl <- brmsfamily("bernoulli", link = "identity")
# 3PL model with unknown guessing parameters
formula_va_3pl <- bf(
r2 ~ gamma + (1 - gamma) * inv_logit(exp(logalpha) * eta),
eta ~ 1 + (1 |i| item) + (1 | id),
logalpha ~ 1 + (1 |i| item),
logitgamma ~ 1 + (1 |i| item),
nlf(gamma ~ inv_logit(logitgamma)),
nl = TRUE
)
# ----------- Code for Section 5.2 ------------
# fit ordinal models to the VerbAgg data
# specify a basic GRM
# this models throws some convergence warnings which are false
# positives and can be safely ignored
formula_va_ord_1pl <- bf(resp ~ 1 + (1 | item) + (1 | id))
fit_va_ord_1pl <- brm(
formula = formula_va_ord_1pl,
data = VerbAgg,
family = brmsfamily("cumulative", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_ord_1pl"
)
summary(fit_va_ord_1pl)
plot(fit_va_ord_1pl, ask = FALSE)
# extract item and person parameters
(ranef_va_ord_1pl <- ranef(fit_va_ord_1pl))
# plot person parameters
ranef_va_ord_1pl$id[, , "Intercept"] %>%
as_tibble() %>%
rownames_to_column() %>%
arrange(Estimate) %>%
mutate(id = seq_len(n())) %>%
ggplot(aes(id, Estimate, ymin = Q2.5, ymax = Q97.5)) +
geom_pointrange(alpha = 0.7) +
coord_flip() +
labs(x = "Person Number (Sorted)")
# -------------- ordinal 1PL models with varying thresholds ----------
# specify a GRM with varying thresholds across items
formula_va_ord_thres_1pl <- bf(resp | thres(gr = item) ~ 1 + (1 | id))
prior_va_ord_thres_1pl <-
prior("normal(0, 3)", class = "Intercept") +
prior("normal(0, 3)", class = "sd", group = "id")
# this models throws some convergence warnings which are false
# positives and can be safely ignored
fit_va_ord_thres_1pl <- brm(
formula = formula_va_ord_thres_1pl,
data = VerbAgg,
family = brmsfamily("cumulative", "logit"),
prior = prior_va_ord_thres_1pl,
inits = 0, chains = 2,
seed = 1234,
file = "models/fit_va_ord_thres_1pl"
)
summary(fit_va_ord_thres_1pl)
# perform model comparison
loo(fit_va_ord_1pl, fit_va_ord_thres_1pl)
# -------------- ordinal 2PL models ---------------
# specify a GRM with varying discriminations
formula_va_ord_2pl <- bf(
resp ~ 1 + (1 |i| item) + (1 | id),
disc ~ 1 + (1 |i| item)
)
# some weakly informative priors
prior_va_ord_2pl <-
prior("constant(1)", class = "sd", group = "id") +
prior("normal(0, 3)", class = "sd", group = "item") +
prior("normal(0, 1)", class = "sd", group = "item", dpar = "disc")
# fit the model
# this models throws some convergence warnings which are false
# positives and can be safely ignored
fit_va_ord_2pl <- brm(
formula = formula_va_ord_2pl,
data = VerbAgg,
family = brmsfamily("cumulative", "logit"),
prior = prior_va_ord_2pl,
seed = 1234,
file = "models/fit_va_ord_2pl"
)
summary(fit_va_ord_2pl)
# extract item and person parameters
(ranef_va_ord_2pl <- ranef(fit_va_ord_2pl))
# plot person parameters
# item easinesses (deviations from thresholds)
eta <- ranef_va_ord_2pl$item[, , "Intercept"] %>%
as_tibble() %>%
rownames_to_column()
# discriminations
alpha <- ranef_va_ord_2pl$item[, , "disc_Intercept"] %>%
exp() %>%
as_tibble() %>%
rownames_to_column()
# put easinesses and discriminations together
bind_rows(eta, alpha, .id = "nlpar") %>%
rename(item = "rowname") %>%
mutate(item = as.numeric(item)) %>%
mutate(nlpar = factor(nlpar, labels = c("Easiness", "Discrimination"))) %>%
ggplot(aes(item, Estimate, ymin = Q2.5, ymax = Q97.5)) +
facet_wrap("nlpar", scales = "free_x") +
geom_pointrange() +
coord_flip() +
labs(x = "Item Number")
# compute correlations between person parameters across models
cbind(
va_1pl = ranef_va_1pl$id[, "Estimate", "Intercept"],
va_2pl = ranef_va_2pl$id[, "Estimate", "eta_Intercept"],
va_ord_1pl = ranef_va_ord_1pl$id[, "Estimate", "Intercept"],
va_ord_2pl = ranef_va_ord_2pl$id[, "Estimate", "Intercept"]
) %>%
cor() %>%
round(3)
# ------- ordinal models with covariates -------------------
# fit a GRM with person and item covariates
# this models throws some convergence warnings which are false
# positives and can be safely ignored
formula_va_ord_cov1 <- bf(
resp ~ Anger + Gender + btype + situ + mode + mode:Gender +
(0 + Gender | item) + (0 + mode | id)
)
fit_va_ord_cov1 <- brm(
formula = formula_va_ord_cov1,
data = VerbAgg,
family = brmsfamily("cumulative", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_ord_cov1"
)
summary(fit_va_ord_cov1)
# plot effects of Anger
conditional_effects(fit_va_ord_cov1, effects = "Anger", categorical = TRUE)
# fit a PCM with covariates and a category specific effect of 'Anger'
formula_va_ord_cov2 <- bf(
resp ~ cs(Anger) + Gender + btype + situ + mode + mode:Gender +
(0 + Gender | item) + (0 + mode | id)
)
# fit the model
fit_va_ord_cov2 <- brm(
formula = formula_va_ord_cov2,
data = VerbAgg,
family = brmsfamily("acat", "logit"),
prior = prior_va_1pl,
seed = 1234,
file = "models/fit_va_ord_cov2"
)
# summarize the results
summary(fit_va_ord_cov2)
conditional_effects(fit_va_ord_cov2, effects = "Anger", categorical = TRUE)
# ----------- Code for Section 5.2 ------------
# Analysis of the rotation data set using response times IRT models
data("rotation", package = "diffIRT")
rotation <- rotation %>%
as_tibble() %>%
mutate(person = seq_len(n())) %>%
gather("key", "value", -person) %>%
extract("key", into = c("type", "item"), regex = "(.)\\[(.+)\\]") %>%
spread("type", "value") %>%
rename(time = T, resp = X) %>%
mutate(
rotate = factor(case_when(
item %in% c(2, 5, 8) ~ 50,
item %in% c(3, 6, 10) ~ 100,
item %in% c(1, 4, 7, 9) ~ 150
)),
item = as.numeric(item)
)
# get an overview of the data
head(rotation, 10)
# specify a distributional exgaussian model
bform_exg1 <- bf(
time ~ rotate + (1 |p| person) + (1 |i| item),
sigma ~ rotate + (1 |p| person) + (1 |i| item),
beta ~ rotate + (1 |p| person) + (1 |i| item)
)
# fit the model
fit_exg1 <- brm(
bform_exg1, data = rotation,
family = brmsfamily("exgaussian", link_sigma = "log", link_beta = "log"),
chains = 4, cores = 4, inits = 0,
control = list(adapt_delta = 0.99),
seed = 1234,
file = "models/fit_exg1"
)
# summarize the results
summary(fit_exg1)
pp_check(fit_exg1)
# visualize effects of 'rotate'
conditional_effects(fit_exg1, "rotate", dpar = "mu")
conditional_effects(fit_exg1, "rotate", dpar = "sigma")
conditional_effects(fit_exg1, "rotate", dpar = "beta")
# specify a 3-parameter drift diffusion model
bform_drift1 <- bf(
time | dec(resp) ~ rotate + (1 |p| person) + (1 |i| item),
bs ~ rotate + (1 |p| person) + (1 |i| item),
ndt ~ rotate + (1 |p| person) + (1 |i| item),
bias = 0.5
)
# specify initial values to help the model start sampling
# diffusion models require quite a bit of working memory
# and running multiple chains in parallel may exceed memory
# capacity of some standard laptops
# for this reason, we run only a single chain here but,
# in practice, running multiple chains is recommended for
# increased estimation accuracy and better convergence diagnostics
chains <- 1
inits_drift <- list(Intercept_ndt = -3)
inits_drift <- replicate(chains, inits_drift, simplify = FALSE)
# fit the model
fit_drift1 <- brm(
bform_drift1, data = rotation,
family = brmsfamily("wiener", "log", link_bs = "log", link_ndt = "log"),
chains = chains, cores = chains,
inits = inits_drift, init_r = 0.05,
control = list(adapt_delta = 0.99),
seed = 1234,
file = "models/fit_drift1"
)
# summarize the model
summary(fit_drift1)
# extract item specific parameters
coef(fit_drift1)$item
# plot the effect of 'rotate'
conditional_effects(fit_drift1, "rotate", dpar = "mu")
conditional_effects(fit_drift1, "rotate", dpar = "bs")
conditional_effects(fit_drift1, "rotate", dpar = "ndt")
# specify a drift diffusion model without
# 'rotate' affecting the boundary separation
bform_drift2 <- bf(
time | dec(resp) ~ rotate + (1 |p| person) + (1 |i| item),
bs ~ 1 + (1 |p| person),
ndt ~ rotate + (1 |p| person) + (1 |i| item),
bias = 0.5
)
# fit the model
fit_drift2 <- brm(
bform_drift2, rotation,
family = wiener("log", link_bs = "log", link_ndt = "log"),
chains = chains, cores = chains,
inits = inits_drift, init_r = 0.05,
control = list(adapt_delta = 0.99),
seed = 1234,
file = "models/fit_drift2"
)
# perform model comparison via approximate LOO-CV
loo_drift1 <- loo(fit_drift1)
loo_drift2 <- loo(fit_drift2)
loo_drift_compare <- loo_compare(loo_drift1, loo_drift2)
print(loo_drift_compare, simplify = FALSE)