library(sirt)
library(psychotools)
library(TAM)
library(CDM)
library(eRm)
#############################################################################
# EXAMPLE 1: Dichotomous data data.read
#############################################################################
data(data.read , package="sirt")
dat <- data.read
I <- ncol(dat)
#----------------------------------------------------------------
#--- Model 1: Rasch model, setting first item difficulty to zero
mod1a <- immer_cml( dat=dat)
summary(mod1a)
logLik(mod1a) # extract log likelihood
coef(mod1a) # extract coefficients
# estimate model in psychotools package
mod1b <- psychotools::raschmodel(dat)
summary(mod1b)
logLik(mod1b)
# estimate model in eRm package
mod1c <- eRm::RM(dat, sum0=FALSE)
summary(mod1c)
mod1c$etapar
# compare estimates of three packages
cbind( coef(mod1a) , coef(mod1b) , mod1c$etapar )
#----------------------------------------------------------------
#-- Model 2: Rasch model sum normalization
mod2a <- immer_cml( dat=dat , normalization = "sum")
summary(mod2a)
# compare estimation in TAM
mod2b <- tam.mml( dat , constraint="items" )
summary(mod2b)
mod2b$A[,2,]
#----------------------------------------------------------------
#--- Model 3: some fixed item parameters
# fix item difficulties of items 1,4,8
# define fixed parameters in constant parameter vector
b_const <- rep(0,I)
fix_items <- c(1,4,8)
b_const[ fix_items ] <- c( -2.1 , .195 , -.95 )
# design matrix
W <- matrix( 0 , nrow=12 , ncol=9)
W[ cbind( setdiff( 1:12 , fix_items ) , 1:9 ) ] <- 1
colnames(W) <- colnames(dat)[ - fix_items ]
# estimate model
mod3 <- immer_cml( dat=dat , W=W , b_const=b_const)
summary(mod3)
#----------------------------------------------------------------
#--- Model 4: One parameter logistic model
# estimate non-integer item discriminations with 2PL model
I <- ncol(dat)
mod4a <- sirt::rasch.mml2( dat , est.a = 1:I )
summary(mod4a)
a <- mod4a$item$a # extract (non-integer) item discriminations
# estimate integer item discriminations ranging from 1 to 3
a_integer <- immer_opcat( a , hmean = 2 , min = 1 , max = 3 )
# estimate one-parameter model with fixed integer item discriminations
mod4 <- immer_cml( dat=dat , a = a_integer )
summary(mod4)
#----------------------------------------------------------------
#--- Model 5: Linear logistic test model
# define design matrix
W <- matrix( 0 , nrow=12 , ncol=5 )
colnames(W) <- c("B","C", paste0("Pos" , 2:4))
rownames(W) <- colnames(dat)
W[ 5:8 , "B" ] <- 1
W[ 9:12 , "C" ] <- 1
W[ c(2,6,10) , "Pos2" ] <- 1
W[ c(3,7,11) , "Pos3" ] <- 1
W[ c(4,8,12) , "Pos4" ] <- 1
# estimation with immer_cml
mod5a <- immer_cml( dat , W=W )
summary(mod5a)
# estimation in eRm package
mod5b <- eRm::LLTM( dat , W=W )
summary(mod5b)
# compare models 1 and 5 by a likelihood ratio test
anova( mod1a , mod5a )
#############################################################################
# EXAMPLE 2: Polytomous data | data.Students
#############################################################################
data(data.Students,package="CDM")
dat <- data.Students
dat <- dat[ , grep("act" , colnames(dat) ) ]
dat <- dat[1:400,] # select a subdataset
dat <- dat[ rowSums( 1 - is.na(dat) ) > 1 , ]
# remove persons with less than two valid responses
#----------------------------------------------------------------
#--- Model 1: Partial credit model with constraint on first parameter
mod1a <- immer_cml( dat= dat )
summary(mod1a)
# compare pcmodel function from psychotools package
mod1b <- psychotools::pcmodel( dat )
summary(mod1b)
# estimation in eRm package
mod1c <- eRm::PCM( dat , sum0=FALSE )
# -> subjects with only one valid response must be removed
summary(mod1c)
#----------------------------------------------------------------
#-- Model 2: Partial credit model with sum constraint on item difficulties
mod2a <- immer_cml( dat=dat , irtmodel="PCM2" , normalization="sum")
summary(mod2a)
# compare with estimation in TAM
mod2b <- TAM::tam.mml( dat , irtmodel="PCM2" , constraint="items")
summary(mod2b)
#----------------------------------------------------------------
#-- Model 3: Partial credit model with fixed integer item discriminations
mod3 <- immer_cml( dat= dat , normalization="first", a=c(2,2,1,3,1) )
summary(mod3)
#############################################################################
# EXAMPLE 3: Polytomous data | Extracting the structure of W matrix
#############################################################################
data(data.mixed1, package="sirt")
dat <- data.mixed1
# use non-exported function "lpcm_data_prep" to extract the meaning
# of the rows in W which are contained in value "pars_info"
res <- immer:::lpcm_data_prep( dat , weights=NULL , a=NULL )
pi2 <- res$pars_info
# create design matrix with some restrictions on item parameters
W <- matrix( 0 , nrow=nrow(pi2) , ncol=2 )
colnames(W) <- c( "P2" , "P3" )
rownames(W) <- res$parnames
# joint item parameter for items I19 and I20 fixed at zero
# item parameter items I21 and I22
W[ 3:10 , 1 ] <- pi2$cat[ 3:10 ]
# item parameters I23, I24 and I25
W[ 11:13 , 2] <- 1
# estimate model with design matrix W
mod <- immer_cml( dat, W=W)
summary(mod)
#############################################################################
# EXAMPLE 4: Partial credit model with raters
#############################################################################
data(data.immer07)
dat <- data.immer07
#*** reshape dataset for one variable
dfr1 <- immer_reshape_wideformat( dat$I1 , rater = dat$rater , pid = dat$pid )
#-- extract structure of design matrix
res <- immer:::lpcm_data_prep( dat=dfr1[,-1] , weights=NULL , a=NULL)
pars_info <- res$pars_info
# specify design matrix for partial credit model and main rater effects
# -> set sum of all rater effects to zero
W <- matrix( 0 , nrow=nrow(pars_info) , ncol=3+2 )
rownames(W) <- rownames(pars_info)
colnames(W) <- c( "Cat1" , "Cat2" , "Cat3" , "R1" , "R2" )
# define item parameters
W[ cbind( pars_info$index , pars_info$cat ) ] <- 1
# define rater parameters
W[ paste(pars_info$item) == "R1" , "R1" ] <- 1
W[ paste(pars_info$item) == "R2" , "R2" ] <- 1
W[ paste(pars_info$item) == "R3" , c("R1","R2") ] <- -1
# set parameter of first category to zero for identification constraints
W <- W[,-1]
# estimate model
mod <- immer_cml( dfr1[,-1] , W=W)
summary(mod)
#############################################################################
# EXAMPLE 5: Multi-faceted Rasch model | Estimation with a design matrix
#############################################################################
data(data.immer07)
dat <- data.immer07
#*** reshape dataset
dfr1 <- immer_reshape_wideformat( dat[, paste0("I",1:4) ] , rater = dat$rater ,
pid = dat$pid )
#-- structure of design matrix
res <- immer:::lpcm_data_prep( dat=dfr1[,-1] , weights=NULL , a=NULL)
pars_info <- res$pars_info
#--- define design matrix for multi-faceted Rasch model with only main effects
W <- matrix( 0 , nrow=nrow(pars_info) , ncol=3+2+2 )
parnames <- rownames(W) <- rownames(pars_info)
colnames(W) <- c( paste0("I",1:3) , paste0("Cat",1:2) , paste0("R",1:2) )
#+ define item effects
for (ii in c("I1","I2","I3") ){
ind <- grep( ii , parnames )
W[ ind , ii ] <- pars_info$cat[ind ]
}
ind <- grep( "I4" , parnames )
W[ ind , c("I1","I2","I3") ] <- -pars_info$cat[ind ]
#+ define step parameters
for (cc in 1:2 ){
ind <- which( pars_info$cat == cc )
W[ ind , paste0("Cat",1:cc) ] <- 1
}
#+ define rater effects
for (ii in c("R1","R2") ){
ind <- grep( ii , parnames )
W[ ind , ii ] <- pars_info$cat[ind ]
}
ind <- grep( "R3" , parnames )
W[ ind , c("R1","R2") ] <- -pars_info$cat[ind ]
#--- estimate model with immer_cml
mod1 <- immer_cml( dfr1[,-1] , W=W , par_init = rep(0,ncol(W) ) )
summary(mod1)
#--- comparison with estimation in TAM
resp <- dfr1[,-1]
mod2 <- TAM::tam.mml.mfr( resp=dat[,-c(1:2)] , facets = dat[, "rater" , drop=FALSE ] ,
pid = dat$pid , formulaA = ~ item + step + rater )
summary(mod2)
Run the code above in your browser using DataLab