Skip to content

Commit

Permalink
add tests before breaking again
Browse files Browse the repository at this point in the history
  • Loading branch information
philchalmers committed Nov 7, 2024
1 parent ad65d5e commit 1286c60
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion tests/tests/test-18-LLTM.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that('LLTM', {
ifit <- itemfit(lltm)
expect_equal(ifit$S_X2[1:3], c(20.06072, 20.90161, 23.48163), tol=1e-2)
eap <- fscores(lltm)
expect_equal(eap[1:3], c(1.0141828, -0.2427042, -0.2427042))
expect_equal(eap[1:3], c(1.0141828, -0.2427042, -0.2427042), tol=1e-2)

# using unconditional modeling for first four items
itemdesign.sub <- itemdesign[5:nrow(itemdesign), , drop=FALSE]
Expand All @@ -34,6 +34,46 @@ test_that('LLTM', {
})

test_that('MLTM', {
set.seed(42)

as <- matrix(rep(1,60), ncol=2)
as[11:18,1] <- as[1:9,2] <- 0
d1 <- rep(c(3,1),each = 6) # first easy, then medium, last difficult for first trait
d2 <- rep(c(0,1,2),times = 4) # difficult to easy
d <- rnorm(18)
ds <- rbind(cbind(d1=NA, d2=d), cbind(d1, d2))
dat <- simdata(as, ds, 2500,
itemtype = c(rep('dich', 18), rep('partcomp', 12)))

# unconditional model
syntax <- "theta1 = 1-9, 19-30
theta2 = 10-30
COV = theta1*theta2"
itemtype <- c(rep('Rasch', 18), rep('PC1PL', 12))
mod <- mirt(dat, syntax, itemtype=itemtype, verbose=FALSE)
expect_equal(as.numeric(coef(mod, simplify=TRUE)$items[19:21, c('d1', 'd2')]),
c(2.86919500, 3.71533102, 3.23797689, 0.01323629, 0.83160425, 1.90030392), tol=1e-2)
expect_equal(logLik(mod), -43860.17, tol=1e-2)

# MLTM design only for PC1PL items
itemdesign <- data.frame(t1_difficulty= factor(d1, labels=c('medium', 'easy')),
t2_difficulty=factor(d2, labels=c('hard', 'medium', 'easy')))
rownames(itemdesign) <- colnames(dat)[19:30]
itemdesign

# fit MLTM design, leaving first 18 items as 'Rasch' type
mltm <- mirt(dat, syntax, itemtype=itemtype, itemdesign=itemdesign,
item.formula = list(theta1 ~ 0 + t1_difficulty,
theta2 ~ 0 + t2_difficulty), SE=TRUE, verbose=FALSE)
expect_equal(extract.mirt(mltm, 'condnum'), 36.33511, tol=1e-2)
expect_equal(anova(mltm, mod)$p[2], 0.592, tol=1e-2)
cfs <- coef(mltm, simplify=TRUE)$items
expect_equal(sort(unique(as.vector(cfs[,1:5]))),
c(-0.07838095, 0.00000000, 0.92424686, 1.03069049, 1.85666146, 3.18998660), tol=1e-2)
fs <- fscores(mltm)
expect_equal(as.vector(fs[1:3,]), c(-2.0019607,1.138449,0.149316,
-0.4814751,0.3697978,1.7928627), tol=1e-2)


})

0 comments on commit 1286c60

Please sign in to comment.