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 
#>  30 125 113  72  10
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.2668 0.1996
#>  0.2557 0.2735
#>  0.1736 0.2767
#>  0.2789 0.1968
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.2643
#> τ2   0.5262
#> τ3   0.4935
#> τ4   0.2852
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.09039
#> 0001 0.05052
#> 0010 0.03148
#> 0011 0.07752
#> 0100 0.07745
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 24138.91 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4888
#> M2:  0.49
#> total scores:  0.5993
a <- summary(output_NIDA_indept)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.2667624
#> [2,] 0.2557295
#> [3,] 0.1736389
#> [4,] 0.2789057

(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.8121429 0.8557143 0.9142857 0.9292857 0.9457143

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.4571429 0.5400000 0.7028571 0.7485714 0.8057143

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

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