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 
#>  41 110 115  70  14
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.1302       0.6825       0.6183       0.5339       0.7409
#>        0.5213       0.5079       0.5291       0.6317       0.6683
#>        0.6874       0.5009       0.6341       0.2334       0.8266
#>        0.5534       0.6193       0.4453       0.5151       0.7284
#>        0.2005       0.4101       0.5383       0.6939       0.5980
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.2967
#> τ2   0.3594
#> τ3   0.3295
#> τ4   0.4809
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.09130
#> 0001 0.07124
#> 0010 0.09372
#> 0011 0.05630
#> 0100 0.05379
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 23159.75 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5192
#> M2:  0.49
#> total scores:  0.6151
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]      [,2]      [,3]      [,4]
#> [1,] 0.1302459 0.6825197 0.6182755 0.5338955
#> [2,] 0.5213094 0.5078983 0.5290785 0.6316690
#> [3,] 0.6873631 0.5009308 0.6340834 0.2333791
#> [4,] 0.5534394 0.6193282 0.4452943 0.5150836
#> [5,] 0.2004698 0.4101382 0.5382574 0.6938787
#> [6,] 0.5791486 0.2274723 0.1505156 0.6274593

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8485714 0.8885714 0.9235714 0.9514286 0.9521429

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.5257143 0.6342857 0.7314286 0.8200000 0.8171429

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

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