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 
#>  23  99 136  80  12
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.4890       0.6650      0.55971       0.6216       0.6968
#>        0.5658       0.2544      0.58988       0.5436       0.8849
#>        0.5416       0.6930      0.59358       0.2736       0.8774
#>        0.6121       0.5087      0.08738       0.6412       0.9056
#>        0.2606       0.3813      0.60446       0.5671       0.7057
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.4126
#> τ2   0.3796
#> τ3   0.4450
#> τ4   0.3198
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.03897
#> 0001 0.05010
#> 0010 0.10378
#> 0011 0.03177
#> 0100 0.14896
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22661.85 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4984
#> M2:  0.49
#> total scores:  0.612
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]      [,2]       [,3]      [,4]
#> [1,] 0.4889528 0.6650032 0.55970922 0.6215847
#> [2,] 0.5658196 0.2543981 0.58987838 0.5436222
#> [3,] 0.5415788 0.6930347 0.59358192 0.2736228
#> [4,] 0.6121427 0.5087013 0.08737577 0.6411849
#> [5,] 0.2606450 0.3812726 0.60445549 0.5670881
#> [6,] 0.5947591 0.2192572 0.20598793 0.5736394

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8407143 0.8814286 0.9400000 0.9557143 0.9657143

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.5085714 0.6085714 0.7800000 0.8371429 0.8714286

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

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