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  41  87 140  46
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.21358 0.1295
#>  0.12764 0.1291
#>  0.13752 0.1891
#>  0.16142 0.2463
#>  0.09328 0.1028
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    lambdas_EAP
#> λ0    -0.87888
#> λ1     1.58656
#> λ2     0.27205
#> λ3     0.04635
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000  0.1954
#> 0001  0.1828
#> 0010  0.1725
#> 0011  0.1633
#> 0100  0.2049
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 18918.4 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5186
#> M2:  0.49
#> total scores:  0.6267
a <- summary(output_HMDCM)
a$ss_EAP
#>             [,1]
#>  [1,] 0.21358253
#>  [2,] 0.12764328
#>  [3,] 0.13752461
#>  [4,] 0.16142071
#>  [5,] 0.09327540
#>  [6,] 0.11639334
#>  [7,] 0.17008166
#>  [8,] 0.13363641
#>  [9,] 0.09202580
#> [10,] 0.21317484
#> [11,] 0.18044225
#> [12,] 0.13340315
#> [13,] 0.25253290
#> [14,] 0.19863744
#> [15,] 0.09045943
#> [16,] 0.18132641
#> [17,] 0.14035492
#> [18,] 0.13874298
#> [19,] 0.22679362
#> [20,] 0.14854039
#> [21,] 0.17433503
#> [22,] 0.14927752
#> [23,] 0.11550883
#> [24,] 0.10220618
#> [25,] 0.12419254
#> [26,] 0.14270303
#> [27,] 0.20507627
#> [28,] 0.17655728
#> [29,] 0.14276231
#> [30,] 0.12151921
#> [31,] 0.13087156
#> [32,] 0.11924670
#> [33,] 0.15372113
#> [34,] 0.21141074
#> [35,] 0.08586200
#> [36,] 0.15638653
#> [37,] 0.18941353
#> [38,] 0.16143471
#> [39,] 0.16002566
#> [40,] 0.10842000
#> [41,] 0.11254295
#> [42,] 0.08257697
#> [43,] 0.20037207
#> [44,] 0.12738953
#> [45,] 0.16641782
#> [46,] 0.16370288
#> [47,] 0.13573702
#> [48,] 0.13486301
#> [49,] 0.22782097
#> [50,] 0.18130822
a$lambdas_EAP
#>           [,1]
#> λ0 -0.87888032
#> λ1  1.58655746
#> λ2  0.27204642
#> λ3  0.04634614
mean(a$PPP_total_scores)
#> [1] 0.6288571
mean(upper.tri(a$PPP_item_ORs))
#> [1] 0.49
mean(a$PPP_item_means)
#> [1] 0.518

(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.9292857 0.9514286 0.9678571 0.9728571 0.9721429

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.7628571 0.8371429 0.8885714 0.9000000 0.9057143

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2289.555            NA 14499.59 1271.778 18060.92
#> D(theta_bar)   2008.706            NA 13948.75 1245.992 17203.45
#> DIC            2570.405            NA 15050.43 1297.564 18918.40

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