philchalmers / mirt

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

Error when trying to plot DIF #185

Closed isaactpetersen closed 4 years ago

isaactpetersen commented 4 years ago

I get the following error when trying to plot DIF from the DIF() function:

Error in plt$cat:plt$group : NA/NaN argument
In addition: Warning messages:
1: In plt$cat:plt$group :
  numerical expression has 8400 elements: only the first used
2: In plt$cat:plt$group :
  numerical expression has 8400 elements: only the first used
3: In eval(substitute(groups), data, environment(x)) :
  NAs introduced by coercion

I'm attaching the data file here: data.zip. Here's the code I used:

constrainedModel <- multipleGroup(data = cnlsy[,c("bpi_antisocial1","bpi_antisocial2","bpi_antisocial3","bpi_antisocial4","bpi_antisocial5","bpi_antisocial6","bpi_antisocial7")], model = 1, group = cnlsy$sex, invariance = c(c("bpi_antisocial1","bpi_antisocial2","bpi_antisocial3","bpi_antisocial4","bpi_antisocial5","bpi_antisocial6","bpi_antisocial7"), "free_means", "free_var"), SE = TRUE, technical = list(removeEmptyRows = TRUE))
DIF(constrainedModel, c("a1","d1","d2"), plotdif = TRUE)

Thanks in advance! I love the package, and thanks for your help developing it.

philchalmers commented 4 years ago

Part of the problem here is that you have a fully constrained model but are using the default 'add' scheme, which attempts to add additional equity constraints (which there are none). Hence, no item shows any DIF, and so the plot tries but prints nothing at all.

Try changing scheme = 'drop' to remove these constraints on and item-by-item test. Though note that plotdif is not currently supported for dropping schemes (though I supposed it could be...).

philchalmers commented 4 years ago

I also added the use of <= instead of the previous < logical constructors to help detect errors such as this. Thanks for the report.

isaactpetersen commented 4 years ago

Thanks, Phil. For some reason, I'm still getting the same error when trying to use plotdif in the examples provided on the mirt GitHub page. For example, I get the same error when I try to run the following line (from the example here: https://philchalmers.github.io/mirt/html/DIF.html): resulta1d <- DIF(model, c('a1', 'd'), plotdif = TRUE)

Any ideas? Thanks again.

philchalmers commented 4 years ago

The example you link to is using plotdif on a supplied model that was less constrained (i.e., the parameter are not first set equal across groups in the model object). In your examples, the parameters were initially set equal in model, which is why it currently isn't supported (yet).

isaactpetersen commented 4 years ago

Sorry, Phil, my explanation wasn't very clear. I get the same error even when using the supplied model in the example. That is, I get the same error when I run the following code:

#simulate data where group 2 has a smaller slopes and more extreme intercepts
set.seed(12345)
a1 <- a2 <- matrix(abs(rnorm(15,1,.3)), ncol=1)
d1 <- d2 <- matrix(rnorm(15,0,.7),ncol=1)
a2[1:2, ] <- a1[1:2, ]/3
d1[c(1,3), ] <- d2[c(1,3), ]/4
head(data.frame(a.group1 = a1, a.group2 = a2, d.group1 = d1, d.group2 = d2))

itemtype <- rep('2PL', nrow(a1))
N <- 1000
dataset1 <- simdata(a1, d1, N, itemtype)
dataset2 <- simdata(a2, d2, N, itemtype, mu = .1, sigma = matrix(1.5))
dat <- rbind(dataset1, dataset2)
group <- c(rep('D1', N), rep('D2', N))

#### no anchors, all items tested for DIF by adding item constrains one item at a time.
# define a parallel cluster (optional) to help speed up internal functions
mirtCluster()

# Information matrix with Oakes' identity (not controlling for latent group differences)
# NOTE: Without properly equating the groups the following example code is not testing for DIF,
     # but instead reflects a combination of DIF + latent-trait distribution effects
model <- multipleGroup(dat, 1, group, SE = TRUE)

#test whether adding slopes and intercepts constraints results in DIF. Plot items showing DIF
resulta1d <- DIF(model, c('a1', 'd'), plotdif = TRUE)
No hyper-parameters were estimated in the DIF model. For effective DIF testing, freeing the focal group hyper-parameters is recommended.
Error in plt$cat:plt$group : NA/NaN argument
In addition: Warning messages:
1: In plt$cat:plt$group :
  numerical expression has 1600 elements: only the first used
2: In plt$cat:plt$group :
  numerical expression has 1600 elements: only the first used
3: In eval(substitute(groups), data, environment(x)) :
  NAs introduced by coercion
philchalmers commented 4 years ago

No problem, and thanks for the reprex. The above code works fine for me on the dev version (mirt 1.32.2), where I get the following graph.

Rplot

Did you install the latest dev using devtools::install_github('philchalmers/mirt') and check that you have the latest version installed?

isaactpetersen commented 4 years ago

That's probably the issue. I'm using the latest version of mirt on CRAN (1.32.1), but not the dev version (1.32.2)--I'm now seeing on the main page of the github repo that you recommend using the latest dev version. I just tried to install the latest dev version using the command you suggested but received the following error:

C:/rtools40/mingw32/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o mirt.dll tmp.def Estep.o Misc.o dpars.o ggum_derivs.o traceLinePts.o Welcome at Mon Jun 08 09:42:56 2020 Goodbye at Mon Jun 08 09:42:56 2020 -LC:/PROGRA~1/R/R-40~1.0/bin/i386 -lRlapack -LC:/PROGRA~1/R/R-40~1.0/bin/i386 -lRblas -lgfortran -lm -lquadmath -fopenmp -LC:/PROGRA~1/R/R-40~1.0/bin/i386 -lR
g++.exe: error: Welcome: No such file or directory
g++.exe: error: at: No such file or directory
g++.exe: error: Mon: No such file or directory
g++.exe: error: Jun: No such file or directory
g++.exe: error: 08: No such file or directory
g++.exe: error: 09:44:48: Invalid argument
g++.exe: error: 2020: No such file or directory
g++.exe: error: Goodbye: No such file or directory
g++.exe: error: at: No such file or directory
g++.exe: error: Mon: No such file or directory
g++.exe: error: Jun: No such file or directory
g++.exe: error: 08: No such file or directory
g++.exe: error: 09:44:48: Invalid argument
g++.exe: error: 2020: No such file or directory
no DLL was created
ERROR: compilation failed for package 'mirt'
* removing 'C:/Program Files/R/Packages/mirt'
* restoring previous 'C:/Program Files/R/Packages/mirt'
Error: Failed to install 'mirt' from GitHub:
  (converted from warning) installation of package ‘C:/Users/Isaac/AppData/Local/Temp/Rtmp0ouOGW/file3aa865b8715e/mirt_1.32.2.tar.gz’ had non-zero exit status

I have devtools and Rtools installed and verified that the following file exists: C:\rtools40\mingw32\bin\g++.exe

philchalmers commented 4 years ago

Is g++.exe in your windows environmental path? It looks like your CMD program can't locate the correct executable (you should be able to open a CMD window and type g++.exe --version to locate the program and it's version).

isaactpetersen commented 4 years ago

Hmm, the command in the CMD program works:

g++.exe (Built by Jeroen for the R-project) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I realize that this isn't a mirt issue, so I don't want to take more of your time on it. I can look more into it. I appreciate all your help already, though. I'm happy to wait until the dev version is released to CRAN. Thanks again for your work on this great package.