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 
#>  57  63  85 111  34
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.1710 0.1295
#>  0.1940 0.2118
#>  0.1477 0.1207
#>  0.1569 0.2338
#>  0.1490 0.2157
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0     -2.2729
#> λ1      0.1969
#> λ2      0.1822
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1194
#> 0001  0.1940
#> 0010  0.1550
#> 0011  0.2384
#> 0100  0.1760
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 154335 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5276
#> M2:  0.49
#> total scores:  0.6244
a <- summary(output_HMDCM_RT_joint)
a
#> 
#> Model: DINA_HO_RT_joint 
#> 
#> Item Parameters:
#>  ss_EAP gs_EAP
#>  0.1710 0.1295
#>  0.1940 0.2118
#>  0.1477 0.1207
#>  0.1569 0.2338
#>  0.1490 0.2157
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0     -2.2729
#> λ1      0.1969
#> λ2      0.1822
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1194
#> 0001  0.1940
#> 0010  0.1550
#> 0011  0.2384
#> 0100  0.1760
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 154335 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5224
#> M2:  0.49
#> total scores:  0.6256

a$ss_EAP
#>             [,1]
#>  [1,] 0.17102885
#>  [2,] 0.19404185
#>  [3,] 0.14766003
#>  [4,] 0.15685417
#>  [5,] 0.14896812
#>  [6,] 0.12506639
#>  [7,] 0.14593981
#>  [8,] 0.17621620
#>  [9,] 0.17066474
#> [10,] 0.10069325
#> [11,] 0.12116787
#> [12,] 0.27147849
#> [13,] 0.21801974
#> [14,] 0.12486518
#> [15,] 0.14086055
#> [16,] 0.26139691
#> [17,] 0.13087620
#> [18,] 0.18395990
#> [19,] 0.14609280
#> [20,] 0.15641872
#> [21,] 0.18588286
#> [22,] 0.16122183
#> [23,] 0.14168650
#> [24,] 0.14484889
#> [25,] 0.16200293
#> [26,] 0.13470972
#> [27,] 0.17277175
#> [28,] 0.19861351
#> [29,] 0.16521466
#> [30,] 0.34259750
#> [31,] 0.17234675
#> [32,] 0.17790487
#> [33,] 0.07655841
#> [34,] 0.18643061
#> [35,] 0.24700725
#> [36,] 0.18402143
#> [37,] 0.23905749
#> [38,] 0.20459515
#> [39,] 0.22533556
#> [40,] 0.19453570
#> [41,] 0.20255444
#> [42,] 0.23265717
#> [43,] 0.19411872
#> [44,] 0.20407748
#> [45,] 0.23176666
#> [46,] 0.15967514
#> [47,] 0.20101965
#> [48,] 0.13378019
#> [49,] 0.18100237
#> [50,] 0.21979400
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.1710289
#> [2,] 0.1940419
#> [3,] 0.1476600
#> [4,] 0.1568542
#> [5,] 0.1489681
#> [6,] 0.1250664

(3) Check for parameter estimation accuracy

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

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.9171429 0.9178571 0.9457143 0.9500000 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.7114286 0.7114286 0.8085714 0.8285714 0.8114286

