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)
Jt = J/L

(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)
Q_examinee <- Q_list(Q_matrix, Test_order, Test_versions)
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 <- simulate_alphas_HO_sep(lambdas_true,thetas_true,Alphas_0,Q_examinee,L,Jt)
table(rowSums(Alphas[,,5]) - rowSums(Alphas[,,1])) # used to see how much transition has taken place
#> 
#>   0   1   2   3   4 
#>  75 118 100  50   7
itempars_true <- array(runif(J*2,0.1,0.3), dim = c(Jt,2,L))
RT_itempars_true <- array(NA, dim = c(Jt,2,L))
RT_itempars_true[,2,] <- rnorm(Jt*L,3.45,.5)
RT_itempars_true[,1,] <- runif(Jt*L,1.5,2)

Y_sim <- simDINA(Alphas,itempars_true,ETAs,Test_order,Test_versions)
L_sim <- sim_RT(Alphas,RT_itempars_true,Q_matrix,taus_true,phi_true,ETAs,G_version,Test_order,Test_versions)

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

output_HMDCM_RT_sep = hmcdm(Y_sim,Q_matrix,"DINA_HO_RT_sep",Test_order,Test_versions,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: 50
#> Number of Time Points: 5 
#> 
#> Chain Length: 100, burn-in: 30
summary(output_HMDCM_RT_sep)
#> 
#> Model: DINA_HO_RT_sep 
#> 
#> Item Parameters:
#>  ss_EAP gs_EAP
#>  0.2223 0.1943
#>  0.1445 0.2416
#>  0.1312 0.2318
#>  0.2010 0.2657
#>  0.2383 0.3690
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0      -1.949
#> λ1       1.843
#> λ2       0.176
#> λ3       0.125
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.02430
#> 0001 0.04893
#> 0010 0.05569
#> 0011 0.05079
#> 0100 0.08536
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 52821.98 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4989
#> M2:  0.49
#> total scores:  0.6135
a <- summary(output_HMDCM_RT_sep)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.2222802
#> [2,] 0.1445485
#> [3,] 0.1312421
#> [4,] 0.2009617
#> [5,] 0.2382525
#> [6,] 0.2990315
object <- output_HMDCM_RT_sep

(3) Check for parameter estimation accuracy

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

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8742857 0.9164286 0.9328571 0.9607143 0.9600000

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.6142857 0.7114286 0.7714286 0.8628571 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          1441.025      32796.82 17410.96 3775.186 55423.99
#> D(theta_bar)   1163.203      36172.52 16933.51 3756.775 58026.01
#> DIC            1718.846      29421.12 17888.42 3793.597 52821.98
head(a$PPP_total_scores)
#>           [,1]      [,2]       [,3]      [,4]       [,5]
#> [1,] 0.6142857 0.9714286 0.98571429 0.5142857 0.92857143
#> [2,] 0.2000000 0.7857143 0.65714286 0.6571429 0.35714286
#> [3,] 0.4571429 0.1571429 0.27142857 0.3285714 0.62857143
#> [4,] 0.6714286 0.5571429 0.94285714 0.7714286 0.28571429
#> [5,] 0.8571429 0.5857143 0.07142857 0.7142857 0.41428571
#> [6,] 0.3714286 0.7428571 0.88571429 1.0000000 0.01428571
head(a$PPP_item_means)
#> [1] 0.5142857 0.5000000 0.6142857 0.4142857 0.6000000 0.4142857
head(a$PPP_item_ORs)
#>      [,1]      [,2]      [,3]       [,4]      [,5]      [,6]       [,7]
#> [1,]   NA 0.4142857 0.0000000 0.84285714 0.1571429 0.5000000 0.27142857
#> [2,]   NA        NA 0.4714286 0.08571429 0.8857143 0.6714286 0.30000000
#> [3,]   NA        NA        NA 0.37142857 0.1285714 0.3714286 0.61428571
#> [4,]   NA        NA        NA         NA 0.7285714 0.1714286 0.08571429
#> [5,]   NA        NA        NA         NA        NA 0.7857143 0.44285714
#> [6,]   NA        NA        NA         NA        NA        NA 0.87142857
#>           [,8]      [,9]     [,10]     [,11]     [,12]     [,13]      [,14]
#> [1,] 0.2285714 0.4285714 0.2285714 0.3000000 0.7000000 0.6428571 0.85714286
#> [2,] 0.5000000 0.7714286 0.5857143 0.2714286 0.6142857 0.2714286 0.05714286
#> [3,] 0.3285714 0.0000000 0.6428571 0.5428571 0.5428571 0.4000000 0.10000000
#> [4,] 0.9571429 0.5428571 0.2857143 0.6285714 0.7428571 0.8714286 0.25714286
#> [5,] 0.7428571 0.6571429 0.1857143 0.2571429 0.8285714 0.2000000 0.71428571
#> [6,] 0.9142857 0.4571429 0.5142857 0.8000000 0.8857143 0.8571429 0.28571429
#>          [,15]     [,16]      [,17]     [,18]      [,19]     [,20]      [,21]
#> [1,] 0.3857143 0.4857143 0.62857143 0.7857143 0.80000000 0.5000000 0.84285714
#> [2,] 0.7428571 0.8571429 0.77142857 0.7000000 0.04285714 0.5142857 0.82857143
#> [3,] 0.4857143 0.1571429 0.97142857 0.8000000 0.92857143 0.2000000 0.84285714
#> [4,] 0.2428571 0.1714286 0.05714286 0.8428571 0.50000000 0.7285714 0.05714286
#> [5,] 0.8428571 0.8428571 0.88571429 0.1714286 0.18571429 0.9428571 0.14285714
#> [6,] 0.2285714 0.6714286 0.77142857 0.5000000 0.31428571 0.6142857 0.52857143
#>          [,22]     [,23]      [,24]     [,25]      [,26]     [,27]     [,28]
#> [1,] 0.5142857 0.9571429 0.82857143 0.1714286 0.95714286 0.8142857 0.3428571
#> [2,] 0.8571429 0.8714286 0.72857143 0.7857143 0.87142857 0.7285714 0.8714286
#> [3,] 0.0000000 0.9000000 0.40000000 0.6428571 0.01428571 0.6571429 0.9571429
#> [4,] 0.7714286 0.7714286 0.37142857 0.1714286 0.85714286 0.5714286 0.4857143
#> [5,] 0.0000000 0.4428571 0.05714286 0.1142857 0.60000000 0.5428571 0.1571429
#> [6,] 0.9571429 0.5857143 0.98571429 0.9428571 0.84285714 0.8142857 0.1714286
#>          [,29]     [,30]     [,31]      [,32]      [,33]     [,34]     [,35]
#> [1,] 0.6857143 0.8857143 0.6714286 0.18571429 0.54285714 0.5857143 0.5857143
#> [2,] 0.7714286 0.8285714 0.9714286 0.17142857 0.64285714 0.6571429 0.5285714
#> [3,] 0.5714286 0.4142857 0.6142857 0.90000000 0.30000000 0.8428571 0.2000000
#> [4,] 0.4714286 0.4285714 0.1428571 0.32857143 0.07142857 0.5428571 0.3428571
#> [5,] 0.8428571 0.5285714 0.6714286 0.07142857 0.44285714 0.1857143 0.5142857
#> [6,] 0.6000000 0.1714286 0.5714286 0.97142857 0.81428571 0.4000000 0.8571429
#>          [,36]      [,37]     [,38]     [,39]      [,40]     [,41]     [,42]
#> [1,] 0.4857143 0.51428571 0.5142857 0.5142857 0.55714286 0.1285714 0.3142857
#> [2,] 1.0000000 0.25714286 1.0000000 0.3714286 0.60000000 0.2428571 0.4571429
#> [3,] 0.8142857 0.04285714 0.8571429 0.8571429 0.80000000 0.4857143 0.0000000
#> [4,] 0.2000000 0.27142857 0.3000000 0.2857143 0.07142857 0.5000000 0.6000000
#> [5,] 0.8142857 0.27142857 0.2857143 0.1428571 0.47142857 0.3571429 0.4142857
#> [6,] 0.6857143 0.47142857 0.9000000 0.6428571 0.74285714 0.1142857 0.9000000
#>           [,43]      [,44]     [,45]      [,46]      [,47]      [,48]     [,49]
#> [1,] 0.67142857 0.18571429 0.5285714 0.52857143 0.32857143 0.15714286 0.6714286
#> [2,] 0.05714286 0.30000000 0.6142857 0.97142857 0.38571429 0.37142857 0.5000000
#> [3,] 0.78571429 0.84285714 0.7571429 0.14285714 0.90000000 0.71428571 0.8285714
#> [4,] 0.57142857 0.01428571 0.3857143 0.08571429 0.48571429 0.01428571 0.2857143
#> [5,] 0.40000000 0.44285714 0.3428571 0.22857143 0.01428571 0.75714286 0.1571429
#> [6,] 0.90000000 0.61428571 0.5428571 0.84285714 0.30000000 0.37142857 0.3285714
#>           [,50]
#> [1,] 0.34285714
#> [2,] 0.44285714
#> [3,] 0.92857143
#> [4,] 0.32857143
#> [5,] 0.04285714
#> [6,] 0.38571429
library(bayesplot)
#> This is bayesplot version 1.9.0
#> - 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")