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 
#>  30  41  89 157  33
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.09904 0.1334
#>  0.15350 0.1231
#>  0.11918 0.1246
#>  0.13944 0.1090
#>  0.14266 0.1728
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -0.77043
#> λ1     1.60727
#> λ2     0.18842
#> λ3     0.05505
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.2059
#> 0001  0.1672
#> 0010  0.1926
#> 0011  0.1652
#> 0100  0.1959
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 19189.46 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5137
#> M2:  0.49
#> total scores:  0.6266
a <- summary(output_HMDCM)
a$ss_EAP
#>             [,1]
#>  [1,] 0.09904479
#>  [2,] 0.15349964
#>  [3,] 0.11917743
#>  [4,] 0.13944262
#>  [5,] 0.14265567
#>  [6,] 0.17917100
#>  [7,] 0.17893958
#>  [8,] 0.19397319
#>  [9,] 0.17730484
#> [10,] 0.19406573
#> [11,] 0.16799602
#> [12,] 0.16082684
#> [13,] 0.10217917
#> [14,] 0.14911720
#> [15,] 0.22037698
#> [16,] 0.15187253
#> [17,] 0.15990293
#> [18,] 0.15115797
#> [19,] 0.19408649
#> [20,] 0.14882995
#> [21,] 0.17842014
#> [22,] 0.15902048
#> [23,] 0.19926322
#> [24,] 0.21231154
#> [25,] 0.17114294
#> [26,] 0.16002626
#> [27,] 0.16955515
#> [28,] 0.10899531
#> [29,] 0.19188149
#> [30,] 0.18282283
#> [31,] 0.14038541
#> [32,] 0.12966556
#> [33,] 0.23626679
#> [34,] 0.14041686
#> [35,] 0.20336883
#> [36,] 0.19487705
#> [37,] 0.22517102
#> [38,] 0.14514032
#> [39,] 0.14596526
#> [40,] 0.16462857
#> [41,] 0.17758418
#> [42,] 0.14343191
#> [43,] 0.17433316
#> [44,] 0.14901705
#> [45,] 0.19574577
#> [46,] 0.13351346
#> [47,] 0.11984117
#> [48,] 0.15755354
#> [49,] 0.12785622
#> [50,] 0.13342697
a$lambdas_EAP
#>           [,1]
#> λ0 -0.77042865
#> λ1  1.60726771
#> λ2  0.18841849
#> λ3  0.05504785
mean(a$PPP_total_scores)
#> [1] 0.6254776
mean(upper.tri(a$PPP_item_ORs))
#> [1] 0.49
mean(a$PPP_item_means)
#> [1] 0.4988571

