DINA_HO_RT_joint

library(hmcdm)

Load the spatial rotation data

N = length(Test_versions)
J = nrow(Q_matrix)
K = ncol(Q_matrix)
L = nrow(Test_order)

(1) Simulate responses and response times based on the HMDCM model with response times (no covariance between speed and learning ability)

ETAs <- ETAmat(K, J, Q_matrix)
class_0 <- sample(1:2^K, N, replace = L)
Alphas_0 <- matrix(0,N,K)
mu_thetatau = c(0,0)
Sig_thetatau = rbind(c(1.8^2,.4*.5*1.8),c(.4*.5*1.8,.25))
Z = matrix(rnorm(N*2),N,2)
thetatau_true = Z%*%chol(Sig_thetatau)
thetas_true = thetatau_true[,1]
taus_true = thetatau_true[,2]
G_version = 3
phi_true = 0.8
for(i in 1:N){
  Alphas_0[i,] <- inv_bijectionvector(K,(class_0[i]-1))
}
lambdas_true <- c(-2, .4, .055)       # empirical from Wang 2017
Alphas <- sim_alphas(model="HO_joint", 
                    lambdas=lambdas_true, 
                    thetas=thetas_true, 
                    Q_matrix=Q_matrix, 
                    Design_array=Design_array)
table(rowSums(Alphas[,,5]) - rowSums(Alphas[,,1])) # used to see how much transition has taken place
#> 
#>   0   1   2   3   4 
#>  64  57  78 120  31
itempars_true <- matrix(runif(J*2,.1,.2), ncol=2)
RT_itempars_true <- matrix(NA, nrow=J, ncol=2)
RT_itempars_true[,2] <- rnorm(J,3.45,.5)
RT_itempars_true[,1] <- runif(J,1.5,2)

Y_sim <- sim_hmcdm(model="DINA",Alphas,Q_matrix,Design_array,
                   itempars=itempars_true)
L_sim <- sim_RT(Alphas,Q_matrix,Design_array,
                  RT_itempars_true,taus_true,phi_true,G_version)

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

output_HMDCM_RT_joint = hmcdm(Y_sim,Q_matrix,"DINA_HO_RT_joint",Design_array,100,30,
                                 Latency_array = L_sim, G_version = G_version,
                                 theta_propose = 2,deltas_propose = c(.45,.25,.06))
#> 0
output_HMDCM_RT_joint
#> 
#> Model: DINA_HO_RT_joint 
#> 
#> Sample Size: 350
#> Number of Items: 
#> Number of Time Points: 
#> 
#> Chain Length: 100, burn-in: 50
summary(output_HMDCM_RT_joint)
#> 
#> Model: DINA_HO_RT_joint 
#> 
#> Item Parameters:
#>  ss_EAP  gs_EAP
#>  0.1673 0.09386
#>  0.2128 0.18290
#>  0.1111 0.10748
#>  0.1525 0.10612
#>  0.1326 0.20154
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -1.87422
#> λ1     0.18022
#> λ2     0.09529
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1597
#> 0001  0.1758
#> 0010  0.1989
#> 0011  0.2119
#> 0100  0.1795
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 154096.9 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5204
#> M2:  0.49
#> total scores:  0.6245
a <- summary(output_HMDCM_RT_joint)
a
#> 
#> Model: DINA_HO_RT_joint 
#> 
#> Item Parameters:
#>  ss_EAP  gs_EAP
#>  0.1673 0.09386
#>  0.2128 0.18290
#>  0.1111 0.10748
#>  0.1525 0.10612
#>  0.1326 0.20154
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -1.87422
#> λ1     0.18022
#> λ2     0.09529
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1597
#> 0001  0.1758
#> 0010  0.1989
#> 0011  0.2119
#> 0100  0.1795
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 154096.9 
#> 
#> Posterior Predictive P-value (PPP):
#> M1:  0.52
#> M2:  0.49
#> total scores:  0.6288

a$ss_EAP
#>             [,1]
#>  [1,] 0.16732267
#>  [2,] 0.21280151
#>  [3,] 0.11106709
#>  [4,] 0.15254399
#>  [5,] 0.13257797
#>  [6,] 0.13627085
#>  [7,] 0.19870128
#>  [8,] 0.15621726
#>  [9,] 0.17897035
#> [10,] 0.17210040
#> [11,] 0.15469951
#> [12,] 0.18412440
#> [13,] 0.21246399
#> [14,] 0.11063679
#> [15,] 0.23078036
#> [16,] 0.14171145
#> [17,] 0.14775895
#> [18,] 0.10901296
#> [19,] 0.17306186
#> [20,] 0.16469536
#> [21,] 0.19246733
#> [22,] 0.17671904
#> [23,] 0.18656314
#> [24,] 0.16892639
#> [25,] 0.13957136
#> [26,] 0.13376516
#> [27,] 0.26220253
#> [28,] 0.18748245
#> [29,] 0.18157060
#> [30,] 0.09170670
#> [31,] 0.18621950
#> [32,] 0.14279553
#> [33,] 0.16948927
#> [34,] 0.10053940
#> [35,] 0.09706387
#> [36,] 0.09500953
#> [37,] 0.23002014
#> [38,] 0.20243444
#> [39,] 0.14741026
#> [40,] 0.17389245
#> [41,] 0.20792716
#> [42,] 0.21651258
#> [43,] 0.12087518
#> [44,] 0.12486395
#> [45,] 0.14737296
#> [46,] 0.13874216
#> [47,] 0.13870303
#> [48,] 0.27791396
#> [49,] 0.13286808
#> [50,] 0.16928385
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.1673227
#> [2,] 0.2128015
#> [3,] 0.1110671
#> [4,] 0.1525440
#> [5,] 0.1325780
#> [6,] 0.1362708

(3) Check for parameter estimation accuracy

(cor_thetas <- cor(thetas_true,a$thetas_EAP))
#>           [,1]
#> [1,] 0.8112809
(cor_taus <- cor(taus_true,a$response_times_coefficients$taus_EAP))
#>           [,1]
#> [1,] 0.9873944

(cor_ss <- cor(as.vector(itempars_true[,1]),a$ss_EAP))
#>           [,1]
#> [1,] 0.6046299
(cor_gs <- cor(as.vector(itempars_true[,2]),a$gs_EAP))
#>           [,1]
#> [1,] 0.6419391

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.9314286 0.9535714 0.9478571 0.9607143 0.9585714

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.7657143 0.8314286 0.8142857 0.8514286 0.8542857

(4) Evaluate the fit of the model to the observed response and response times data (here, Y_sim and R_sim)

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