philchalmers / mirt

Multidimensional item response theory
https://philchalmers.github.io/mirt/
201 stars 75 forks source link

Issues with multipleGroup function when SE=T, more than dentype="mixture-2" (e.g., "mixture-3") #247

Closed ysuh09 closed 9 months ago

ysuh09 commented 10 months ago

Hi Phil,

Thank you so much for all your work with the MIRT package. We've been noticing that when using the multipleGroup function with SE=T and dentype="mixture-h" where h is more than 2, the standard errors are not calculated and causes the R program itself to shut down (Once in a blue moon when I first open R or Rstudio and try to run things for a first time, it works, but from the second try, the R program shuts down). Here is an example:

library(mirt)
#> Loading required package: stats4
#> Loading required package: lattice

set.seed(12345)
nitems <- 20
a1 <- matrix(.50, ncol=1, nrow=nitems)
a2 <- matrix(1.0, ncol=1, nrow=nitems)
a3 <- matrix(2, ncol=1, nrow=nitems)
d1 <- d2 <- d3 <- matrix(rnorm(nitems,0,1),ncol=1)
itemtype <- rep('2PL', nrow(a1))
N1 <- 500
N2 <- N3 <- N1*2 # second class twice as large
dataset1 <- simdata(a1, d1, N1, itemtype)
dataset2 <- simdata(a2, d2, N2, itemtype)
dataset3 <- simdata(a2, d2, N3, itemtype)
dat <- rbind(dataset1, dataset2, dataset3)
# group <- c(rep('D1', N1), rep('D2', N2), rep('D3',N3))
models <- 'F1 = 1-20
CONSTRAIN = (1-20, a1)'
mod_mix <- multipleGroup(dat, models, dentype = 'mixture-3', GenRandomPars = TRUE,TOL=1e-2)
#> Iteration: 1, Log-Lik: -35862.908, Max-Change: 2.75917Iteration: 2, Log-Lik: -30269.332, Max-Change: 0.31715Iteration: 3, Log-Lik: -30179.834, Max-Change: 0.14959Iteration: 4, Log-Lik: -30155.754, Max-Change: 0.07165Iteration: 5, Log-Lik: -30144.826, Max-Change: 0.06674Iteration: 6, Log-Lik: -30138.434, Max-Change: 0.06260Iteration: 7, Log-Lik: -30135.103, Max-Change: 0.05245Iteration: 8, Log-Lik: -30132.224, Max-Change: 0.05442Iteration: 9, Log-Lik: -30129.998, Max-Change: 0.05252Iteration: 10, Log-Lik: -30122.744, Max-Change: 0.05234Iteration: 11, Log-Lik: -30121.637, Max-Change: 0.04835Iteration: 12, Log-Lik: -30120.544, Max-Change: 0.05266Iteration: 13, Log-Lik: -30115.580, Max-Change: 0.04657Iteration: 14, Log-Lik: -30114.407, Max-Change: 0.04928Iteration: 15, Log-Lik: -30113.245, Max-Change: 0.05299Iteration: 16, Log-Lik: -30108.968, Max-Change: 0.04173Iteration: 17, Log-Lik: -30108.331, Max-Change: 0.03458Iteration: 18, Log-Lik: -30107.843, Max-Change: 0.03511Iteration: 19, Log-Lik: -30105.832, Max-Change: 0.02282Iteration: 20, Log-Lik: -30105.672, Max-Change: 0.02046Iteration: 21, Log-Lik: -30105.534, Max-Change: 0.02035Iteration: 22, Log-Lik: -30104.909, Max-Change: 0.01692Iteration: 23, Log-Lik: -30104.841, Max-Change: 0.01686Iteration: 24, Log-Lik: -30104.779, Max-Change: 0.01870Iteration: 25, Log-Lik: -30104.432, Max-Change: 0.01344Iteration: 26, Log-Lik: -30104.383, Max-Change: 0.01442Iteration: 27, Log-Lik: -30104.340, Max-Change: 0.01367Iteration: 28, Log-Lik: -30104.110, Max-Change: 0.01017Iteration: 29, Log-Lik: -30104.082, Max-Change: 0.00986
mod_mix_SE <- multipleGroup(dat, models, dentype = 'mixture-3', GenRandomPars = TRUE,TOL=1e-2, SE=T)

