HMDCM

library(hmcdm)

Load the spatial rotation data

N = dim(Design_array)[1]
J = nrow(Q_matrix)
K = ncol(Q_matrix)
L = dim(Design_array)[3]

(1) Simulate responses based on the HMDCM model

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)
lambdas_true = c(-1, 1.8, .277, .055)
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 
#>  36  39  89 136  50
itempars_true <- matrix(runif(J*2,.1,.2), ncol=2)

Y_sim <- sim_hmcdm(model="DINA",Alphas,Q_matrix,Design_array,
                   itempars=itempars_true)

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

output_HMDCM = hmcdm(Y_sim,Q_matrix,"DINA_HO",Test_order = Test_order, Test_versions = Test_versions,
                     chain_length=100,burn_in=30,
                     theta_propose = 2,deltas_propose = c(.45,.35,.25,.06))
#> 0

output_HMDCM = hmcdm(Y_sim,Q_matrix,"DINA_HO",Design_array,
                     chain_length=100,burn_in=30,
                     theta_propose = 2,deltas_propose = c(.45,.35,.25,.06))
#> 0

output_HMDCM
#> 
#> Model: DINA_HO 
#> 
#> Sample Size: 350
#> Number of Items: 
#> Number of Time Points: 
#> 
#> Chain Length: 100, burn-in: 30

summary(output_HMDCM)
#> 
#> Model: DINA_HO 
#> 
#> Item Parameters:
#>  ss_EAP gs_EAP
#>  0.2112 0.1371
#>  0.1593 0.1608
#>  0.1248 0.1812
#>  0.1877 0.1787
#>  0.1054 0.1409
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -0.85498
#> λ1     1.56434
#> λ2     0.24989
#> λ3     0.03998
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1519
#> 0001  0.2056
#> 0010  0.1318
#> 0011  0.2366
#> 0100  0.2073
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 19251.16 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5143
#> M2:  0.49
#> total scores:  0.6222
a <- summary(output_HMDCM)
a$ss_EAP
#>             [,1]
#>  [1,] 0.21118897
#>  [2,] 0.15925862
#>  [3,] 0.12479288
#>  [4,] 0.18765883
#>  [5,] 0.10538152
#>  [6,] 0.15571992
#>  [7,] 0.11071405
#>  [8,] 0.16459741
#>  [9,] 0.13739319
#> [10,] 0.18774889
#> [11,] 0.18563769
#> [12,] 0.23822574
#> [13,] 0.15611685
#> [14,] 0.10946231
#> [15,] 0.26411282
#> [16,] 0.20400420
#> [17,] 0.18502860
#> [18,] 0.21323029
#> [19,] 0.17883469
#> [20,] 0.12286980
#> [21,] 0.11799424
#> [22,] 0.18697447
#> [23,] 0.19234367
#> [24,] 0.13734697
#> [25,] 0.24828932
#> [26,] 0.21623678
#> [27,] 0.14381524
#> [28,] 0.13961786
#> [29,] 0.18972727
#> [30,] 0.14970499
#> [31,] 0.22186050
#> [32,] 0.16504859
#> [33,] 0.10649067
#> [34,] 0.15676563
#> [35,] 0.22212583
#> [36,] 0.23371503
#> [37,] 0.12960363
#> [38,] 0.15292586
#> [39,] 0.16413422
#> [40,] 0.18707181
#> [41,] 0.14417459
#> [42,] 0.21031624
#> [43,] 0.19377094
#> [44,] 0.11203844
#> [45,] 0.09280784
#> [46,] 0.14427225
#> [47,] 0.14602329
#> [48,] 0.15070811
#> [49,] 0.18602924
#> [50,] 0.16902714
a$lambdas_EAP
#>           [,1]
#> λ0 -0.85497976
#> λ1  1.56434394
#> λ2  0.24989230
#> λ3  0.03997534
mean(a$PPP_total_scores)
#> [1] 0.6225224
mean(upper.tri(a$PPP_item_ORs))
#> [1] 0.49
mean(a$PPP_item_means)
#> [1] 0.5111429

(3) Evaluate the accuracy of estimated parameters

Attribute-wise agreement rate between true and estimated alphas

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.9242857 0.9342857 0.9692857 0.9800000 0.9778571

