5.14 MCMC for DINA model in JAGS and R2jags R package
Zhan et al. (2019) introduced how to use JAGS for Bayesian estimation for CDMs using MCMC algorithm. To use JAGS, you need to download and install GAGS from here.
The following code was adapted from Zhan et al. (2019) and can be used for estimating parameters of the DINA model.
Code
#
<- sim10GDINA$simdat
Y <- sim10GDINA$simQ
Q <- GDINA::attributepattern(ncol(Q))
all.patterns <- function() {
jags.dina for (n in 1:N) {
for (i in 1:I) {
<- 1 * (sum(alpha[n, 1:K] * Q[i, 1:K]) >= sum(Q[i,
eta[n, i] 1:K]))
<- g[i] + (1 - s[i] - g[i]) * eta[n, i]
p[n, i] ~ dbern(p[n, i])
Y[n, i]
}for (k in 1:K) {
<- all.patterns[latent.group.index[n], k]
alpha[n, k]
}~ dcat(pi[1:C])
latent.group.index[n]
}1:C] ~ ddirch(delta[1:C])
pi[for (i in 1:I) {
~ dbeta(1, 1)
s[i] ~ dbeta(1, 1) %_% T(0, 1 - s[i])
g[i]
}
}library(R2jags)
<- nrow(Y)
N <- nrow(Q)
I <- ncol(Q)
K <- nrow(all.patterns)
C <- rep(1, C)
delta <- list("N", "I", "K", "Y", "Q", "C", "all.patterns", "delta")
jags.data <- c("s", "g", "latent.group.index", "pi")
jags.parameters <- NULL
jags.inits <- jags(data = jags.data, inits = jags.inits, parameters.to.save = jags.parameters,
jags.dina.mcmc model.file = jags.dina, n.chains = 2, n.iter = 5000, n.burnin = 2500,
n.thin = 1, DIC = TRUE)
Code
library(MCMCvis)
::MCMCsummary(jags.dina.mcmc, params = c("s", "g"), Rhat = TRUE,
MCMCvisround = 2)
## mean sd 2.5% 50% 97.5% Rhat n.eff
## s[1] 0.26 0.03 0.21 0.26 0.31 1 580
## s[2] 0.28 0.03 0.22 0.28 0.33 1 685
## s[3] 0.11 0.02 0.07 0.11 0.16 1 614
## s[4] 0.20 0.03 0.14 0.20 0.26 1 569
## s[5] 0.29 0.04 0.22 0.29 0.36 1 754
## s[6] 0.07 0.02 0.04 0.07 0.11 1 835
## s[7] 0.32 0.03 0.27 0.32 0.38 1 957
## s[8] 0.30 0.04 0.23 0.31 0.38 1 423
## s[9] 0.21 0.03 0.15 0.21 0.26 1 1257
## s[10] 0.16 0.03 0.10 0.16 0.23 1 1011
## g[1] 0.07 0.05 0.00 0.07 0.18 1 160
## g[2] 0.09 0.03 0.03 0.09 0.15 1 308
## g[3] 0.07 0.03 0.02 0.07 0.13 1 335
## g[4] 0.22 0.02 0.18 0.22 0.26 1 836
## g[5] 0.08 0.01 0.06 0.08 0.11 1 1342
## g[6] 0.59 0.03 0.53 0.59 0.63 1 959
## g[7] 0.26 0.02 0.22 0.26 0.30 1 1401
## g[8] 0.15 0.02 0.11 0.15 0.19 1 964
## g[9] 0.27 0.02 0.23 0.27 0.31 1 1142
## g[10] 0.33 0.02 0.30 0.34 0.37 1 1098
Code
::MCMCtrace(jags.dina.mcmc, params = c("g[1]", "s[1]"), ISB = FALSE,
MCMCvisexact = TRUE, ind = TRUE, pdf = FALSE, Rhat = TRUE, n.eff = TRUE, iter = 5000)
References
Zhan, P., Jiao, H., Man, K., & Wang, L. (2019). Using JAGS for bayesian cognitive diagnosis modeling: A tutorial. Journal of Educational and Behavioral Statistics, 44(4), 473–503. https://doi.org/10.3102/1076998619826040