Created on 2023-12-05 with reprex v2.0.2

mod_mix without the SE=T, runs fine but mod_mix_SE results in a fatal error with R session being aborted

Here is a screenshot: image

Just in case it helps, here I paste the sessioninfo as well.

Session info ``` r sessionInfo() #> R version 4.3.2 (2023-10-31 ucrt) #> Platform: x86_64-w64-mingw32/x64 (64-bit) #> Running under: Windows 10 x64 (build 19045) #> #> Matrix products: default #> #> #> locale: #> [1] LC_COLLATE=English_United States.utf8 #> [2] LC_CTYPE=English_United States.utf8 #> [3] LC_MONETARY=English_United States.utf8 #> [4] LC_NUMERIC=C #> [5] LC_TIME=English_United States.utf8 #> #> time zone: America/Los_Angeles #> tzcode source: internal #> #> attached base packages: #> [1] stats4 stats graphics grDevices utils datasets methods #> [8] base #> #> other attached packages: #> [1] mirt_1.41 lattice_0.21-9 #> #> loaded via a namespace (and not attached): #> [1] permute_0.9-7 nlme_3.1-163 cli_3.6.1 #> [4] knitr_1.45 rlang_1.1.2 xfun_0.41 #> [7] GPArotation_2023.11-1 glue_1.6.2 htmltools_0.5.7 #> [10] gridExtra_2.3 rmarkdown_2.25 grid_4.3.2 #> [13] evaluate_0.23 MASS_7.3-60 fastmap_1.1.1 #> [16] yaml_2.3.7 lifecycle_1.0.4 cluster_2.1.4 #> [19] compiler_4.3.2 fs_1.6.3 Rcpp_1.0.11 #> [22] dcurver_0.9.2 Deriv_4.1.3 vegan_2.6-4 #> [25] mgcv_1.9-0 pbapply_1.7-2 rstudioapi_0.15.0 #> [28] digest_0.6.33 reprex_2.0.2 parallel_4.3.2 #> [31] splines_4.3.2 Matrix_1.6-1.1 gtable_0.3.4 #> [34] tools_4.3.2 withr_2.5.2 ```

Could you please help us solve this issue? Thank you.

philchalmers commented 9 months ago

Thanks for the report, should be patched now. I modified your example to verify that it was working correctly (you had some repeated elements in the example that seemed incorrect in this context), so here's the code that I used.

library(mirt)

set.seed(12345)
nitems <- 20
a1 <- matrix(.50, ncol=1, nrow=nitems)
a2 <- matrix(1.0, ncol=1, nrow=nitems)
a3 <- matrix(2, ncol=1, nrow=nitems)
d1 <- matrix(rnorm(nitems,0,1),ncol=1)
d2 <- matrix(rnorm(nitems,0,1),ncol=1)
d3 <- matrix(rnorm(nitems,0,1),ncol=1)
itemtype <- rep('2PL', nrow(a1))
N1 <- 1000
N2 <- N3 <- N1*2 # second class twice as large
dataset1 <- simdata(a1, d1, N1, itemtype)
dataset2 <- simdata(a2, d2, N2, itemtype)
dataset3 <- simdata(a3, d3, N3, itemtype)
dat <- rbind(dataset1, dataset2, dataset3)
# group <- c(rep('D1', N1), rep('D2', N2), rep('D3',N3))

models <- 'F1 = 1-20
CONSTRAIN = (1-20, a1)'
mod_mix_SE <- multipleGroup(dat, models, dentype = 'mixture-3', SE=T, GenRandomPars = TRUE)

coef(mod_mix_SE, printSE=TRUE)
coef(mod_mix_SE, simplify=TRUE)
summary(mod_mix_SE)