#############################################################################
# SIMULATED EXAMPLE 1: Simulated data according to the Nedelsky model
#############################################################################
#*** simulate data
set.seed(123)
I <- 20 # number of items
b <- matrix(NA,I,ncol=3)
b[,1] <- -.5 + runif( I , -.75 , .75 )
b[,2] <- -1.5 + runif( I , -.75 , .75 )
b[,3] <- -2.5 + runif( I , -.75 , .75 )
K <- 3 # number of distractors
N <- 2000 # number of persons
# apply simulation function
dat <- nedelsky.sim( theta = rnorm(N,sd=1.2) , b=b )
#*** latent response patterns
K <- 3
combis <- nedelsky.latresp(K=3)
#*** defining the Nedelky item response function for estimation in mirt
par <- c( 3 , rep(-1,K) , 1 , rep(1,K+1) ,1)
names(par) <- c("K" , paste0("b",1:K) , "a" , paste0("tau" , 0:K) ,"thdim")
est <- c( FALSE , rep(TRUE,K) , rep(FALSE , K+1 + 2 ) )
names(est) <- names(par)
nedelsky.icc <- function( par , Theta , ncat ){
K <- par[1]
b <- par[ 1:K + 1]
a <- par[ K+2]
tau <- par[1:(K+1) + (K+2) ]
thdim <- par[ K+2+K+1 +1 ]
probs <- nedelsky.irf( Theta , K=K , b=b , a=a , tau=tau , combis ,
thdim=thdim )$probs
return(probs)
}
name <- "nedelsky"
# create item response function
nedelsky.itemfct <- mirt::createItem(name, par=par, est=est, P=nedelsky.icc)
#*** define model in mirt
mirtmodel <- mirt::mirt.model("
F1 = 1-20
COV = F1*F1
# define some prior distributions
PRIOR = (1-20,b1 ,norm,-1,2),(1-20,b2 ,norm,-1,2),
(1-20,b3 ,norm,-1,2)
" )
itemtype <- rep("nedelsky" , I )
customItems <- list("nedelsky"= nedelsky.itemfct)
# define parameters to be estimated
mod1.pars <- mirt::mirt(dat, mirtmodel , itemtype=itemtype ,
customItems=customItems, pars = "values")
# estimate model
mod1 <- mirt::mirt(dat,mirtmodel , itemtype=itemtype , customItems=customItems,
pars = mod1.pars , verbose=TRUE )
# model summaries
print(mod1)
summary(mod1)
mirt.wrapper.coef( mod1 )$coef
mirt.wrapper.itemplot(mod1 ,ask=TRUE)
#############################################################################
# EXAMPLE 2: Multiple choice dataset data.si06
#############################################################################
data(data.si06)
dat <- data.si06
#*** create latent responses
combis <- nedelsky.latresp(K)
I <- ncol(dat)
#*** define item response function
K <- 3
par <- c( 3 , rep(-1,K) , 1 , rep(1,K+1) ,1)
names(par) <- c("K" , paste0("b",1:K) , "a" , paste0("tau" , 0:K) ,"thdim")
est <- c( FALSE , rep(TRUE,K) , rep(FALSE , K+1 + 2 ) )
names(est) <- names(par)
nedelsky.icc <- function( par , Theta , ncat ){
K <- par[1]
b <- par[ 1:K + 1]
a <- par[ K+2]
tau <- par[1:(K+1) + (K+2) ]
thdim <- par[ K+2+K+1 +1 ]
probs <- nedelsky.irf( Theta , K=K , b=b , a=a , tau=tau , combis ,
thdim=thdim )$probs
return(probs)
}
name <- "nedelsky"
# create item response function
nedelsky.itemfct <- mirt::createItem(name, par=par, est=est, P=nedelsky.icc)
#*** define model in mirt
mirtmodel <- mirt::mirt.model("
F1 = 1-14
COV = F1*F1
PRIOR = (1-14,b1 ,norm,-1,2),(1-14,b2 ,norm,-1,2),
(1-14,b3 ,norm,-1,2)
" )
itemtype <- rep("nedelsky" , I )
customItems <- list("nedelsky"= nedelsky.itemfct)
# define parameters to be estimated
mod1.pars <- mirt::mirt(dat, mirtmodel , itemtype=itemtype ,
customItems=customItems, pars = "values")
#*** estimate model
mod1 <- mirt::mirt(dat,mirtmodel , itemtype=itemtype , customItems=customItems,
pars = mod1.pars , verbose=TRUE )
#*** summaries
print(mod1)
summary(mod1)
mirt.wrapper.coef( mod1 )$coef
mirt.wrapper.itemplot(mod1 ,ask=TRUE)
Run the code above in your browser using DataLab