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 
#>  43 152 106  40   9
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.2172 0.2624
#>  0.2349 0.1939
#>  0.1333 0.2460
#>  0.1685 0.2022
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.2327
#> τ2   0.2136
#> τ3   0.2210
#> τ4   0.5510
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.04930
#> 0001 0.07797
#> 0010 0.07891
#> 0011 0.04798
#> 0100 0.06124
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22455.11 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5072
#> M2:  0.49
#> total scores:  0.6069
a <- summary(output_NIDA_indept)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.2172293
#> [2,] 0.2349224
#> [3,] 0.1332734
#> [4,] 0.1685322

(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.8778571 0.9064286 0.9171429 0.9371429 0.9414286

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.5828571 0.6800000 0.7171429 0.7685714 0.7885714

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

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