(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          1959.256      132647.9  15069.7 3491.572 153168.4
#> D(theta_bar)   1675.961      132215.4  14750.7 3359.738 152001.8
#> DIC            2242.550      133080.3  15388.7 3623.407 154335.0
head(a$PPP_total_scores)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.90 0.38 0.04 0.00 0.12
#> [2,] 0.64 1.00 0.04 0.78 0.16
#> [3,] 0.96 0.36 0.76 0.18 0.32
#> [4,] 0.60 0.26 1.00 0.92 0.98
#> [5,] 0.56 0.56 0.64 0.12 0.60
#> [6,] 0.48 0.82 0.02 0.16 0.18
head(a$PPP_total_RTs)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.62 0.28 0.20 0.14 0.82
#> [2,] 0.54 0.88 0.20 0.70 0.54
#> [3,] 0.64 0.40 0.58 0.76 0.44
#> [4,] 0.80 0.12 0.54 0.90 0.22
#> [5,] 0.72 0.06 0.40 0.54 0.68
#> [6,] 0.40 0.86 0.44 0.68 0.86
head(a$PPP_item_means)
#> [1] 0.48 0.50 0.44 0.52 0.60 0.50
head(a$PPP_item_mean_RTs)
#> [1] 0.46 0.50 0.44 0.62 0.50 0.58
head(a$PPP_item_ORs)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   NA 0.66 0.82 0.54 0.50 0.78 0.52 0.46 0.36  0.70  0.38  0.34  0.86  0.26
#> [2,]   NA   NA 0.78 0.38 0.82 0.62 0.46 0.72 0.56  0.72  0.36  0.00  0.68  0.00
#> [3,]   NA   NA   NA 0.84 0.82 0.90 0.76 0.92 0.42  0.98  0.92  0.38  0.58  0.44
#> [4,]   NA   NA   NA   NA 1.00 0.54 0.98 0.52 0.98  0.82  0.74  0.18  0.96  0.48
#> [5,]   NA   NA   NA   NA   NA 0.88 0.54 0.50 0.64  0.94  0.64  0.26  0.22  0.70
#> [6,]   NA   NA   NA   NA   NA   NA 0.38 0.84 0.80  0.28  0.28  0.06  0.98  0.62
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,]  0.46  0.76  0.86  0.98  0.12  0.44  0.58  0.38  0.34  0.44  0.30  0.10
#> [2,]  0.08  0.52  0.70  0.88  0.90  0.08  0.80  0.94  0.72  0.62  0.46  0.78
#> [3,]  0.66  0.78  0.58  0.54  0.28  0.50  0.08  0.28  0.74  0.18  0.12  0.26
#> [4,]  0.76  0.98  0.86  0.80  0.62  0.92  0.32  0.76  0.84  0.26  0.72  0.52
#> [5,]  0.20  0.66  0.18  0.90  0.22  0.06  0.62  0.78  0.94  0.14  0.76  0.38
#> [6,]  0.68  0.38  0.52  0.50  0.52  0.46  0.98  1.00  0.86  0.62  0.86  0.18
#>      [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#> [1,]  0.52  0.16  0.44  0.86  0.86  0.66  0.40  0.78  0.72  0.56  0.70  0.82
#> [2,]  0.74  0.60  0.88  0.46  0.50  0.88  0.96  0.80  0.48  0.96  0.20  0.62
#> [3,]  0.58  0.00  0.72  0.18  0.40  0.90  0.76  0.92  0.88  0.60  0.78  0.56
#> [4,]  0.74  0.64  0.38  0.62  0.68  0.38  0.68  0.50  0.92  0.30  0.82  0.04
#> [5,]  0.20  0.12  0.54  0.48  0.78  0.56  0.38  0.70  0.62  0.02  0.06  0.36
#> [6,]  0.92  0.42  0.56  0.66  0.94  0.62  0.76  0.94  0.84  0.80  0.64  0.82
#>      [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#> [1,]  0.76  0.94  0.90  0.84  0.92  0.64  0.68  0.76  0.66  0.98  1.00  0.94
#> [2,]  0.68  0.68  0.20  0.24  0.48  0.92  0.60  0.34  0.72  0.98  0.78  0.42
#> [3,]  0.70  0.68  0.94  0.54  0.34  0.94  0.96  0.30  0.46  0.84  0.88  0.34
#> [4,]  0.74  0.88  0.70  0.42  0.40  0.56  0.68  0.98  0.90  0.28  0.50  0.88
#> [5,]  0.98  0.46  0.40  0.90  0.34  0.24  0.60  0.10  0.54  0.12  0.58  0.62
#> [6,]  0.82  1.00  0.38  0.74  0.86  0.70  0.68  0.72  0.40  0.94  0.74  0.74