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 
#>  26 119 124  69  12
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.1544 0.2124
#>  0.1527 0.2716
#>  0.2336 0.2351
#>  0.1842 0.1802
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.4969
#> τ2   0.3157
#> τ3   0.2717
#> τ4   0.4502
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.06531
#> 0001 0.02429
#> 0010 0.13878
#> 0011 0.02071
#> 0100 0.08161
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22472.21 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4908
#> M2:  0.49
#> total scores:  0.6054
a <- summary(output_NIDA_indept)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.1543751
#> [2,] 0.1527295
#> [3,] 0.2335961
#> [4,] 0.1841662

(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.8678571 0.8914286 0.9400000 0.9614286 0.9728571

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.5742857 0.6342857 0.7914286 0.8628571 0.8942857

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

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