Learn R Programming

sirt (version 0.47-36)

mirt.wrapper: Some Functions for Wrapping with the mirt Package

Description

Some functions for wrapping with the mirt package.

Usage

# extract coefficients	
mirt.wrapper.coef(mirt.obj)

# extract posterior, likelihood, ...
mirt.wrapper.posterior(mirt.obj,weights=NULL)

# compute factor scores
mirt.wrapper.posterior(mirt.obj,weights=NULL)

Arguments

mirt.obj
A fitted model in mirt package
weights
Optional vector of student weights

Value

  • Function mirt.wrapper.coef -- List with entries
  • coefData frame with item parameters
  • GroupParsData frame or list with distribution parameters
  • Function mirt.wrapper.posterior -- List with entries
  • theta.kGrid of theta points
  • pi.kTrait distribution on theta.k
  • f.yi.qkIndividual likelihood
  • f.qk.yiIndividual posterior
  • n.ikExpected counts
  • dataUsed dataset
  • Function mirt.wrapper.fscores -- List with entries
  • personData frame with person parameter estimates (factor scores) EAP, MAP and MLE for all dimensions.
  • EAP.relEAP reliabilities

Details

The function mirt.wrapper.coef collects all item parameters in a data frame. The function mirt.wrapper.posterior extracts the individual likelihood, individual likelihood and expected counts. This function does not yet cover the case of multiple groups. The function mirt.wrapper.fscores computes factor scores EAP, MAP and MLE. The factor scores are computed on the discrete grid of latent traits (contrary to mirt!) as specified in mirt.obj@Theta. This function does also not work for multiple groups.

See Also

See the mirt package on CRAN: http://cran.r-project.org/web/packages/mirt/index.html See the online vignette of mirt: http://philchalmers.github.io/mirt/mirt-vignettes.html See https://groups.google.com/forum/#!forum/mirt-package for discussion about the mirt package. See coef-method (mirt) for extracting cofficients. See mod2values (mirt) for collecting parameter values in a mirt parameter table. See lavaan2mirt for converting lavaan syntax to mirt syntax. See tam2mirt for converting fitted tam models into mirt objects.

Examples

Run this code
library(mirt)

#############################################################################
# EXAMPLE 1: Extracting item parameters and posterior LSAT data
#############################################################################

data(LSAT7, package="mirt")
data <- mirt::expand.table(LSAT7)

#*** Model 1: 3PL model for item 5 only, other items 2PL
mod1 <- mirt::mirt(data, 1, itemtype=c("2PL","2PL","2PL","2PL","3PL") , verbose=TRUE)
print(mod1)
summary(mod1)
# extracting coefficients
coef(mod1)
mirt.wrapper.coef(mod1)$coef
# extract parameter values in mirt
mirt::mod2values(mod1)
# extract posterior
post1 <- mirt.wrapper.posterior(mod1)

