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 
#>  26  81 131  97  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.3176      0.56935       0.6557       0.5686       0.7948
#>        0.5866      0.34038       0.6143       0.5746       0.7097
#>        0.6205      0.56921       0.5533       0.1699       0.9049
#>        0.5548      0.68518       0.3228       0.5565       0.6844
#>        0.3052      0.09051       0.6452       0.5397       0.5408
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.4811
#> τ2   0.3098
#> τ3   0.4789
#> τ4   0.5353
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.16409
#> 0001 0.05140
#> 0010 0.01208
#> 0011 0.05345
#> 0100 0.09501
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22492.79 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5244
#> M2:  0.49
#> total scores:  0.6171
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]       [,2]      [,3]      [,4]
#> [1,] 0.3176423 0.56935453 0.6556858 0.5685947
#> [2,] 0.5866180 0.34038099 0.6142510 0.5746406
#> [3,] 0.6204877 0.56921088 0.5532851 0.1698577
#> [4,] 0.5548152 0.68517833 0.3227724 0.5564594
#> [5,] 0.3051780 0.09050959 0.6451712 0.5396716
#> [6,] 0.5678409 0.17589919 0.3046964 0.5800936

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8571429 0.8878571 0.9328571 0.9700000 0.9785714

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.5285714 0.6257143 0.7800000 0.8942857 0.9200000

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2229.594            NA 17716.58 1795.939 21742.11
#> D(theta_bar)   2169.645            NA 17038.69 1783.100 20991.44
#> DIC            2289.543            NA 18394.47 1808.778 22492.79
head(a$PPP_total_scores)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.96 0.98 0.52 0.10 0.96
#> [2,] 0.94 0.18 0.32 0.60 0.04
#> [3,] 0.90 0.40 1.00 0.28 1.00
#> [4,] 0.86 0.26 0.02 1.00 0.22
#> [5,] 0.48 0.60 0.38 0.94 0.84
#> [6,] 0.62 0.62 1.00 1.00 0.98
head(a$PPP_item_means)
#> [1] 0.56 0.60 0.58 0.54 0.50 0.52
head(a$PPP_item_ORs)
#>      [,1] [,2] [,3] [,4] [,5] [,6]      [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,]   NA  0.2 0.76 0.90 0.56 0.98 0.8367347 0.54 0.46  0.10  0.06  0.90  0.52
#> [2,]   NA   NA 0.76 0.70 0.78 0.50 0.8571429 0.14 0.52  0.52  0.56  0.42  0.78
#> [3,]   NA   NA   NA 0.64 0.44 0.56 0.8367347 0.40 0.40  0.14  0.48  0.26  0.68
#> [4,]   NA   NA   NA   NA 0.74 0.38 0.7551020 0.46 0.70  0.08  0.96  0.98  0.78
#> [5,]   NA   NA   NA   NA   NA 0.90 0.8979592 0.06 0.44  0.42  0.84  0.58  0.92
#> [6,]   NA   NA   NA   NA   NA   NA 0.7551020 0.50 0.92  0.54  0.54  0.48  0.90
#>      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
#> [1,]  0.26  0.38  0.52  0.60  0.86  0.94  0.06  0.76  0.80  0.28  0.44  0.08
#> [2,]  0.06  0.72  0.66  0.80  0.86  0.24  0.96  0.76  0.26  0.94  0.60  0.48
#> [3,]  0.78  0.70  0.36  0.42  0.86  0.44  0.18  0.56  0.54  0.08  0.22  0.36
#> [4,]  0.46  0.98  0.28  0.96  0.54  0.82  0.74  0.74  0.58  0.18  0.76  0.90
#> [5,]  0.18  0.98  0.94  0.88  0.90  0.84  0.82  0.36  0.68  0.38  0.36  0.68
#> [6,]  0.80  0.26  0.38  0.90  0.72  0.04  0.76  0.76  0.14  0.66  0.80  0.96
#>      [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
#> [1,]  0.34  0.32  0.46  0.76  0.64  0.48  0.82  0.98  0.64  0.88  0.82  0.54
#> [2,]  0.96  0.24  0.84  0.64  0.58  0.86  0.22  0.20  0.48  0.18  0.64  0.50
#> [3,]  0.36  0.52  0.64  0.74  0.28  0.38  0.14  0.94  0.82  0.46  0.88  0.20
#> [4,]  0.04  0.74  0.62  0.88  0.28  0.54  0.72  0.24  0.04  0.40  0.62  0.04
#> [5,]  0.74  0.38  0.42  0.18  0.82  0.94  0.60  0.84  0.14  0.84  0.74  0.56
#> [6,]  0.76  0.84  0.90  0.10  0.38  0.84  0.78  0.88  0.26  0.80  0.42  0.88
#>      [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49]
#> [1,]  0.12  0.64  0.08  0.32  0.64  0.86  0.98  0.94  0.20  0.62  0.92  0.80
#> [2,]  0.36  0.84  0.08  0.86  0.20  0.58  0.52  0.76  0.24  0.32  0.38  0.24
#> [3,]  0.26  0.80  0.64  0.60  0.34  0.28  0.40  0.88  0.38  0.36  0.58  0.36
#> [4,]  0.50  0.58  0.54  0.22  0.26  0.82  0.56  0.92  0.42  0.56  0.78  0.24
#> [5,]  0.18  0.42  0.64  1.00  0.44  0.82  0.14  0.66  0.56  0.90  0.90  0.80
#> [6,]  0.24  0.34  0.20  0.34  0.82  0.50  0.44  0.54  0.38  0.34  0.90  0.74
#>          [,50]
#> [1,] 0.3061224
#> [2,] 0.6938776
#> [3,] 0.2244898
#> [4,] 0.5306122
#> [5,] 0.4489796
#> [6,] 0.2857143