DINA_HO_RT_sep

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)

class_0 <- sample(1:2^K, N, replace = L)
Alphas_0 <- matrix(0,N,K)
for(i in 1:N){
  Alphas_0[i,] <- inv_bijectionvector(K,(class_0[i]-1))
}
thetas_true = rnorm(N,0,1)
tausd_true=0.5
taus_true = rnorm(N,0,tausd_true)
G_version = 3
phi_true = 0.8
lambdas_true <- c(-2, 1.6, .4, .055)       # empirical from Wang 2017
Alphas <- sim_alphas(model="HO_sep", 
                    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  55  92 109  37
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_sep = hmcdm(Y_sim,Q_matrix,"DINA_HO_RT_sep",Design_array,
                            100, 30,
                            Latency_array = L_sim, G_version = G_version,
                            theta_propose = 2,deltas_propose = c(.45,.35,.25,.06))
#> 0
output_HMDCM_RT_sep
#> 
#> Model: DINA_HO_RT_sep 
#> 
#> Sample Size: 350
#> Number of Items: 
#> Number of Time Points: 
#> 
#> Chain Length: 100, burn-in: 50
summary(output_HMDCM_RT_sep)
#> 
#> Model: DINA_HO_RT_sep 
#> 
#> Item Parameters:
#>  ss_EAP  gs_EAP
#>  0.1696 0.15456
#>  0.2198 0.12591
#>  0.1568 0.05395
#>  0.2044 0.06935
#>  0.2349 0.20335
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0     -2.2055
#> λ1      1.8472
#> λ2      0.1755
#> λ3      0.1377
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1174
#> 0001  0.1680
#> 0010  0.1724
#> 0011  0.2427
#> 0100  0.2042
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 160343.7 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5144
#> M2:  0.49
#> total scores:  0.6252
a <- summary(output_HMDCM_RT_sep)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.1696388
#> [2,] 0.2198299
#> [3,] 0.1567779
#> [4,] 0.2044430
#> [5,] 0.2348873
#> [6,] 0.1668379

(3) Check for parameter estimation accuracy

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

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.9250000 0.9442857 0.9500000 0.9592857 0.9542857

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.7428571 0.8057143 0.8200000 0.8514286 0.8457143

(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          2246.504      138880.8 15091.95 3206.424 159425.7
#> D(theta_bar)   1978.438      138454.7 14912.34 3162.238 158507.7
#> DIC            2514.570      139307.0 15271.56 3250.610 160343.7
head(a$PPP_total_scores)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.66 0.86 0.98 0.62 0.90
#> [2,] 0.90 0.30 0.72 0.20 0.52
#> [3,] 0.74 0.90 0.42 0.92 0.68
#> [4,] 0.26 0.96 0.82 0.86 0.32
#> [5,] 0.88 0.12 0.40 0.90 0.86
#> [6,] 0.92 0.66 0.64 0.84 0.84
head(a$PPP_item_means)
#> [1] 0.52 0.40 0.46 0.54 0.48 0.66
head(a$PPP_item_ORs)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   NA 0.74 0.58 0.78 0.84 0.94 0.82 0.68 0.74  0.26  0.86  0.32  0.60  0.14
#> [2,]   NA   NA 0.88 0.84 0.76 0.62 0.24 0.90 0.86  0.80  0.94  0.52  0.54  0.84
#> [3,]   NA   NA   NA 0.46 0.62 0.58 0.84 0.66 0.46  0.22  0.40  0.38  0.48  0.56
#> [4,]   NA   NA   NA   NA 0.26 0.64 0.20 0.46 0.78  0.12  0.80  0.94  0.58  0.08
#> [5,]   NA   NA   NA   NA   NA 0.28 0.66 0.74 0.62  0.08  0.00  0.06  0.08  0.06
#> [6,]   NA   NA   NA   NA   NA   NA 0.52 0.94 0.92  0.52  0.58  0.32  0.74  0.22
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,]  0.52  0.52  0.08  0.86  0.16  0.64  0.18  0.08  0.84  0.02  0.42  0.10
#> [2,]  0.64  0.80  0.14  1.00  0.64  0.68  0.22  0.62  1.00  0.26  0.66  0.58
#> [3,]  0.48  0.84  0.44  0.68  0.22  0.60  0.66  0.26  0.06  0.02  0.56  0.04
#> [4,]  0.32  0.98  0.66  0.12  0.22  0.02  0.76  0.40  0.38  0.16  0.30  0.10
#> [5,]  0.24  0.46  0.06  0.14  0.02  0.28  0.68  0.44  0.12  0.04  0.80  0.02
#> [6,]  0.92  0.72  0.40  0.96  0.48  0.74  0.44  0.18  0.68  0.44  0.46  0.30
#>      [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#> [1,]  0.22  0.44  0.06  0.16  0.76  1.00  0.88  0.84  0.72  0.94  0.40  0.70
#> [2,]  0.34  0.08  0.44  0.52  0.62  0.56  0.98  0.26  0.58  1.00  0.22  0.60
#> [3,]  0.22  0.02  0.62  0.20  0.80  0.74  0.36  0.46  0.36  0.26  0.78  0.30
#> [4,]  0.38  0.00  0.26  0.46  0.94  0.08  0.46  0.52  0.96  0.86  0.06  0.64
#> [5,]  0.96  0.06  0.44  0.02  0.34  0.20  0.18  0.06  0.70  0.24  0.10  0.26
#> [6,]  0.06  0.78  0.66  0.42  0.66  0.46  0.54  0.86  0.92  1.00  0.36  0.14
#>      [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#> [1,]  0.88  0.98  0.88  0.82  0.92  0.18  0.24  0.64  0.96  0.92  0.58  0.96
#> [2,]  0.84  0.52  0.94  0.88  0.06  0.14  0.72  0.34  0.42  0.34  0.72  0.30
#> [3,]  0.92  1.00  0.56  0.26  0.54  0.40  0.36  0.68  0.90  0.66  0.16  0.72
#> [4,]  0.70  0.90  0.82  0.86  0.84  0.58  0.78  0.64  0.48  0.30  0.56  0.06
#> [5,]  0.00  0.60  0.32  0.22  0.14  0.32  0.04  0.02  0.22  0.20  0.76  0.30
#> [6,]  0.42  0.74  0.92  0.34  0.70  0.78  0.30  0.96  0.90  0.72  1.00  1.00
library(bayesplot)
#> This is bayesplot version 1.11.1
#> - Online documentation and vignettes at mc-stan.org/bayesplot
#> - bayesplot theme set to bayesplot::theme_default()
#>    * Does _not_ affect other ggplot2 plots
#>    * See ?bayesplot_theme_set for details on theme setting
pp_check(output_HMDCM_RT_sep, type="total_latency")