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 
#>  40 113 108  73  16
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.2201      0.63916       0.5136       0.6006       0.8725
#>        0.6384      0.26734       0.5350       0.6329       0.9119
#>        0.5041      0.51642       0.5014       0.2379       0.7484
#>        0.5130      0.54455       0.2144       0.6876       0.7460
#>        0.1747      0.08728       0.5567       0.5889       0.6286
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.4278
#> τ2   0.3184
#> τ3   0.2943
#> τ4   0.3873
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.10548
#> 0001 0.07632
#> 0010 0.03664
#> 0011 0.02147
#> 0100 0.04348
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22466.89 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4872
#> M2:  0.49
#> total scores:  0.6101
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]       [,2]      [,3]      [,4]
#> [1,] 0.2200505 0.63915789 0.5135897 0.6005675
#> [2,] 0.6383718 0.26733970 0.5350295 0.6329117
#> [3,] 0.5041279 0.51641669 0.5014147 0.2379158
#> [4,] 0.5130058 0.54455479 0.2144217 0.6876169
#> [5,] 0.1746550 0.08728403 0.5566902 0.5889067
#> [6,] 0.5617911 0.42562100 0.2734040 0.5995232

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8857143 0.9171429 0.9335714 0.9478571 0.9557143

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.5971429 0.7057143 0.7657143 0.8085714 0.8428571

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

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