This vignette includes some templates for hypothesis objects that can be altered for your specific use case.
The templates currently include:
The templates are described in some more detail in the respective sections. We are open for your suggestions to include further templates.
We test the null hypothesis that the parameters adhere to the Rasch model against the alternative hypothesis that the parameters are better described by a 2PL model.
<- list(res = function(altpars, nullpars = NULL) {
h_1PLvs2PL
<- length(altpars[[1]])
n.items
<- list(n.items = n.items, itemtype = "2PL",
re Amat = c(1, 0, -1, 0, rep(0, (n.items - 1) *
2)) |>
function(x) rep(x, n.items - 2))() |>
(c(1, 0, -1, 0) |>
matrix(ncol = n.items * 2, byrow = TRUE),
cvec = 0, model = mirt::mirt.model(paste("F = 1-",
"
n.items, CONSTRAIN = (1-",
", a1)")))
n.items, return(re)
unres = function(altpars) {
},
<- list(parsets = altpars, model = 1, itemtype = "2PL",
re longpars = pars.long(pars = altpars, itemtype = "2PL"))
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
<- function(pars, funs) {
maxlpreload # returns the density for each response
# pattern under the model parameters pars
<- as.matrix(expand.grid(lapply(seq_len(length(pars$a)),
patterns function(x) c(0, 1))))
<- c()
pre for (i in seq_len(nrow(patterns))) {
<- funs$g(patterns[i, ], pars)
pre[i]
}
return(pre)
}
<- function(x, pars, pre, funs) {
maxl # calculates the likelihood of parameters
# x given model 'pars'
<- as.matrix(expand.grid(lapply(seq_len(length(pars$a)),
patterns function(x) c(0, 1))))
<- list(a = rep(x[1], length(pars$a)), d = x[2:length(x)])
x
<- c()
res for (i in seq_len(nrow(patterns))) {
<- pre[i]
px <- funs$g(patterns[i, ], x)
qx <- {
res[i] * log(qx)
px
}
}<- -sum(res)
re
}<- hyp$resmod
resmod <- hyp$unresmod
unresmod
<- unresmod$parsets
pars
<- load.functions(unresmod$itemtype)
funs
<- c(mean(pars$a), as.numeric(pars$d))
startval
<- maxlpreload(pars, funs)
maxlpre <- stats::optim(startval, function(x) {
optpar maxl(x, pars, maxlpre, funs)
method = "BFGS")
}, <- pars
re $a <- rep(optpar$par[1], length(pars$a))
re$d <- optpar$par[2:length(optpar$par)]
re
return(re)
})
We test for differential item functioning in the 2PL model. Our alternative hypothesis is that certain item parameters differ in two known participant groups. Which item parameters differ can be determined in the altpars object, see the “demo” vignette for an example.
<- list(res = function(altpars, nullpars = NULL) {
h_DIF2PL
<- length(altpars[[1]][[1]])
n.items
<- altpars[[1]]
reA <- altpars[[2]]
reB
<- which(reA$a != reB$a)
hyp_a <- which(reA$d != reB$d)
hyp_d
<- matrix(0, nrow = length(c(hyp_a, hyp_d)),
Amat ncol = n.items * 2)
<- 1
i for (j in hyp_a) {
* 2 - 1] <- 1
Amat[i, j <- i + 1
i
}for (j in hyp_d) {
* 2] <- 1
Amat[i, j <- i + 1
i
}<- cbind(Amat, -Amat)
Amat
<- (colSums(Amat) == 0) & (1:(n.items *
delcols 2 * 2)) > 2 * n.items
<- colSums(Amat[, 1:(2 * n.items)]) ==
relpars 1
<- Amat[, !delcols]
Amat
<- list(n.items = n.items, itemtype = "2PL",
re Amat = Amat, cvec = 0, model = mirt::mirt.model(paste("F = 1-",
"
n.items, CONSTRAINB = (1-",
", d), (1-", n.items, ", a1)")),
n.items, multigroup = TRUE, delcols = delcols, relpars = relpars)
return(re)
unres = function(altpars) {
},
<- length(altpars[[1]][[1]])
n.items
<- altpars[[1]]
reA <- altpars[[2]]
reB
$itemtype <- reB$itemtype <- "2PL"
reA
$longpars <- pars.long(pars = reA, itemtype = "2PL")
reA$longpars <- pars.long(pars = reB, itemtype = "2PL")
reB
<- which(reA$a == reB$a)
constrain_a <- which(reA$d == reB$d)
constrain_d
<- which(reA$a != reB$a)
hyp_a <- which(reA$d != reB$d)
hyp_d
<- matrix(0, nrow = length(c(hyp_a, hyp_d)),
Amat ncol = n.items * 2)
<- 1
i for (j in hyp_a) {
* 2 - 1] <- 1
Amat[i, j <- i + 1
i
}for (j in hyp_d) {
* 2] <- 1
Amat[i, j <- i + 1
i
}<- cbind(Amat, -Amat)
Amat
<- (colSums(Amat) == 0) & (1:(n.items *
delcols 2 * 2)) > 2 * n.items
<- c(reA$longpars, reB$longpars)[!delcols]
longpars
<- list(parsets = list(reA, reB), model = mirt::mirt.model(paste("F = 1-",
re "
n.items, CONSTRAINB = (",
paste(constrain_d, collapse = ","), ", d), (",
paste(constrain_a, collapse = ","), ", a1)")),
longpars = longpars, multigroup = TRUE, itemtype = "2PL",
delcols = delcols)
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
# L Optimizer
<- function(x, pars1, pars2, i) {
maxl
<- function(th) {
px1 $f(th, pars1$a[i], pars1$d[i], 1)
funs
}<- function(th) {
px2 $f(th, pars2$a[i], pars2$d[i], 1)
funs
}<- function(th) {
qx $f(th, x[1], x[2], 1)
funs
}<- function(th) {
kl px1(th) * log(qx(th)) + (1 - px1(th)) *
log(1 - qx(th)) + px2(th) * log(qx(th)) +
1 - px2(th)) * log((1 - qx(th)))
(
}<- -spatstat.random::gauss.hermite(kl, order = 20)
re
}
<- hyp$resmod
resmod <- hyp$unresmod
unresmod
<- unresmod$parsets
pars
<- pars[[1]]
pars1 <- pars[[2]]
pars2
<- load.functions(pars1$itemtype)
funs <- pars1
re
for (i in seq_len(length(pars1$a))) {
<- c(re$a[i], re$d[i])
startval <- stats::optim(startval, function(x) {
optpar maxl(x, pars1, pars2, i)
method = "BFGS")
}, $a[i] <- optpar$par[1]
re$d[i] <- optpar$par[2]
re
}return(re)
})
We test the null hypothesis that the parameters adhere to the partial credit model against the alternative hypothesis that the parameters are better described by the generalized partial credit model.
<- list(res = function(altpars, nullpars = NULL) {
h_PCMvsGPCM
<- length(altpars[[1]])
n.items <- ncol(altpars$d)
nkat
<- list(n.items = n.items, itemtype = "gpcm",
re Amat = c(1, rep(0, nkat - 1), -1, rep(0, nkat -
1), rep(0, (n.items - 1) * nkat)) |>
function(x) rep(x, n.items - 2))() |>
(c(1, rep(0, nkat - 1), -1, rep(0, nkat -
1)) |>
matrix(ncol = n.items * nkat, byrow = TRUE),
cvec = 0, model = mirt::mirt.model(paste("F = 1-",
"
n.items, CONSTRAIN = (1-",
", a1)")))
n.items, return(re)
unres = function(altpars) {
},
<- list(parsets = altpars, model = 1, itemtype = "gpcm",
re longpars = pars.long(pars = altpars, itemtype = "gpcm"))
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
<- function(pars) {
maxlpreload # returns the density for each response
# pattern under the model parameters pars
<- length(pars$a)
n.items <- max(ncol(pars$d), 2)
n.kat <- as.matrix(expand.grid(lapply(1:n.items,
patterns function(x) 0:(n.kat - 1))))
<- c()
pre for (i in seq_len(nrow(patterns))) {
<- funs$g(patterns[i, ], pars)
pre[i]
}
return(pre)
}
<- function(x, pars, pre) {
maxl # calculates the likelihood of parameters
# x given model 'pars'
<- length(pars$a)
n.items <- max(ncol(pars$d), 2)
n.kat <- as.matrix(expand.grid(lapply(1:n.items,
patterns function(x) 0:(n.kat - 1))))
<- list(a = rep(x[1], n.items), d = matrix(c(rep(0,
x 2:length(x)]), ncol = ncol(pars$d)))
n.items), x[
<- c()
res for (i in seq_len(nrow(patterns))) {
<- pre[i]
px <- funs$g(patterns[i, ], x)
qx <- {
res[i] * log(qx)
px
}
}<- -sum(res)
re
}
<- hyp$resmod
resmod <- hyp$unresmod
unresmod
<- unresmod$parsets
pars <- load.functions(unresmod$itemtype)
funs
<- max(ncol(pars$d), 2)
n.kat <- length(pars$a)
n.items <- c(mean(pars$a), as.numeric(pars$d[,
startval 2:n.kat]))
<- maxlpreload(pars)
maxlpre
<- stats::optim(startval, function(x) {
optpar maxl(x, pars, maxlpre)
method = "BFGS")
}, <- pars
re $a <- rep(optpar$par[1], n.items)
re$d <- matrix(c(rep(0, n.items), optpar$par[2:length(optpar$par)]),
rencol = ncol(pars$d))
return(re)
})
The null hypothesis states that the first item has difficulty 0. The underlying model is the 2PL model.
<- list(res = function(altpars, nullpars = NULL) {
h_2PL_basic
<- length(altpars[[1]])
n.items
<- list(n.items = n.items, itemtype = "2PL",
re Amat = c(0, 1, rep(0, (n.items - 1) * 2)) |>
function(x) matrix(x, ncol = n.items *
(2, byrow = TRUE))(), cvec = 0, model = mirt::mirt.model(paste("F = 1-",
"
n.items, FIXED = (1, d)
START = (1,d,0)")))
return(re)
unres = function(altpars) {
},
<- list(parsets = altpars, model = 1, itemtype = "2PL",
re longpars = pars.long(pars = altpars, itemtype = "2PL"))
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
<- function(pars) {
maxlpreload # returns the density for each response
# pattern under the model parameters pars
<- as.matrix(expand.grid(lapply(seq_len(length(pars$a)),
patterns function(x) c(0, 1))))
<- c()
pre for (i in seq_len(nrow(patterns))) {
<- funs$g(patterns[i, ], pars)
pre[i]
}
return(pre)
}
<- function(x, pars, pre) {
maxl # calculates the likelihood of parameters
# x given model 'pars'
<- as.matrix(expand.grid(lapply(seq_len(length(pars$a)),
patterns function(x) c(0, 1))))
<- list(a = c(x, pars$a[2:length(pars$a)]),
x d = c(0, pars$d[2:length(pars$d)]))
<- c()
res for (i in seq_len(nrow(patterns))) {
<- pre[i]
px <- funs$g(patterns[i, ], x)
qx <- {
res[i] * log(qx)
px
}
}<- -sum(res)
re
}<- hyp$resmod
resmod <- hyp$unresmod
unresmod
<- unresmod$parsets
pars <- load.functions(unresmod$itemtype)
funs
<- pars$a[1]
startval
<- maxlpreload(pars)
maxlpre
<- stats::optim(startval, function(x) {
optpar maxl(x, pars, maxlpre)
method = "BFGS")
}, <- pars
re $a <- c(optpar$par[1], pars$a[2:length(pars$a)])
re$d <- c(0, pars$d[2:length(pars$d)])
re
return(re)
})
We test the null hypothesis that the first item parameters are (1,0,.2) for the a,d, and g parameters respectively. The underlying model is the 3PL model.
<- list(res = function(altpars, nullpars = NULL) {
h_3PL_basic <- length(altpars[[2]])
n.items
<- list(n.items = n.items, itemtype = "3PL",
re Amat = c(1, 0, 0, rep(0, (n.items - 1) * 3),
0, 1, 0, rep(0, (n.items - 1) * 3), 0,
0, 1, rep(0, (n.items - 1) * 3)) |>
matrix(ncol = n.items * 3, byrow = TRUE),
cvec = c(1, 0, 0.2), model = mirt::mirt.model(paste("F = 1-",
"
n.items, FIXED = (1, d), (1,a1), (1,g)
START = (1,d,0),(1,a1,1),(1,g,.2)")))
return(re)
unres = function(altpars) {
}, <- length(altpars[[2]])
n.items
<- list(parsets = altpars, model = 1, itemtype = "3PL",
re longpars = pars.long(pars = altpars, itemtype = "3PL"))
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
<- function(pars) {
maxlpreload # returns the density for each response
# pattern under the model parameters pars
<- as.matrix(expand.grid(lapply(seq_len(length(pars$a)),
patterns function(x) c(0, 1))))
<- c()
pre for (i in seq_len(nrow(patterns))) {
<- funs$g(patterns[i, ], pars)
pre[i]
}
return(pre)
}
<- function(x, pars, pre) {
maxl # calculates the likelihood of parameters
# x given model 'pars'
<- as.matrix(expand.grid(lapply(seq_len(length(pars$a)),
patterns function(x) c(0, 1))))
<- list(a = c(x, pars$a[2:length(pars$a)]),
x d = c(0, pars$d[2:length(pars$d)]))
<- c()
res for (i in seq_len(nrow(patterns))) {
<- pre[i]
px <- funs$g(patterns[i, ], x)
qx <- {
res[i] * log(qx)
px
}
}<- -sum(res)
re
}<- hyp$resmod
resmod <- hyp$unresmod
unresmod
<- unresmod$parsets
pars <- load.functions(unresmod$itemtype)
funs
<- pars$a[1]
startval
<- maxlpreload(pars)
maxlpre
<- stats::optim(startval, function(x) {
optpar maxl(x, pars, maxlpre)
method = "BFGS")
}, <- pars
re $a <- c(optpar$par[1], pars$a[2:length(pars$a)])
re$d <- c(0, pars$d[2:length(pars$d)])
re
return(re)
})
We test the null hypothesis that the difficulty of the first two item parameters is equal. The underlying model is the two-dimensional 2PL model. Only the sampling-based approach is available for now.
<- list(res = function(altpars, nullpars = NULL) {
h_multi_basic <- length(altpars[[2]])
n.items
<- list(n.items = n.items, itemtype = "2PL",
re Amat = c(0, 0, 1, 0, 0, -1, rep(0, (n.items -
3) * 3 + 2)) |>
matrix(ncol = n.items * 3 - 1, byrow = TRUE),
cvec = 0, model = mirt::mirt.model(paste("F1 = 1-",
"
n.items, F2 = 1-",
- 1, "
n.items CONSTRAIN = (1-2, d")))
return(re)
unres = function(altpars) {
}, <- length(altpars[[2]])
n.items
<- list(parsets = altpars, model = mirt::mirt.model(paste("F1 = 1-",
re "
n.items, F2 = 1-",
- 1, "")), itemtype = "2PL", longpars = pars.long(pars = altpars,
n.items itemtype = "2PL"))
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
# not written yet, only sampling-based
# available for now
})
We test the null hypothesis that the difficulty of the first item is equal to 2. The underlying model is the two-dimensional 2PL model. Only the sampling-based approach is available for now.
<- list(res = function(altpars, nullpars = NULL) {
h_multi_basic2 <- length(altpars[[2]])
n.items
<- list(n.items = n.items, itemtype = "2PL",
re Amat = c(0, 0, 1, rep(0, (n.items - 2) * 3 +
2)) |>
matrix(ncol = n.items * 3 - 1, byrow = TRUE),
cvec = 2, model = mirt::mirt.model(paste("F1 = 1-",
"
n.items, F2 = 1-",
- 1, "
n.items FIXED = (1, d)
START = (1,d,2)")))
return(re)
unres = function(altpars) {
}, <- length(altpars[[2]])
n.items
<- list(parsets = altpars, model = mirt::mirt.model(paste("F1 = 1-",
re "
n.items, F2 = 1-",
- 1, "")), itemtype = "2PL", longpars = pars.long(pars = altpars,
n.items itemtype = "2PL"))
return(re)
maximizeL = function(hyp) {
}, # Hypothesis-specific algorithm to find the
# maximum likelihood restricted parameter set
# not written yet, only sampling-based
# available for now
})