(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.9428571 0.9500000 0.9657143 0.9778571 0.9742857

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.7942857 0.8257143 0.8714286 0.9171429 0.9028571

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2294.316            NA 14776.35 1262.826 18333.49
#> D(theta_bar)   2034.267            NA 14215.09 1228.161 17477.52
#> DIC            2554.364            NA 15337.61 1297.490 19189.46

head(a$PPP_total_scores)
#>           [,1]      [,2]      [,3]      [,4]      [,5]
#> [1,] 0.5000000 0.6000000 1.0000000 0.4857143 1.0000000
#> [2,] 0.9285714 0.5142857 0.8142857 0.3857143 0.5142857
#> [3,] 0.9714286 0.4714286 0.7857143 0.2857143 0.8000000
#> [4,] 0.2714286 0.7857143 1.0000000 0.5000000 0.8285714
#> [5,] 0.8142857 0.2142857 0.4571429 0.9714286 0.5142857
#> [6,] 0.6857143 0.5000000 0.4428571 0.2142857 1.0000000
head(a$PPP_item_means)
#> [1] 0.4857143 0.4857143 0.4571429 0.5142857 0.5142857 0.4714286
head(a$PPP_item_ORs)
#>      [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]
#> [1,]   NA 0.4285714 0.8000000 0.3428571 0.5857143 0.5142857 0.5428571 0.6000000
#> [2,]   NA        NA 0.2857143 0.9428571 0.3000000 0.5857143 0.5285714 0.7000000
#> [3,]   NA        NA        NA 0.7714286 0.7571429 0.9714286 0.5714286 0.7428571
#> [4,]   NA        NA        NA        NA 0.9428571 0.8285714 0.5000000 0.7571429
#> [5,]   NA        NA        NA        NA        NA 0.7000000 0.3857143 0.6714286
#> [6,]   NA        NA        NA        NA        NA        NA 0.3571429 0.6428571
#>            [,9]     [,10]      [,11]     [,12]     [,13]     [,14]     [,15]
#> [1,] 0.07142857 0.7142857 0.08571429 0.3714286 0.9857143 0.8142857 0.5714286
#> [2,] 0.55714286 0.9142857 0.77142857 0.6714286 0.9142857 0.8857143 0.1714286
#> [3,] 0.81428571 0.3857143 0.90000000 0.0000000 0.3571429 0.1714286 0.3428571
#> [4,] 0.72857143 0.1714286 0.20000000 0.6428571 0.7857143 0.2857143 0.3142857
#> [5,] 0.77142857 0.8857143 0.64285714 0.5000000 0.6571429 0.4000000 0.2714286
#> [6,] 0.75714286 0.3000000 0.72857143 0.4714286 0.9428571 0.2857143 0.4857143
#>           [,16]      [,17]     [,18]     [,19]      [,20]     [,21]      [,22]
#> [1,] 0.21428571 0.32857143 0.6428571 0.7571429 0.50000000 0.3714286 0.50000000
#> [2,] 0.72857143 0.38571429 0.6714286 0.6428571 0.27142857 0.4428571 0.35714286
#> [3,] 0.08571429 0.08571429 0.7428571 0.8000000 0.05714286 0.9000000 0.62857143
#> [4,] 0.48571429 0.67142857 0.1714286 0.9857143 0.62857143 0.3571429 0.07142857
#> [5,] 0.47142857 0.22857143 0.7000000 0.8142857 0.44285714 0.7142857 0.38571429
#> [6,] 0.72857143 0.40000000 0.5285714 0.7000000 0.67142857 0.7142857 0.67142857
#>          [,23]      [,24]     [,25]     [,26]     [,27]      [,28]     [,29]
#> [1,] 0.6571429 0.61428571 0.7714286 0.8571429 0.9000000 0.68571429 0.5857143
#> [2,] 0.9857143 0.60000000 0.9000000 0.9428571 0.6428571 0.85714286 0.7000000
#> [3,] 0.6428571 0.38571429 0.5285714 0.4428571 0.8714286 0.44285714 0.4571429
#> [4,] 0.3142857 0.40000000 0.2285714 0.3428571 0.8142857 0.10000000 0.5000000
#> [5,] 0.6142857 0.00000000 0.5571429 0.6142857 0.8857143 0.04285714 0.4857143
#> [6,] 0.6142857 0.04285714 0.3714286 0.5285714 0.9428571 0.04285714 0.7285714
#>          [,30]      [,31]      [,32]      [,33]     [,34]      [,35]      [,36]
#> [1,] 0.5714286 0.32857143 0.91428571 0.11428571 0.3142857 0.91428571 0.35714286
#> [2,] 0.6428571 0.87142857 0.95714286 0.64285714 0.6857143 0.88571429 0.05714286
#> [3,] 0.8142857 0.11428571 0.50000000 0.55714286 0.4000000 0.04285714 0.34285714
#> [4,] 0.2000000 0.41428571 0.08571429 0.78571429 0.1000000 0.35714286 0.84285714
#> [5,] 0.4000000 0.01428571 0.18571429 0.30000000 0.1142857 0.11428571 0.54285714
#> [6,] 0.5000000 0.35714286 0.78571429 0.04285714 0.6571429 0.20000000 0.31428571
#>           [,37]      [,38]     [,39]      [,40]     [,41]     [,42]     [,43]
#> [1,] 0.62857143 0.17142857 0.8857143 0.70000000 0.8000000 0.3428571 0.4714286
#> [2,] 0.17142857 0.04285714 0.1857143 0.41428571 0.7428571 0.8142857 0.5428571
#> [3,] 0.01428571 0.07142857 0.6142857 0.22857143 0.8857143 0.8285714 0.8428571
#> [4,] 0.10000000 0.65714286 0.5857143 0.65714286 0.2714286 0.6142857 0.9428571
#> [5,] 0.00000000 0.02857143 0.1714286 0.07142857 0.1857143 0.9714286 0.5857143
#> [6,] 0.30000000 0.47142857 1.0000000 0.14285714 0.8428571 0.1285714 0.1000000
#>          [,44]     [,45]      [,46]     [,47]     [,48]     [,49]      [,50]
#> [1,] 0.1428571 0.6000000 0.62857143 0.1428571 0.3285714 0.1285714 0.10000000
#> [2,] 0.7571429 0.4857143 0.81428571 0.3857143 0.8428571 0.8714286 0.64285714
#> [3,] 0.3000000 0.7571429 0.64285714 0.6857143 0.5000000 0.6142857 0.45714286
#> [4,] 0.8428571 0.6000000 0.07142857 0.7000000 0.6000000 0.1000000 0.91428571
#> [5,] 0.3428571 0.2142857 0.62857143 0.8142857 0.2000000 0.2285714 0.54285714
#> [6,] 0.8428571 0.3285714 0.04285714 0.3714286 0.4000000 0.3714286 0.08571429
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)