rRUM_indept

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 and response times based on the rRUM model

tau <- numeric(K)
for(k in 1:K){
  tau[k] <- runif(1,.2,.6)
}
R = matrix(0,K,K)
# Initial alphas
p_mastery <- c(.5,.5,.4,.4)
Alphas_0 <- matrix(0,N,K)
for(i in 1:N){
  for(k in 1:K){
    prereqs <- which(R[k,]==1)
    if(length(prereqs)==0){
      Alphas_0[i,k] <- rbinom(1,1,p_mastery[k])
    }
    if(length(prereqs)>0){
      Alphas_0[i,k] <- prod(Alphas_0[i,prereqs])*rbinom(1,1,p_mastery)
    }
  }
}
Alphas <- sim_alphas(model="indept",taus=tau,N=N,L=L,R=R,alpha0=Alphas_0)
table(rowSums(Alphas[,,5]) - rowSums(Alphas[,,1])) # used to see how much transition has taken place
#> 
#>   0   1   2   3   4 
#>  16  83 128  99  24
Smats <- matrix(runif(J*K,.1,.3),c(J,K))
Gmats <- matrix(runif(J*K,.1,.3),c(J,K))
# Simulate rRUM parameters
r_stars <- Gmats / (1-Smats)
pi_stars <- apply((1-Smats)^Q_matrix, 1, prod)

Y_sim <- sim_hmcdm(model="rRUM",Alphas,Q_matrix,Design_array,
                   r_stars=r_stars,pi_stars=pi_stars)

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

output_rRUM_indept = hmcdm(Y_sim,Q_matrix,"rRUM_indept",Design_array,
                           100,30,R = R)
#> 0
output_rRUM_indept
#> 
#> Model: rRUM_indept 
#> 
#> Sample Size: 350
#> Number of Items: 
#> Number of Time Points: 
#> 
#> Chain Length: 100, burn-in: 50
summary(output_rRUM_indept)
#> 
#> Model: rRUM_indept 
#> 
#> Item Parameters:
#>  r_stars1_EAP r_stars2_EAP r_stars3_EAP r_stars4_EAP pi_stars_EAP
#>       0.29776       0.5901       0.6925       0.6861       0.8599
#>       0.53300       0.2135       0.5674       0.6894       0.8352
#>       0.52574       0.6920       0.5118       0.2915       0.8226
#>       0.50214       0.6039       0.2948       0.6201       0.8675
#>       0.06155       0.3297       0.6349       0.6697       0.7271
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.4745
#> τ2   0.5502
#> τ3   0.5710
#> τ4   0.5693
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.09773
#> 0001 0.10582
#> 0010 0.08946
#> 0011 0.01896
#> 0100 0.07938
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22586.41 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5188
#> M2:  0.49
#> total scores:  0.6139
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>            [,1]      [,2]      [,3]      [,4]
#> [1,] 0.29775749 0.5900971 0.6925137 0.6861163
#> [2,] 0.53299908 0.2134798 0.5674403 0.6894325
#> [3,] 0.52574237 0.6919573 0.5118377 0.2915179
#> [4,] 0.50214219 0.6038765 0.2948108 0.6200818
#> [5,] 0.06155305 0.3296737 0.6348960 0.6697372
#> [6,] 0.59803607 0.5290684 0.1659758 0.5963194

(3) Check for parameter estimation accuracy

