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 
#>  27  82 127  90  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.3081       0.5385       0.6212       0.5782       0.7310
#>        0.5001       0.3482       0.5509       0.5863       0.8390
#>        0.6712       0.6390       0.6901       0.2140       0.9042
#>        0.6484       0.5420       0.1269       0.6512       0.7376
#>        0.4346       0.2727       0.5536       0.6695       0.6713
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.5358
#> τ2   0.3648
#> τ3   0.5138
#> τ4   0.3337
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.12053
#> 0001 0.08251
#> 0010 0.03308
#> 0011 0.04807
#> 0100 0.12996
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22482.6 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4992
#> M2:  0.49
#> total scores:  0.6108
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]      [,2]      [,3]      [,4]
#> [1,] 0.3081118 0.5384539 0.6211834 0.5782396
#> [2,] 0.5001011 0.3481962 0.5508894 0.5863365
#> [3,] 0.6712041 0.6389907 0.6900689 0.2139740
#> [4,] 0.6484342 0.5419983 0.1268692 0.6512053
#> [5,] 0.4345869 0.2727459 0.5536478 0.6695411
#> [6,] 0.5818555 0.1747877 0.2873324 0.5116342

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8650000 0.9057143 0.9414286 0.9578571 0.9671429

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.5542857 0.6685714 0.7914286 0.8428571 0.8771429

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

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