createItem() for creating custom graded and gpcm items

174 views
Skip to first unread message

Phil Chalmers

unread,
May 6, 2014, 11:32:40 AM5/6/14
to mirt-p...@googlegroups.com
This is just pointing to a chat that Felix and I had about how to construct your own IRT models from scratch using the createItem() function. Might be useful to someone in the future looking to construct their own IRT model that isn't common. https://github.com/philchalmers/mirt/issues/32#issuecomment-42317346
Message has been deleted

Phil Chalmers

unread,
May 21, 2014, 10:54:29 AM5/21/14
to Dr. Hans Hansen, mirt-package
This should work out of the box. Are you passing an arbitrary response
vector or matrix instead of estimating the scores from the data? I
haven't tested the former yet, so that might be the issue.

Phil

On Wed, May 21, 2014 at 6:58 AM, Dr. Hans Hansen <bea...@gmx.de> wrote:
> hi phil,
>
> when using fscores on a model with items created from scratch i get the
> following error:
>
> Error in LoadPars(itemtype = itemtype, itemloc = itemloc, lambdas = lambdas,
> :
> Unknown itemtype gpcm2 gpcm2 gpcm2 gpcm2 gpcm2 gpcm2 gpcm2 gpcm2
>
> Is that resolvable?
>
> best, felix
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "mirt-package" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mirt-package...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Dr. Hans Hansen

unread,
May 26, 2014, 4:14:58 PM5/26/14
to mirt-p...@googlegroups.com, Dr. Hans Hansen

Hi Phil, you are right, this happens when i pass a response.pattern to fscores(). Thanks, felix

Phil Chalmers

unread,
May 26, 2014, 10:22:20 PM5/26/14
to Dr. Hans Hansen, mirt-package
Thanks Felix; it looks to work on the latest dev version now. See below.

Phil 

> require(mirt)
Loading required package: mirt
Loading required package: stats4
Loading required package: lattice
> 
> name <- 'old2PL'
> par <- c(a = .5, b = -2)
> est <- c(TRUE, TRUE)
> P.old2PL <- function(par,Theta, ncat){
+     a <- par[1]
+     b <- par[2]
+     P1 <- 1 / (1 + exp(-1*a*(Theta - b)))
+     cbind(1-P1, P1)
+ }
> x <- createItem(name, par=par, est=est, P=P.old2PL)
> 
> dat <- expand.table(LSAT7)
> mod <- mirt(dat, 1, c(rep('2PL',4), 'old2PL'), customItems=list(old2PL=x))
Iteration: 24, Log-Lik: -2658.805, Max-Change: 0.00005
> fscores(mod, response.pattern=c(0,0,1,1,0))
     Item.1 Item.2 Item.3 Item.4 Item.5 Freq         F1     SE_F1
[1,]      0      0      1      1      0    1 -0.7549973 0.6728278
> fscores(mod, response.pattern=matrix(c(0,0,1,1,0, 1,1,0,0,1), 2, byrow=TRUE))
     Item.1 Item.2 Item.3 Item.4 Item.5 Freq         F1     SE_F1
[1,]      0      0      1      1      0    1 -0.7549973 0.6728278
[2,]      1      1      0      0      1    1 -0.5995563 0.6803557
> 
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8       
 [4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
 [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] mirt_1.3.6      lattice_0.20-29 testthat_0.8.1  devtools_1.5   

loaded via a namespace (and not attached):
 [1] digest_0.6.4         evaluate_0.5.5       GPArotation_2012.3-1 grid_3.1.0          
 [5] httr_0.3             memoise_0.2.1        numDeriv_2012.9-1    parallel_3.1.0      
 [9] Rcpp_0.11.1          RCurl_1.95-4.1       stringr_0.6.2        tools_3.1.0         
[13] whisker_0.3-2 


On Mon, May 26, 2014 at 4:14 PM, Dr. Hans Hansen <bea...@gmx.de> wrote:

Hi Phil, you are right, this happens when i pass a response.pattern to fscores(). Thanks, felix

--

Dr. Hans Hansen

unread,
May 28, 2014, 12:08:21 PM5/28/14
to mirt-p...@googlegroups.com, Dr. Hans Hansen
great, thanks a lot!


Dr. Hans Hansen

unread,
Jun 11, 2014, 10:49:41 AM6/11/14
to mirt-p...@googlegroups.com, bea...@gmx.de
Hi Phil,

i found another problem regarding this issue: i cannot fix the item parameters and calculate mean/cov:

library(mirt)
set.seed(1223)
name <- 'gpcm2'
par <- c(a = 1.5, b1 = -1, b2 = .3, b3 = 1)
est <- c(TRUE, TRUE, TRUE, TRUE)

summation = function(upto, Theta = seq(-4,4,.1), a, b){
    x = rep(0,length(Theta))
    if (upto > 0) x = apply(sapply(1:upto, function(x) (a*(Theta - b[x]))),1,sum)
    return(exp(x))
}

P.gpcm2 <- function(par,Theta, ncat){
    a <- par[1]
    b <- par[2:length(par)]
    nominator = sapply(0:(ncat-1), summation, Theta = Theta, a = a, b = b)
    denominator = apply(nominator,1,sum)
    ret <- nominator/denominator
    ret <- ifelse(ret > (1-1e-10), (1-1e-10), ret)
    ret <- ifelse(ret < 1e-10, 1e-10, ret)
    return(ret)
}

x <- createItem(name, par=par, est=est, P=P.gpcm2)

dat <- Science
mod <- mirt( dat, 1, "gpcm2", customItems=list(gpcm2=x))

pars = mod2values(mod)
pars$est = pars$est==FALSE
mod <- mirt( dat, 1, "gpcm2", customItems=list(gpcm2=x), pars=pars)

would be great if you could look into this!

thanks a lot, felix

Phil Chalmers

unread,
Jun 11, 2014, 10:58:24 AM6/11/14
to Dr. Hans Hansen, mirt-package
What is the line 'pars$est = pars$est==FALSE' supposed to be doing; do you mean pars$est <- FALSE?

Phil


--

Dr. Hans Hansen

unread,
Jun 11, 2014, 11:31:28 AM6/11/14
to

No, it should set the item parameters to FALSE, but the GROUP parameters to TRUE. (It doesnt make too much sense here, but i use item parameters from other models).

Phil Chalmers

unread,
Jun 11, 2014, 11:37:22 AM6/11/14
to Dr. Hans Hansen, mirt-package
So, something like this?

pars = mod2values(mod)
pars$est <- FALSE
pars$est[17:18] <- TRUE

mod <- mirt( dat, 1, "gpcm2", customItems=list(gpcm2=x), pars=pars)

This seems to work fine on the latest dev (converges in 2 iterations, 9 if you set TOL = 1e-7). 

> coef(mod, digits = 10)
$Comfort
            a        b1        b2       b3
par 0.8652818 -3.268075 -2.881734 1.532433

$Work
            a        b1        b2       b3
par 0.8403551 -2.035006 -1.032612 2.058211

$Future
           a        b1         b2        b3
par 2.215178 -2.087085 -0.9778947 0.8335761

$Benefit
            a        b1        b2       b3
par 0.7233524 -2.899758 -1.105971 1.627087

$GroupPars
         MEAN_1    COV_11
par 5.48909e-05 0.9997952


On Wed, Jun 11, 2014 at 11:31 AM, Dr. Hans Hansen <bea...@gmx.de> wrote:

No, it should set the item parameters to FALSE, but the GROUP parameters to TRUE.

--
Reply all
Reply to author
Forward
0 new messages