NIDA_indept

library(hmcdm)

Load the spatial rotation data

N = dim(Design_array)[1]
J = nrow(Q_matrix)
K = ncol(Q_matrix)
L = dim(Design_array)[3]

(1) Simulate responses and response times based on the NIDA model

tau <- numeric(K)
for(k in 1:K){
  tau[k] <- runif(1,.2,.6)
}
R = matrix(0,K,K)
# Initial alphas
p_mastery <- c(.5,.5,.4,.4)
Alphas_0 <- matrix(0,N,K)
for(i in 1:N){
  for(k in 1:K){
    prereqs <- which(R[k,]==1)
    if(length(prereqs)==0){
      Alphas_0[i,k] <- rbinom(1,1,p_mastery[k])
    }
    if(length(prereqs)>0){
      Alphas_0[i,k] <- prod(Alphas_0[i,prereqs])*rbinom(1,1,p_mastery)
    }
  }
}
Alphas <- sim_alphas(model="indept",taus=tau,N=N,L=L,R=R,alpha0=Alphas_0)
table(rowSums(Alphas[,,5]) - rowSums(Alphas[,,1])) # used to see how much transition has taken place
#> 
#>   0   1   2   3   4 
#>  35  86 129  78  22
Svec <- runif(K,.1,.3)
Gvec <- runif(K,.1,.3)

Y_sim <- sim_hmcdm(model="NIDA",Alphas,Q_matrix,Design_array,
                   Svec=Svec,Gvec=Gvec)

(2) Run the MCMC to sample parameters from the posterior distribution

output_NIDA_indept = hmcdm(Y_sim, Q_matrix, "NIDA_indept", Design_array,
                           100, 30, R = R)
#> 0
output_NIDA_indept
#> 
#> Model: NIDA_indept 
#> 
#> Sample Size: 350
#> Number of Items: 
#> Number of Time Points: 
#> 
#> Chain Length: 100, burn-in: 50
summary(output_NIDA_indept)
#> 
#> Model: NIDA_indept 
#> 
#> Item Parameters:
#>  ss_EAP gs_EAP
#>  0.2095 0.2363
#>  0.2610 0.2356
#>  0.2199 0.1682
#>  0.2255 0.2412
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.3271
#> τ2   0.4871
#> τ3   0.3235
#> τ4   0.6188
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.10210
#> 0001 0.06989
#> 0010 0.01778
#> 0011 0.06670
#> 0100 0.05699
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 23758.62 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4964
#> M2:  0.49
#> total scores:  0.6031
a <- summary(output_NIDA_indept)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.2095002
#> [2,] 0.2610238
#> [3,] 0.2199261
#> [4,] 0.2254784

(3) Check for parameter estimation accuracy

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8114286 0.8864286 0.9214286 0.9450000 0.9535714

PAR_vec <- numeric(L)
for(t in 1:L){
  PAR_vec[t] <- mean(rowSums((Alphas[,,t]-a$Alphas_est[,,t])^2)==0)
}
PAR_vec
#> [1] 0.4600000 0.6200000 0.7314286 0.7971429 0.8171429

(4) Evaluate the fit of the model to the observed response

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2077.876            NA 19109.77 1821.468 23009.11
#> D(theta_bar)   1966.934            NA 18511.68 1780.988 22259.61
#> DIC            2188.817            NA 19707.85 1861.948 23758.62
head(a$PPP_total_scores)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.00 0.26 0.84 0.08 0.06
#> [2,] 0.34 0.84 0.74 0.10 1.00
#> [3,] 0.72 0.64 0.48 0.04 0.88
#> [4,] 0.90 0.30 0.54 0.66 0.90
#> [5,] 0.18 0.70 0.34 0.30 0.80
#> [6,] 0.44 0.14 0.44 0.74 0.72
head(a$PPP_item_means)
#> [1] 0.24 0.20 0.24 0.18 0.50 0.32
head(a$PPP_item_ORs)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   NA 0.54 0.24 0.72 0.36 0.52 0.68 0.48 0.84  0.40  0.94  0.80  0.60  0.26
#> [2,]   NA   NA 0.52 0.74 0.34 0.44 0.06 0.62 0.94  0.50  0.06  0.18  0.08  0.10
#> [3,]   NA   NA   NA 0.96 0.96 0.32 0.76 0.18 0.58  0.54  0.10  0.58  0.04  0.22
#> [4,]   NA   NA   NA   NA 0.74 0.28 0.02 0.08 0.32  0.42  0.28  0.84  0.24  0.16
#> [5,]   NA   NA   NA   NA   NA 0.38 0.24 0.46 0.12  0.18  0.02  0.00  0.00  0.36
#> [6,]   NA   NA   NA   NA   NA   NA 0.80 0.28 0.52  0.82  0.80  0.46  0.50  0.70
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,]  0.92  0.98  0.64  0.94  0.74  0.78  0.08  0.72  0.74  0.56  0.84  0.74
#> [2,]  0.44  0.60  0.74  0.20  0.22  0.54  0.32  0.78  0.68  0.58  0.80  0.42
#> [3,]  0.04  0.10  0.58  0.42  0.76  0.30  0.44  0.42  0.36  0.06  0.32  0.66
#> [4,]  0.02  0.30  0.82  0.34  0.38  0.52  0.46  1.00  0.28  0.08  0.24  0.22
#> [5,]  0.28  0.40  0.34  0.00  0.42  0.92  0.38  0.40  0.88  0.42  0.22  0.82
#> [6,]  0.68  0.14  0.42  0.92  0.54  0.08  0.74  0.46  0.86  0.74  0.42  0.10
#>      [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#> [1,]  0.54  0.16  0.74  0.02  0.76  0.64  1.00  0.98  0.76  0.96  0.36  0.44
#> [2,]  0.68  0.16  0.46  0.70  0.68  0.14  0.28  0.64  0.12  0.12  0.24  0.06
#> [3,]  0.66  0.12  1.00  0.76  0.82  0.06  0.02  0.48  0.62  0.06  0.32  0.72
#> [4,]  0.98  0.14  1.00  0.18  0.70  0.20  0.34  0.46  0.36  0.94  0.22  0.58
#> [5,]  0.92  0.02  0.46  0.48  0.36  0.02  0.60  0.50  0.42  1.00  0.82  0.58
#> [6,]  0.86  0.72  0.58  0.32  0.58  0.08  0.82  0.82  0.64  0.22  0.52  0.54
#>      [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#> [1,]  0.58  0.64  0.70  0.94  0.26  0.00  0.22  0.70  0.98  0.48  0.98  0.74
#> [2,]  0.16  0.80  0.32  0.04  0.32  0.50  0.08  0.76  0.82  0.54  0.22  0.40
#> [3,]  0.30  0.80  0.00  0.52  0.04  0.80  0.62  0.16  0.46  0.54  0.44  0.94
#> [4,]  0.46  0.16  0.30  0.82  0.10  0.78  0.40  0.30  0.32  0.02  0.10  0.44
#> [5,]  0.18  0.38  0.74  0.22  0.50  0.80  0.22  0.46  0.14  0.04  0.80  1.00
#> [6,]  0.16  0.18  0.94  0.96  0.50  0.16  0.90  0.70  0.58  0.34  0.90  0.62