#*** Model 2: Confirmatory model with two factors
cmodel <- mirt::mirt.model("
        F1 = 1,4,5
        F2 = 2,3
        ")
mod2 <- mirt::mirt(data, cmodel , verbose=TRUE)
print(mod2)
summary(mod2)
# extract coefficients
coef(mod2)
mirt.wrapper.coef(mod2)$coef
# extract posterior
post2 <- mirt.wrapper.posterior(mod2)

#############################################################################
# EXAMPLE 2: Extracting item parameters and posterior for differering 
#            number of response catagories | Dataset Science
#############################################################################

data(Science,package="mirt")
library(psych)
psych::describe(Science)

# modify dataset
dat <- Science
dat[ dat[,1] > 3 ,1] <- 3
psych::describe(dat)

# estimate generalized partial credit model
mod1 <- mirt::mirt(dat, 1, itemtype="gpcm")
print(mod1)
# extract coefficients
coef(mod1)
mirt.wrapper.coef(mod1)$coef
# extract posterior
post1 <- mirt.wrapper.posterior(mod1)

#############################################################################
# SIMULATED EXAMPLE 3: Multiple group model; simulated dataset from
#        mirt package
#############################################################################

#*** simulate data (copy from the multipleGroup manual site in mirt package)
set.seed(1234)
a <- matrix(c(abs(rnorm(5,1,.3)), rep(0,15),abs(rnorm(5,1,.3)),
          rep(0,15),abs(rnorm(5,1,.3))), 15, 3)
d <- matrix(rnorm(15,0,.7),ncol=1)
mu <- c(-.4, -.7, .1)
sigma <- matrix(c(1.21,.297,1.232,.297,.81,.252,1.232,.252,1.96),3,3)
itemtype <- rep("dich", nrow(a))
N <- 1000
dataset1 <- mirt::simdata(a, d, N, itemtype)
dataset2 <- mirt::simdata(a, d, N, itemtype, mu = mu, sigma = sigma)
dat <- rbind(dataset1, dataset2)
group <- c(rep("D1", N), rep("D2", N))

#group models
model <- mirt::mirt.model("
   F1 = 1-5
   F2 = 6-10
   F3 = 11-15
      ")

# separate analysis
mod_configural <- multipleGroup(dat, model, group = group , verbose=TRUE)
mirt.wrapper.coef(mod_configural)

# equal slopes (metric invariance)
mod_metric <- multipleGroup(dat, model, group = group, invariance=c("slopes") , 
                verbose=TRUE)
mirt.wrapper.coef(mod_metric)
                  
# equal slopes and intercepts (scalar invariance)
mod_scalar <- multipleGroup(dat, model, group = group,  
    invariance=c("slopes","intercepts","free_means","free_varcov"), verbose=TRUE)
mirt.wrapper.coef(mod_scalar)

# full constraint
mod_fullconstrain <- multipleGroup(dat, model, group = group,  
    invariance=c("slopes", "intercepts") , verbose=TRUE )
mirt.wrapper.coef(mod_fullconstrain)

#############################################################################
# EXAMPLE 4: Nonlinear item response model
#############################################################################

data(data.read)
dat <- data.read
# specify mirt model with some interactions
mirtmodel <- mirt.model("
   A = 1-4
   B = 5-8
   C = 9-12
   (A*B) = 4,8
   (C*C) = 9
   (A*B*C) = 12
   " )
# estimate model
res <- mirt::mirt( dat , mirtmodel , verbose=TRUE , technical=list(NCYCLES=3) )
# look at estimated parameters
mirt.wrapper.coef(res)
coef(res)
mirt::mod2values(res)
# model specification
res@model

#############################################################################
# EXAMPLE 5: Extracting factor scores
#############################################################################

data(data.read)
dat <- data.read
# define lavaan model convert syntax to mirt
lavmodel <- "
    A=~ a*A1+a*A2+1.3*A3+A4 # set loading of A3 to 1.3
    B=~ B1+1*B2+b3*B3+B4
    C=~ c*C1+C2+c*C3+C4
    A1 | da*t1
    A3 | da*t1
    C4 | dg*t1
    B1 | 0*t1
    B3 | -1.4*t1 # fix item threshold of B3 to -1.4
    A ~~ B # estimate covariance between A and B
    A ~~ .6 * C # fix covariance to .6
    B ~~ B # estimate variance of B
    A ~ .5*1 # set mean of A to .5
    B ~ 1 # estimate mean of B
    "
res <- lavaan2mirt( dat , lavmodel , verbose=TRUE , technical=list(NCYCLES=3) )
# estimated coefficients
mirt.wrapper.coef(res$mirt)
# extract factor scores
fres <- mirt.wrapper.fscores(res$mirt)
# look at factor scores
head( round(fres$person,2))
  ##     case    M EAP.Var1 SE.EAP.Var1 EAP.Var2 SE.EAP.Var2 EAP.Var3 SE.EAP.Var3 MLE.Var1
  ##   1    1 0.92     1.26        0.67     1.61        0.60     0.05        0.69     2.65
  ##   2    2 0.58     0.06        0.59     1.14        0.55    -0.80        0.56     0.00
  ##   3    3 0.83     0.86        0.66     1.15        0.55     0.48        0.74     0.53
  ##   4    4 1.00     1.52        0.67     1.57        0.60     0.73        0.76     2.65
  ##   5    5 0.50    -0.13        0.58     0.85        0.48    -0.82        0.55    -0.53
  ##   6    6 0.75     0.41        0.63     1.09        0.54     0.27        0.71     0.00
  ##     MLE.Var2 MLE.Var3 MAP.Var1 MAP.Var2 MAP.Var3
  ##   1     2.65    -0.53     1.06     1.59     0.00
  ##   2     1.06    -1.06     0.00     1.06    -1.06
  ##   3     1.06     2.65     1.06     1.06     0.53
  ##   4     2.65     2.65     1.59     1.59     0.53
  ##   5     0.53    -1.06    -0.53     0.53    -1.06
  ##   6     1.06     2.65     0.53     1.06     0.00
# EAP reliabilities
round(fres$EAP.rel,3)
  ##    Var1  Var2  Var3 
  ##   0.574 0.452 0.541

Run the code above in your browser using DataLab