(cor_pistars <- cor(as.vector(pi_stars),as.vector(a$pi_stars_EAP)))
#> [1] 0.9603115
(cor_rstars <- cor(as.vector(r_stars*Q_matrix),as.vector(a$r_stars_EAP*Q_matrix)))
#> [1] 0.9068436

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8364286 0.8892857 0.9421429 0.9721429 0.9864286

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.5057143 0.6314286 0.7971429 0.8914286 0.9457143

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          1995.091            NA 18037.56 1817.587 21850.23
#> D(theta_bar)   1941.895            NA 17382.04 1790.128 21114.06
#> DIC            2048.286            NA 18693.07 1845.047 22586.41
head(a$PPP_total_scores)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.34 0.90 0.06 0.66 0.26
#> [2,] 0.98 0.60 0.62 0.66 0.68
#> [3,] 0.34 1.00 0.44 0.50 1.00
#> [4,] 0.80 0.96 0.74 0.50 0.34
#> [5,] 0.80 0.48 1.00 0.20 0.68
#> [6,] 0.76 0.28 0.84 0.78 0.20
head(a$PPP_item_means)
#> [1] 0.58 0.46 0.54 0.62 0.34 0.56
head(a$PPP_item_ORs)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]   NA  0.7 0.90 0.58 0.54 0.82 0.34 0.12 0.24  0.36  0.32  0.82  0.68  0.76
#> [2,]   NA   NA 0.78 0.94 0.72 0.76 0.98 0.62 0.46  0.88  0.28  0.50  0.44  0.88
#> [3,]   NA   NA   NA 0.60 0.88 0.56 0.54 0.46 0.04  0.80  0.70  0.60  0.76  0.90
#> [4,]   NA   NA   NA   NA 0.88 0.52 0.68 0.70 0.14  0.86  0.52  0.40  0.74  0.66
#> [5,]   NA   NA   NA   NA   NA 0.32 0.56 0.12 0.56  0.60  0.56  0.34  0.44  0.42
#> [6,]   NA   NA   NA   NA   NA   NA 0.62 0.24 0.50  0.62  0.86  0.26  0.10  0.76
#>      [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> [1,]  0.38  0.22  0.58  0.20  0.36  0.46  0.62  0.58  0.08  0.42  0.92  0.72
#> [2,]  0.46  0.46  0.64  0.26  0.80  0.28  0.38  0.42  0.78  0.96  0.34  0.88
#> [3,]  0.42  0.72  0.68  0.70  0.14  0.54  0.42  0.26  0.92  0.96  0.70  0.72
#> [4,]  0.74  0.58  0.18  0.18  0.94  0.14  0.82  0.02  0.36  0.98  0.48  0.20
#> [5,]  0.60  0.10  0.86  0.36  0.70  0.28  0.46  0.78  0.94  0.56  1.00  0.80
#> [6,]  1.00  0.38  0.84  0.16  0.36  0.58  0.32  0.10  0.92  0.62  0.98  0.64
#>      [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
#> [1,]  0.94  0.90  0.78  0.96  0.96  0.78  0.64  0.14  0.74  0.68  0.90  0.76
#> [2,]  0.84  0.70  0.56  0.36  0.28  0.92  0.22  0.02  0.04  0.98  0.64  0.26
#> [3,]  0.54  1.00  0.32  0.38  0.42  0.16  0.26  0.32  0.86  0.70  0.40  0.46
#> [4,]  0.00  0.84  0.72  0.20  0.74  1.00  0.66  0.72  0.66  0.28  0.42  0.56
#> [5,]  0.22  0.88  0.46  0.56  0.18  1.00  0.48  0.24  0.42  0.46  0.92  0.78
#> [6,]  0.74  0.36  0.64  0.26  0.96  0.88  0.92  0.10  0.68  0.50  0.60  0.74
#>      [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
#> [1,]  0.30  0.60  0.60  0.20  0.54  0.90  0.66  0.94  0.36  0.76  0.66  0.90
#> [2,]  0.22  0.70  0.20  0.12  0.34  0.16  0.60  0.88  0.40  0.14  0.50  0.78
#> [3,]  0.48  0.62  0.30  0.92  0.18  0.12  0.76  0.30  0.16  0.22  0.22  0.52
#> [4,]  0.56  0.56  0.84  0.50  0.62  0.76  0.46  0.24  0.18  0.82  0.82  0.48
#> [5,]  0.30  0.70  0.08  0.74  0.26  0.64  0.28  0.66  0.76  0.56  0.66  0.62
#> [6,]  0.10  0.92  0.58  0.42  0.08  0.96  0.28  0.72  0.76  0.42  0.54  0.46