Pattern-wise agreement rate between true and estimated alphas

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.7514286 0.7714286 0.8971429 0.9285714 0.9257143

(4) Evaluate the fit of the model to the observed response

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2143.122            NA 15029.29 1233.981 18406.39
#> D(theta_bar)   1882.604            NA 14489.03 1189.979 17561.61
#> DIC            2403.641            NA 15569.54 1277.984 19251.16

head(a$PPP_total_scores)
#>           [,1]      [,2]      [,3]      [,4]      [,5]
#> [1,] 0.9142857 0.8571429 1.0000000 1.0000000 0.5000000
#> [2,] 0.8571429 0.7428571 0.6428571 0.3857143 1.0000000
#> [3,] 0.1142857 0.2857143 0.1142857 0.3000000 0.5285714
#> [4,] 0.9142857 0.4571429 0.1000000 1.0000000 0.4428571
#> [5,] 0.9142857 0.5142857 0.2428571 0.9285714 0.3571429
#> [6,] 0.8571429 0.5857143 0.3714286 0.2571429 0.5285714
head(a$PPP_item_means)
#> [1] 0.5428571 0.5857143 0.5428571 0.5285714 0.5857143 0.5428571
head(a$PPP_item_ORs)
#>      [,1] [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]
#> [1,]   NA  0.4 0.6285714 0.5142857 0.8142857 0.8000000 0.6142857 0.8285714
#> [2,]   NA   NA 0.1428571 0.3142857 0.2142857 0.5285714 0.1285714 0.4428571
#> [3,]   NA   NA        NA 0.5571429 0.4142857 0.9285714 0.7428571 0.4571429
#> [4,]   NA   NA        NA        NA 0.6142857 0.3571429 0.7000000 0.6142857
#> [5,]   NA   NA        NA        NA        NA 0.5857143 0.6428571 0.5428571
#> [6,]   NA   NA        NA        NA        NA        NA 0.9285714 0.9571429
#>           [,9]     [,10]     [,11]      [,12]      [,13]      [,14]     [,15]
#> [1,] 0.2571429 0.7571429 0.7428571 0.34285714 0.04285714 0.45714286 0.4000000
#> [2,] 0.4000000 0.6428571 0.5571429 0.02857143 0.00000000 0.35714286 0.7571429
#> [3,] 0.5000000 0.8000000 0.6142857 0.55714286 0.34285714 0.27142857 0.7285714
#> [4,] 0.4000000 0.2000000 0.8285714 0.61428571 0.94285714 0.51428571 0.6571429
#> [5,] 0.5571429 0.3714286 0.7857143 0.18571429 0.02857143 0.01428571 0.4142857
#> [6,] 0.2571429 0.6857143 0.6428571 0.95714286 0.18571429 0.61428571 0.6714286
#>          [,16]      [,17]     [,18]     [,19]     [,20]     [,21]      [,22]
#> [1,] 0.3142857 0.17142857 0.9142857 0.6571429 0.6714286 0.5714286 0.10000000
#> [2,] 0.7000000 0.10000000 0.4571429 0.2142857 0.1000000 0.2714286 0.14285714
#> [3,] 0.2714286 0.27142857 0.6857143 0.3285714 0.2142857 0.3857143 0.90000000
#> [4,] 0.9857143 0.07142857 0.9571429 0.1714286 0.9714286 0.6142857 0.47142857
#> [5,] 0.1285714 0.31428571 0.7142857 0.7142857 0.7142857 0.3714286 0.02857143
#> [6,] 0.7142857 0.52857143 0.0000000 0.2857143 0.8428571 0.5571429 0.52857143
#>          [,23]     [,24]     [,25]     [,26]      [,27]     [,28]     [,29]
#> [1,] 0.9000000 0.6428571 0.7428571 0.8142857 0.75714286 0.5142857 0.9285714
#> [2,] 0.7000000 0.3857143 0.9428571 0.5571429 0.05714286 0.1571429 0.1285714
#> [3,] 0.3714286 0.7571429 0.3428571 1.0000000 0.94285714 0.8857143 0.6142857
#> [4,] 0.9428571 0.4142857 0.7571429 0.8857143 0.98571429 0.4571429 0.7857143
#> [5,] 0.4142857 0.5000000 0.3857143 0.8285714 0.40000000 0.7000000 0.5000000
#> [6,] 0.4428571 0.8714286 0.9000000 0.9857143 0.27142857 0.3714286 0.9714286
#>          [,30]     [,31]     [,32]     [,33]     [,34]      [,35]      [,36]
#> [1,] 0.6285714 0.2428571 0.4142857 0.6000000 0.3857143 0.11428571 0.81428571
#> [2,] 0.1142857 0.6714286 0.2000000 0.0000000 0.0000000 0.08571429 0.01428571
#> [3,] 0.9142857 0.4000000 0.5428571 0.4285714 0.7428571 0.82857143 0.34285714
#> [4,] 0.9428571 0.4142857 0.3714286 0.3285714 0.2857143 0.75714286 0.41428571
#> [5,] 0.3142857 0.5714286 0.8857143 0.5142857 0.2428571 0.22857143 0.18571429
#> [6,] 0.6142857 0.3857143 0.4857143 0.2000000 0.7428571 0.64285714 0.44285714
#>           [,37]     [,38]      [,39]     [,40]      [,41]     [,42]      [,43]
#> [1,] 0.67142857 0.9285714 0.57142857 0.8000000 0.81428571 0.6571429 0.87142857
#> [2,] 0.07142857 0.2142857 0.02857143 0.1571429 0.02857143 0.6428571 0.01428571
#> [3,] 0.71428571 0.5000000 0.52857143 0.8142857 0.72857143 0.5142857 0.17142857
#> [4,] 0.62857143 0.7571429 0.27142857 0.2571429 0.08571429 0.1285714 0.08571429
#> [5,] 0.58571429 0.9857143 0.81428571 0.7285714 0.65714286 0.4428571 0.62857143
#> [6,] 0.35714286 0.9857143 0.38571429 0.8428571 0.78571429 0.9142857 0.10000000
#>          [,44]      [,45]      [,46]     [,47]      [,48]     [,49]      [,50]
#> [1,] 0.4571429 0.81428571 0.61428571 1.0000000 0.64285714 0.4714286 0.75714286
#> [2,] 0.5857143 0.02857143 0.05714286 0.3285714 0.04285714 0.2428571 0.11428571
#> [3,] 0.8142857 0.20000000 0.80000000 0.7714286 0.34285714 0.4571429 0.08571429
#> [4,] 0.2428571 0.10000000 0.11428571 0.2571429 0.01428571 0.1857143 0.18571429
#> [5,] 0.1428571 0.28571429 0.22857143 0.8000000 0.41428571 0.9000000 0.71428571
#> [6,] 0.7571429 0.25714286 0.78571429 1.0000000 0.88571429 0.5428571 0.88571429
library(bayesplot)
pp_check(output_HMDCM)

