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 
#>  74  60  71 117  28
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.20705 0.1283
#>  0.15767 0.1357
#>  0.09078 0.1521
#>  0.12670 0.1003
#>  0.15192 0.2148
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -1.88583
#> λ1     0.30564
#> λ2     0.09087
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1842
#> 0001  0.1782
#> 0010  0.2406
#> 0011  0.1770
#> 0100  0.1457
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 158462 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5112
#> M2:  0.49
#> total scores:  0.6264
a <- summary(output_HMDCM_RT_joint)
a
#> 
#> Model: DINA_HO_RT_joint 
#> 
#> Item Parameters:
#>   ss_EAP gs_EAP
#>  0.20705 0.1283
#>  0.15767 0.1357
#>  0.09078 0.1521
#>  0.12670 0.1003
#>  0.15192 0.2148
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -1.88583
#> λ1     0.30564
#> λ2     0.09087
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1842
#> 0001  0.1782
#> 0010  0.2406
#> 0011  0.1770
#> 0100  0.1457
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 158462 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5448
#> M2:  0.49
#> total scores:  0.6316

a$ss_EAP
#>             [,1]
#>  [1,] 0.20704767
#>  [2,] 0.15766942
#>  [3,] 0.09077809
#>  [4,] 0.12670256
#>  [5,] 0.15191635
#>  [6,] 0.24279745
#>  [7,] 0.08624698
#>  [8,] 0.15108104
#>  [9,] 0.10719064
#> [10,] 0.16058157
#> [11,] 0.13916831
#> [12,] 0.15008475
#> [13,] 0.10167298
#> [14,] 0.11434314
#> [15,] 0.24206183
#> [16,] 0.20632409
#> [17,] 0.17296677
#> [18,] 0.21395660
#> [19,] 0.20324883
#> [20,] 0.18729603
#> [21,] 0.14448680
#> [22,] 0.18378790
#> [23,] 0.18033803
#> [24,] 0.21707827
#> [25,] 0.16837838
#> [26,] 0.26575317
#> [27,] 0.15191721
#> [28,] 0.15897600
#> [29,] 0.11326398
#> [30,] 0.10849611
#> [31,] 0.13542571
#> [32,] 0.23021428
#> [33,] 0.15838810
#> [34,] 0.11475781
#> [35,] 0.08184248
#> [36,] 0.18200751
#> [37,] 0.15616078
#> [38,] 0.12858040
#> [39,] 0.12446524
#> [40,] 0.13009291
#> [41,] 0.16009386
#> [42,] 0.18827360
#> [43,] 0.20704827
#> [44,] 0.14585591
#> [45,] 0.13300932
#> [46,] 0.11498016
#> [47,] 0.11368938
#> [48,] 0.13960418
#> [49,] 0.09386531
#> [50,] 0.17776224
head(a$ss_EAP)
#>            [,1]
#> [1,] 0.20704767
#> [2,] 0.15766942
#> [3,] 0.09077809
#> [4,] 0.12670256
#> [5,] 0.15191635
#> [6,] 0.24279745

(3) Check for parameter estimation accuracy

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

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.9428571 0.9478571 0.9650000 0.9650000 0.9592857

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.8028571 0.8171429 0.8771429 0.8685714 0.8485714

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