# NOT RUN {
#############################################################################
# EXAMPLE 1: Simulated DINA data | different condensation rules
#############################################################################
data(sim.dina, package="CDM")
data(sim.qmatrix, package="CDM")
dat <- sim.dina
Q <- sim.qmatrix
#***
# Model 1: estimation of the GDINA model (identity link)
mod1 <- CDM::gdina( data=dat, q.matrix=Q)
summary(mod1)
plot(mod1) # apply plot function
# }
# NOT RUN {
# Model 1a: estimate model with different simulation seed
mod1a <- CDM::gdina( data=dat, q.matrix=Q, seed=9089)
summary(mod1a)
# Model 1b: estimate model with some fixed delta parameters
delta.fixed <- as.list( rep(NA,9) ) # List for parameters of 9 items
delta.fixed[[2]] <- c( 0, .15, .15, .45 )
delta.fixed[[6]] <- c( .25, .25 )
mod1b <- CDM::gdina( data=dat, q.matrix=Q, delta.fixed=delta.fixed)
summary(mod1b)
# Model 1c: fix all delta parameters to previously fitted model
mod1c <- CDM::gdina( data=dat, q.matrix=Q, delta.fixed=mod1$delta)
summary(mod1c)
# Model 1d: estimate GDINA model with GDINA package
mod1d <- GDINA::GDINA( dat=dat, Q=Q, model="GDINA" )
summary(mod1d)
# extract item parameters
GDINA::itemparm(mod1d)
GDINA::itemparm(mod1d, what="delta")
# compare likelihood
logLik(mod1)
logLik(mod1d)
#***
# Model 2: estimation of the DINA model with gdina function
mod2 <- CDM::gdina( data=dat, q.matrix=Q, rule="DINA")
summary(mod2)
plot(mod2)
#***
# Model 2b: compare results with din function
mod2b <- CDM::din( data=dat, q.matrix=Q, rule="DINA")
summary(mod2b)
# Model 2: estimation of the DINO model with gdina function
mod3 <- CDM::gdina( data=dat, q.matrix=Q, rule="DINO")
summary(mod3)
#***
# Model 4: DINA model with logit link
mod4 <- CDM::gdina( data=dat, q.matrix=Q, rule="DINA", linkfct="logit" )
summary(mod4)
#***
# Model 5: DINA model log link
mod5 <- CDM::gdina( data=dat, q.matrix=Q, rule="DINA", linkfct="log")
summary(mod5)
#***
# Model 6: RRUM model
mod6 <- CDM::gdina( data=dat, q.matrix=Q, rule="RRUM")
summary(mod6)
#***
# Model 7: Higher order GDINA model
mod7 <- CDM::gdina( data=dat, q.matrix=Q, HOGDINA=1)
summary(mod7)
#***
# Model 8: GDINA model with independent attributes
mod8 <- CDM::gdina( data=dat, q.matrix=Q, HOGDINA=0)
summary(mod8)
#***
# Model 9: Estimating the GDINA model with monotonicity constraints
mod9 <- CDM::gdina( data=dat, q.matrix=Q, rule="GDINA",
mono.constr=TRUE, linkfct="logit")
summary(mod9)
#***
# Model 10: Estimating the ACDM model with SCAD penalty and regularization
# parameter of .05
mod10 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM",
linkfct="logit", regular_type="scad", regular_lam=.05 )
summary(mod10)
#***
# Model 11: Estimation of GDINA model with prior distributions
# N(0,10^2) prior for item intercepts
prior_intercepts <- c(0,10)
# N(0,1^2) prior for item slopes
prior_slopes <- c(0,1)
# estimate model
mod11 <- CDM::gdina( data=dat, q.matrix=Q, rule="GDINA",
prior_intercepts=prior_intercepts, prior_slopes=prior_slopes)
summary(mod11)
#############################################################################
# EXAMPLE 2: Simulated DINO data
# additive cognitive diagnosis model with different link functions
#############################################################################
data(sim.dino, package="CDM")
data(sim.matrix, package="CDM")
dat <- sim.dino
Q <- sim.qmatrix
#***
# Model 1: additive cognitive diagnosis model (ACDM; identity link)
mod1 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM")
summary(mod1)
#***
# Model 2: ACDM logit link
mod2 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM", linkfct="logit")
summary(mod2)
#***
# Model 3: ACDM log link
mod3 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM", linkfct="log")
summary(mod3)
#***
# Model 4: Different condensation rules per item
I <- 9 # number of items
rule <- rep( "GDINA", I )
rule[1] <- "DINO" # 1st item: DINO model
rule[7] <- "GDINA2" # 7th item: GDINA model with first- and second-order interactions
rule[8] <- "ACDM" # 8ht item: additive CDM
rule[9] <- "DINA" # 9th item: DINA model
mod4 <- CDM::gdina( data=dat, q.matrix=Q, rule=rule )
summary(mod4)
#############################################################################
# EXAMPLE 3: Model with user-specified design matrices
#############################################################################
data(sim.dino, package="CDM")
data(sim.qmatrix, package="CDM")
dat <- sim.dino
Q <- sim.qmatrix
# do a preliminary analysis and modify obtained design matrices
mod0 <- CDM::gdina( data=dat, q.matrix=Q, maxit=1)
# extract default design matrices
Mj <- mod0$Mj
Mj.user <- Mj # these user defined design matrices are modified.
#~~~ For the second item, the following model should hold
# X1 ~ V2 + V2*V3
mj <- Mj[[2]][[1]]
mj.lab <- Mj[[2]][[2]]
mj <- mj[,-3]
mj.lab <- mj.lab[-3]
Mj.user[[2]] <- list( mj, mj.lab )
# [[1]]
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 1 1 0
# [3,] 1 0 0
# [4,] 1 1 1
# [[2]]
# [1] "0" "1" "1-2"
#~~~ For the eight item an equality constraint should hold
# X8 ~ a*V2 + a*V3 + V2*V3
mj <- Mj[[8]][[1]]
mj.lab <- Mj[[8]][[2]]
mj[,2] <- mj[,2] + mj[,3]
mj <- mj[,-3]
mj.lab <- c("0", "1=2", "1-2" )
Mj.user[[8]] <- list( mj, mj.lab )
Mj.user[[8]]
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 1 1 0
## [3,] 1 1 0
## [4,] 1 2 1
##
## [[2]]
## [1] "0" "1=2" "1-2"
mod <- CDM::gdina( data=dat, q.matrix=Q,
Mj=Mj.user, maxit=200 )
summary(mod)
#############################################################################
# EXAMPLE 4: Design matrix for delta parameters
#############################################################################
data(sim.dino, package="CDM")
data(sim.qmatrix, package="CDM")
#~~~ estimate an initial model
mod0 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM", maxit=1)
# extract coefficients
c0 <- mod0$coef
I <- 9 # number of items
delta.designmatrix <- matrix( 0, nrow=nrow(c0), ncol=nrow(c0) )
diag( delta.designmatrix) <- 1
# set intercept of item 1 and item 3 equal to each other
delta.designmatrix[ 7, 1 ] <- 1 ; delta.designmatrix[,7] <- 0
# set loading of V1 of item1 and item 3 equal
delta.designmatrix[ 8, 2 ] <- 1 ; delta.designmatrix[,8] <- 0
delta.designmatrix <- delta.designmatrix[, -c(7:8) ]
# exclude original parameters with indices 7 and 8
#***
# Model 1: ACDM with designmatrix
mod1 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM",
delta.designmatrix=delta.designmatrix )
summary(mod1)
#***
# Model 2: Same model, but with logit link instead of identity link function
mod2 <- CDM::gdina( data=dat, q.matrix=Q, rule="ACDM",
delta.designmatrix=delta.designmatrix, linkfct="logit")
summary(mod2)
#############################################################################
# EXAMPLE 5: Multiple group estimation
#############################################################################
# simulate data
set.seed(9279)
N1 <- 200 ; N2 <- 100 # group sizes
I <- 10 # number of items
q.matrix <- matrix(0,I,2) # create Q-matrix
q.matrix[1:7,1] <- 1 ; q.matrix[ 5:10,2] <- 1
# simulate first group
dat1 <- CDM::sim.din(N1, q.matrix=q.matrix, mean=c(0,0) )$dat
# simulate second group
dat2 <- CDM::sim.din(N2, q.matrix=q.matrix, mean=c(-.3, -.7) )$dat
# merge data
dat <- rbind( dat1, dat2 )
# group indicator
group <- c( rep(1,N1), rep(2,N2) )
# estimate GDINA model with multiple groups assuming invariant item parameters
mod1 <- CDM::gdina( data=dat, q.matrix=q.matrix, group=group)
summary(mod1)
# estimate DINA model with multiple groups assuming invariant item parameters
mod2 <- CDM::gdina( data=dat, q.matrix=q.matrix, group=group, rule="DINA")
summary(mod2)
# estimate GDINA model with noninvariant item parameters
mod3 <- CDM::gdina( data=dat, q.matrix=q.matrix, group=group, invariance=FALSE)
summary(mod3)
# estimate GDINA model with some invariant item parameters (I001, I006, I008)
mod4 <- CDM::gdina( data=dat, q.matrix=q.matrix, group=group,
invariance=c("I001", "I006","I008") )
#--- model comparison
IRT.compareModels(mod1,mod2,mod3,mod4)
# estimate GDINA model with non-invariant item parameters except for the
# items I001, I006, I008
mod5 <- CDM::gdina( data=dat, q.matrix=q.matrix, group=group,
invariance=setdiff( colnames(dat), c("I001", "I006","I008") ) )
#############################################################################
# EXAMPLE 6: User specified reduced skill space
#############################################################################
# Some correlations between attributes should be set to zero.
q.matrix <- expand.grid( c(0,1), c(0,1), c(0,1), c(0,1) )
colnames(q.matrix) <- colnames( paste("Attr", 1:4,sep=""))
q.matrix <- q.matrix[ -1, ]
Sigma <- matrix( .5, nrow=4, ncol=4 )
diag(Sigma) <- 1
Sigma[3,2] <- Sigma[2,3] <- 0 # set correlation of attribute A2 and A3 to zero
dat <- CDM::sim.din( N=1000, q.matrix=q.matrix, Sigma=Sigma)$dat
#~~~ Step 1: initial estimation
mod1a <- CDM::gdina( data=dat, q.matrix=q.matrix, maxit=1, rule="DINA")
# estimate also "full" model
mod1 <- CDM::gdina( data=dat, q.matrix=q.matrix, rule="DINA")
#~~~ Step 2: modify designmatrix for reduced skillspace
Z.skillspace <- data.frame( mod1a$Z.skillspace )
# set correlations of A2/A4 and A3/A4 to zero
vars <- c("A2_A3","A2_A4")
for (vv in vars){ Z.skillspace[,vv] <- NULL }
#~~~ Step 3: estimate model with reduced skillspace
mod2 <- CDM::gdina( data=dat, q.matrix=q.matrix,
Z.skillspace=Z.skillspace, rule="DINA")
#~~~ eliminate all covariances
Z.skillspace <- data.frame( mod1$Z.skillspace )
colnames(Z.skillspace)
Z.skillspace <- Z.skillspace[, -grep( "_", colnames(Z.skillspace),fixed=TRUE)]
colnames(Z.skillspace)
mod3 <- CDM::gdina( data=dat, q.matrix=q.matrix,
Z.skillspace=Z.skillspace, rule="DINA")
summary(mod1)
summary(mod2)
summary(mod3)
#############################################################################
# EXAMPLE 7: Polytomous GDINA model (Chen & de la Torre, 2013)
#############################################################################
data(data.pgdina, package="CDM")
dat <- data.pgdina$dat
q.matrix <- data.pgdina$q.matrix
# pGDINA model with "DINA rule"
mod1 <- CDM::gdina( dat, q.matrix=q.matrix, rule="DINA")
summary(mod1)
# no reduced skill space
mod1a <- CDM::gdina( dat, q.matrix=q.matrix, rule="DINA",reduced.skillspace=FALSE)
summary(mod1)
# pGDINA model with "GDINA rule"
mod2 <- CDM::gdina( dat, q.matrix=q.matrix, rule="GDINA")
summary(mod2)
#############################################################################
# EXAMPLE 8: Fraction subtraction data: DINA and HO-DINA model
#############################################################################
data(fraction.subtraction.data, package="CDM")
data(fraction.subtraction.qmatrix, package="CDM")
dat <- fraction.subtraction.data
Q <- fraction.subtraction.qmatrix
# Model 1: DINA model
mod1 <- CDM::gdina( dat, q.matrix=Q, rule="DINA")
summary(mod1)
# Model 2: HO-DINA model
mod2 <- CDM::gdina( dat, q.matrix=Q, HOGDINA=1, rule="DINA")
summary(mod2)
#############################################################################
# EXAMPLE 9: Skill space approximation data.jang
#############################################################################
data(data.jang, package="CDM")
data <- data.jang$data
q.matrix <- data.jang$q.matrix
#*** Model 1: Reduced RUM model
mod1 <- CDM::gdina( data, q.matrix, rule="RRUM", conv.crit=.001, maxit=500 )
#*** Model 2: Reduced RUM model with skill space approximation
# use 300 instead of 2^9=512 skill classes
skillspace <- CDM::skillspace.approximation( L=300, K=ncol(q.matrix) )
mod2 <- CDM::gdina( data, q.matrix, rule="RRUM", conv.crit=.001,
skillclasses=skillspace )
## > logLik(mod1)
## 'log Lik.' -30318.08 (df=153)
## > logLik(mod2)
## 'log Lik.' -30326.52 (df=153)
#############################################################################
# EXAMPLE 10: CDM with a linear hierarchy
#############################################################################
# This model is equivalent to a unidimensional IRT model with an ordered
# ordinal latent trait and is actually a probabilistic Guttman model.
set.seed(789)
# define 3 competency levels
alpha <- scan()
0 0 0 1 0 0 1 1 0 1 1 1
# define skill class distribution
K <- 3
skillspace <- alpha <- matrix( alpha, K + 1, K, byrow=TRUE )
alpha <- alpha[ rep( 1:4, c(300,300,200,200) ), ]
# P(000)=P(100)=.3, P(110)=P(111)=.2
# define Q-matrix
Q <- scan()
1 0 0 1 1 0 1 1 1
Q <- matrix( Q, nrow=K, ncol=K, byrow=TRUE )
Q <- Q[ rep(1:K, each=4 ), ]
colnames(skillspace) <- colnames(Q) <- paste0("A",1:K)
I <- nrow(Q)
# define guessing and slipping parameters
guess <- stats::runif( I, 0, .3 )
slip <- stats::runif( I, 0, .2 )
# simulate data
dat <- CDM::sim.din( q.matrix=Q, alpha=alpha, slip=slip, guess=guess )$dat
#*** Model 1: DINA model with linear hierarchy
mod1 <- CDM::din( dat, q.matrix=Q, rule="DINA", skillclasses=skillspace )
summary(mod1)
#*** Model 2: pGDINA model with 3 levels
# The multidimensional CDM with a linear hierarchy is a unidimensional
# polytomous GDINA model.
Q2 <- matrix( rowSums(Q), nrow=I, ncol=1 )
mod2 <- CDM::gdina( dat, q.matrix=Q2, rule="DINA" )
summary(mod2)
#*** Model 3: estimate probabilistic Guttman model in sirt
# Proctor, C. H. (1970). A probabilistic formulation and statistical
# analysis for Guttman scaling. Psychometrika, 35, 73-78.
library(sirt)
mod3 <- sirt::prob.guttman( dat, itemlevel=Q2[,1] )
summary(mod3)
# -> The three models result in nearly equivalent fit.
#############################################################################
# EXAMPLE 11: Sequential GDINA model (Ma & de la Torre, 2016)
#############################################################################
data(data.cdm04, package="CDM")
#** attach dataset
dat <- data.cdm04$data # polytomous item responses
q.matrix1 <- data.cdm04$q.matrix1
q.matrix2 <- data.cdm04$q.matrix2
#-- DINA model with first Q-matrix
mod1 <- CDM::gdina( dat, q.matrix=q.matrix1, rule="DINA")
summary(mod1)
#-- DINA model with second Q-matrix
mod2 <- CDM::gdina( dat, q.matrix=q.matrix2, rule="DINA")
#-- GDINA model
mod3 <- CDM::gdina( dat, q.matrix=q.matrix2, rule="GDINA")
#** model comparison
IRT.compareModels(mod1,mod2,mod3)
#############################################################################
# EXAMPLE 12: Simulataneous modeling of skills and misconceptions (Kuo et al., 2018)
#############################################################################
data(data.cdm08, package="CDM")
dat <- data.cdm08$data
q.matrix <- data.cdm08$q.matrix
#*** estimate model
mod <- CDM::gdina( dat0, q.matrix, rule="SISM", bugs=colnames(q.matrix)[5:7] )
summary(mod)
#############################################################################
# EXAMPLE 13: Regularized estimation in GDINA model data.dtmr
#############################################################################
data(data.dtmr, package="CDM")
dat <- data.dtmr$data
q.matrix <- data.dtmr$q.matrix
#***** LASSO regularization with lambda parameter of .02
mod1 <- CDM::gdina(dat, q.matrix=q.matrix, rule="GDINA", regular_lam=.02,
regular_type="lasso")
summary(mod1)
mod$delta_regularized
#***** using starting values from previuos estimation
delta.init <- mod1$delta
attr.prob.init <- mod1$attr.prob
mod2 <- CDM::gdina(dat, q.matrix=q.matrix, rule="GDINA", regular_lam=.02, regular_type="lasso",
delta.init=delta.init, attr.prob.init=attr.prob.init)
summary(mod2)
#***** final estimation fixing regularized estimates to zero and estimate all other
#***** item parameters unregularized
regular_weights <- mod2$delta_regularized
delta.init <- mod2$delta
attr.prob.init <- mod2$attr.prob
mod3 <- CDM::gdina(dat, q.matrix=q.matrix, rule="GDINA", regular_lam=1E5, regular_type="lasso",
delta.init=delta.init, attr.prob.init=attr.prob.init,
regular_weights=regular_weights)
summary(mod3)
# }
Run the code above in your browser using DataLab