pp_check(output_HMDCM, plotfun="dens_overlay", type="item_mean")

pp_check(output_HMDCM, plotfun="hist", type="item_OR")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

pp_check(output_HMDCM, plotfun="stat_2d", type="item_mean")

pp_check(output_HMDCM, plotfun="scatter_avg", type="total_score")

pp_check(output_HMDCM, plotfun="error_scatter_avg", type="total_score")

Convergence checking

Checking convergence of the two independent MCMC chains with different initial values using coda package.

# output_HMDCM1 = hmcdm(Y_sim, Q_matrix, "DINA_HO", Design_array,
#                      chain_length=100, burn_in=30,
#                      theta_propose = 2, deltas_propose = c(.45,.35,.25,.06))
# output_HMDCM2 = hmcdm(Y_sim, Q_matrix, "DINA_HO", Design_array,
#                      chain_length=100, burn_in=30,
#                      theta_propose = 2, deltas_propose = c(.45,.35,.25,.06))
# 
# library(coda)
# 
# x <- mcmc.list(mcmc(t(rbind(output_HMDCM1$ss, output_HMDCM1$gs, output_HMDCM1$lambdas))),
#                mcmc(t(rbind(output_HMDCM2$ss, output_HMDCM2$gs, output_HMDCM2$lambdas))))
# 
# gelman.diag(x, autoburnin=F)