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 
#>  24  81 125  95  25
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.1472 0.2752
#>  0.2258 0.1851
#>  0.2164 0.2128
#>  0.2548 0.1089
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.3817
#> τ2   0.5698
#> τ3   0.5855
#> τ4   0.4749
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.07686
#> 0001 0.05897
#> 0010 0.06333
#> 0011 0.04266
#> 0100 0.04656
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 23119.27 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5052
#> M2:  0.49
#> total scores:  0.604
a <- summary(output_NIDA_indept)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.1472050
#> [2,] 0.2257969
#> [3,] 0.2163931
#> [4,] 0.2547650

(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.8385714 0.8900000 0.9385714 0.9650000 0.9735714

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.5057143 0.6342857 0.7771429 0.8714286 0.9057143

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

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