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 
#>  37 106 118  72  17
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.2989       0.5341       0.5993       0.6475       0.8725
#>        0.6758       0.3190       0.6093       0.5328       0.7884
#>        0.5138       0.5329       0.6858       0.2400       0.6886
#>        0.5473       0.5956       0.1320       0.5432       0.8603
#>        0.2891       0.3269       0.5103       0.6944       0.6696
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.3364
#> τ2   0.4608
#> τ3   0.2979
#> τ4   0.3786
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.14686
#> 0001 0.04497
#> 0010 0.04501
#> 0011 0.04183
#> 0100 0.04043
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22668.23 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5172
#> M2:  0.49
#> total scores:  0.6144
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]      [,2]      [,3]      [,4]
#> [1,] 0.2988551 0.5340914 0.5992655 0.6474611
#> [2,] 0.6758219 0.3190238 0.6093436 0.5328129
#> [3,] 0.5138256 0.5329382 0.6858376 0.2399989
#> [4,] 0.5472993 0.5955933 0.1320301 0.5432113
#> [5,] 0.2891097 0.3268992 0.5103029 0.6943803
#> [6,] 0.6568055 0.3739598 0.3769342 0.6470811

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8592857 0.8978571 0.9357143 0.9571429 0.9642857

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.5228571 0.6257143 0.7657143 0.8428571 0.8657143

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2250.124            NA 17841.71 1853.633 21945.46
#> D(theta_bar)   2166.459            NA 17206.19 1850.046 21222.70
#> DIC            2333.788            NA 18477.22 1857.221 22668.23
head(a$PPP_total_scores)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.08 0.60 0.78 0.88 0.80
#> [2,] 0.70 0.58 0.72 0.16 0.22
#> [3,] 0.36 0.50 0.44 0.40 0.88
#> [4,] 0.48 0.92 0.96 0.62 0.26
#> [5,] 0.20 0.80 0.28 0.86 0.50
#> [6,] 0.26 0.06 0.70 1.00 1.00
head(a$PPP_item_means)
#> [1] 0.58 0.50 0.52 0.40 0.58 0.56
head(a$PPP_item_ORs)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]     [,10] [,11] [,12] [,13]
#> [1,]   NA  0.4 1.00 0.50 0.24 0.20 0.86 0.60 0.58 0.9183673  0.48  0.02  0.00
#> [2,]   NA   NA 0.82 0.26 0.64 0.96 0.52 0.56 0.02 0.6122449  0.20  0.92  0.76
#> [3,]   NA   NA   NA 0.04 0.86 0.62 0.88 0.64 0.04 0.6938776  0.30  0.22  0.24
#> [4,]   NA   NA   NA   NA 0.56 0.28 0.98 0.42 0.66 0.5510204  0.54  0.42  0.50
#> [5,]   NA   NA   NA   NA   NA 0.24 0.66 0.46 0.60 0.5918367  0.20  0.46  0.12
#> [6,]   NA   NA   NA   NA   NA   NA 0.98 0.28 0.68 0.4897959  0.70  0.10  0.94
#>      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#> [1,]  0.74  0.58  0.50  0.38  0.20  0.50  0.58  0.42  0.30  0.00  0.06  0.90
#> [2,]  0.60  0.54  0.58  0.24  0.30  0.36  0.10  0.44  0.98  0.28  0.06  0.90
#> [3,]  0.68  0.38  0.72  0.18  0.44  0.26  0.40  0.48  0.58  0.30  0.20  0.84
#> [4,]  0.26  0.70  0.80  0.50  0.54  0.30  0.38  0.68  0.28  0.24  0.82  0.58
#> [5,]  0.96  0.80  0.86  0.96  0.72  0.78  0.42  0.76  0.74  0.10  0.76  0.40
#> [6,]  0.84  0.64  0.94  0.18  0.44  0.34  0.34  0.42  0.54  0.60  0.76  0.30
#>      [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
#> [1,]  0.38  0.40  0.72  0.14  0.72  0.04  0.62  0.36  0.18  0.94  0.04  0.86
#> [2,]  0.42  0.40  0.40  0.30  0.92  0.86  0.82  0.88  0.98  0.66  0.24  0.74
#> [3,]  0.34  0.70  0.42  0.30  0.70  0.66  0.50  0.10  0.24  0.82  0.02  0.56
#> [4,]  0.80  0.40  0.56  0.72  0.98  0.74  0.64  0.16  0.90  0.64  0.68  0.22
#> [5,]  0.70  0.84  0.54  0.54  0.38  0.76  0.64  0.48  0.06  0.26  0.38  0.58
#> [6,]  0.30  0.32  0.20  0.02  0.52  0.40  0.14  0.30  0.96  0.06  0.62  0.18
#>      [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49]
#> [1,]  0.20  0.80  0.98  0.28  0.56  1.00  0.98  0.54  0.22  0.84  1.00  0.86
#> [2,]  0.66  0.54  0.52  0.08  0.02  1.00  0.84  0.72  0.50  0.84  0.52  0.94
#> [3,]  0.42  0.84  0.86  0.48  0.02  1.00  0.88  0.24  0.14  0.36  0.70  0.58
#> [4,]  1.00  0.58  0.66  0.42  0.78  0.80  0.76  0.88  0.74  1.00  0.90  0.82
#> [5,]  0.84  0.10  0.52  0.10  0.36  0.74  0.70  0.80  0.42  0.98  0.82  0.84
#> [6,]  0.16  0.78  0.46  0.42  0.28  0.92  0.20  0.80  0.60  0.72  0.68  0.48
#>          [,50]
#> [1,] 0.2857143
#> [2,] 0.6122449
#> [3,] 0.3061224
#> [4,] 0.6122449
#> [5,] 0.1020408
#> [6,] 0.4489796