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 
#>  60  60  92 110  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.2492 0.09473
#>  0.2325 0.20883
#>  0.2285 0.26959
#>  0.1281 0.10423
#>  0.3080 0.19377
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0     -3.7153
#> λ1      0.1503
#> λ2      0.3511
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.07477
#> 0001 0.15357
#> 0010 0.20589
#> 0011 0.23575
#> 0100 0.11508
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 151624.2 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5284
#> M2:  0.49
#> total scores:  0.6191
a <- summary(output_HMDCM_RT_joint)
a
#> 
#> Model: DINA_HO_RT_joint 
#> 
#> Item Parameters:
#>  ss_EAP  gs_EAP
#>  0.2492 0.09473
#>  0.2325 0.20883
#>  0.2285 0.26959
#>  0.1281 0.10423
#>  0.3080 0.19377
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0     -3.7153
#> λ1      0.1503
#> λ2      0.3511
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.07477
#> 0001 0.15357
#> 0010 0.20589
#> 0011 0.23575
#> 0100 0.11508
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 151624.2 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5152
#> M2:  0.49
#> total scores:  0.6175

a$ss_EAP
#>            [,1]
#>  [1,] 0.2492219
#>  [2,] 0.2324782
#>  [3,] 0.2285012
#>  [4,] 0.1280917
#>  [5,] 0.3080421
#>  [6,] 0.1850605
#>  [7,] 0.1843402
#>  [8,] 0.2451709
#>  [9,] 0.2269631
#> [10,] 0.1935830
#> [11,] 0.1619193
#> [12,] 0.1484891
#> [13,] 0.1998678
#> [14,] 0.2609338
#> [15,] 0.1961030
#> [16,] 0.2265470
#> [17,] 0.1318782
#> [18,] 0.1793646
#> [19,] 0.2046436
#> [20,] 0.1615866
#> [21,] 0.1816740
#> [22,] 0.1752282
#> [23,] 0.1877372
#> [24,] 0.2874214
#> [25,] 0.2189818
#> [26,] 0.2706002
#> [27,] 0.2693866
#> [28,] 0.2759914
#> [29,] 0.2859888
#> [30,] 0.1688346
#> [31,] 0.2132637
#> [32,] 0.1864005
#> [33,] 0.1968759
#> [34,] 0.1818932
#> [35,] 0.2205629
#> [36,] 0.2792817
#> [37,] 0.2163312
#> [38,] 0.1617538
#> [39,] 0.2467375
#> [40,] 0.2207878
#> [41,] 0.2398162
#> [42,] 0.1780133
#> [43,] 0.2135767
#> [44,] 0.1560971
#> [45,] 0.1857134
#> [46,] 0.1814895
#> [47,] 0.2906819
#> [48,] 0.1891996
#> [49,] 0.1858285
#> [50,] 0.2263658
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.2492219
#> [2,] 0.2324782
#> [3,] 0.2285012
#> [4,] 0.1280917
#> [5,] 0.3080421
#> [6,] 0.1850605

(3) Check for parameter estimation accuracy

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

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8685714 0.9000000 0.9257143 0.9321429 0.9342857

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.5542857 0.6514286 0.7228571 0.7685714 0.7771429

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