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 
#>  33 104 142  56  15
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.1090       0.6497       0.6664       0.5431       0.7346
#>        0.5105       0.1389       0.6202       0.6858       0.9074
#>        0.5665       0.6455       0.6599       0.2906       0.7193
#>        0.5811       0.6295       0.1843       0.6234       0.7037
#>        0.1642       0.3118       0.5275       0.6264       0.6543
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.5237
#> τ2   0.3333
#> τ3   0.3879
#> τ4   0.2244
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.10047
#> 0001 0.08954
#> 0010 0.05301
#> 0011 0.05931
#> 0100 0.05846
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22157.27 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5124
#> M2:  0.49
#> total scores:  0.6161
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]      [,2]      [,3]      [,4]
#> [1,] 0.1089692 0.6496760 0.6663546 0.5430807
#> [2,] 0.5105329 0.1389175 0.6201826 0.6858008
#> [3,] 0.5665165 0.6455004 0.6598620 0.2905530
#> [4,] 0.5810751 0.6294537 0.1843273 0.6234479
#> [5,] 0.1641884 0.3117803 0.5275295 0.6264469
#> [6,] 0.5695720 0.2016084 0.5605426 0.5370223

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8607143 0.9050000 0.9378571 0.9478571 0.9578571

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.5457143 0.6714286 0.7800000 0.8200000 0.8514286

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

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