rRUM_indept

library(hmcdm)

Load the spatial rotation data

N = length(Test_versions)
J = nrow(Q_matrix)
K = ncol(Q_matrix)
L = nrow(Test_order)
Jt = J/L

(1) Simulate responses and response times based on the rRUM model

nClass = 2^K
# Reachability matrix
R <- matrix(0,K,K)
tau <- numeric(K)
for(k in 1:K){
  tau[k] <- runif(1,.2,.6)
}
# 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)
    }
  }
}
# Subsequent Alphas
Alphas <- simulate_alphas_indept(tau,Alphas_0,L,R)
table(rowSums(Alphas[,,5]) - rowSums(Alphas[,,1])) # used to see how much transition has taken place
#> 
#>   0   1   2   3   4 
#>  37 103 134  65  11
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 <- matrix(apply((1-Smats)^Q_matrix, 1, prod), nrow=Jt, ncol=L, byrow=L)
Test_versions_sim <- sample(1:5,N,replace = L)

Y_sim = simrRUM(Alphas,r_stars,pi_stars,Q_matrix,Test_order,Test_versions_sim)

(2) Run the MCMC to sample parameters from the posterior distribution

output_rRUM_indept = hmcdm(Y_sim,Q_matrix,"rRUM_indept",Test_order,Test_versions_sim,100,30,
                                    R = R)
#> 0
output_rRUM_indept
#> 
#> Model: rRUM_indept 
#> 
#> Sample Size: 350
#> Number of Items: 50
#> Number of Time Points: 5 
#> 
#> Chain Length: 100, burn-in: 30
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.1344      0.67038       0.5444       0.6660       0.8354
#>        0.6410      0.30652       0.5578       0.5799       0.6158
#>        0.5458      0.60788       0.5163       0.2557       0.8497
#>        0.6569      0.52902       0.6882       0.5855       0.5663
#>        0.1330      0.09826       0.6053       0.5717       0.7373
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.4056
#> τ2   0.4954
#> τ3   0.5195
#> τ4   0.2235
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.05526
#> 0001 0.09155
#> 0010 0.01411
#> 0011 0.03174
#> 0100 0.08264
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22841.27 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.4991
#> M2:  0.49
#> total scores:  0.6069
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]       [,2]      [,3]      [,4]
#> [1,] 0.1344445 0.67037799 0.5443786 0.6659507
#> [2,] 0.6410099 0.30651588 0.5577718 0.5799090
#> [3,] 0.5457982 0.60787553 0.5162956 0.2557140
#> [4,] 0.6569161 0.52901933 0.6882437 0.5854822
#> [5,] 0.1329649 0.09825911 0.6052640 0.5716713
#> [6,] 0.5313663 0.42405103 0.1583775 0.6975747

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8592857 0.8971429 0.9314286 0.9585714 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.5485714 0.6714286 0.7457143 0.8428571 0.8800000

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          1959.504            NA 18304.56 1872.096 22136.16
#> D(theta_bar)   1876.758            NA 17687.21 1867.081 21431.05
#> DIC            2042.250            NA 18921.91 1877.111 22841.27
head(a$PPP_total_scores)
#>           [,1]       [,2]      [,3]      [,4]      [,5]
#> [1,] 0.9857143 0.02857143 0.3571429 0.3000000 0.4142857
#> [2,] 0.4428571 0.31428571 1.0000000 0.8571429 0.1000000
#> [3,] 0.8142857 0.92857143 0.9571429 0.4428571 0.5000000
#> [4,] 0.9142857 0.50000000 0.8142857 0.4714286 0.7571429
#> [5,] 0.8000000 0.40000000 0.9285714 0.8142857 0.2571429
#> [6,] 0.4428571 0.61428571 0.8857143 0.2428571 0.9285714
head(a$PPP_item_means)
#> [1] 0.5857143 0.4285714 0.5285714 0.5285714 0.4857143 0.4571429
head(a$PPP_item_ORs)
#>      [,1]      [,2]      [,3]      [,4]      [,5]       [,6]      [,7]
#> [1,]   NA 0.5285714 0.8428571 0.4857143 0.2714286 0.91428571 0.4285714
#> [2,]   NA        NA 0.5571429 0.5285714 0.2571429 0.30000000 0.5714286
#> [3,]   NA        NA        NA 0.6285714 0.9571429 0.54285714 0.1285714
#> [4,]   NA        NA        NA        NA 0.6714286 0.08571429 0.1714286
#> [5,]   NA        NA        NA        NA        NA 0.98571429 0.4428571
#> [6,]   NA        NA        NA        NA        NA         NA 0.2285714
#>           [,8]      [,9]     [,10]     [,11]     [,12]     [,13]      [,14]
#> [1,] 0.4571429 0.9428571 0.8857143 0.6857143 0.4714286 0.8714286 0.12857143
#> [2,] 0.4571429 0.7428571 0.4428571 0.3142857 0.1857143 0.1285714 0.30000000
#> [3,] 0.6142857 0.7285714 0.5000000 0.2142857 1.0000000 0.5571429 0.04285714
#> [4,] 0.2285714 0.5571429 0.4857143 0.6714286 0.7428571 0.5714286 0.57142857
#> [5,] 0.6714286 0.8857143 0.9714286 0.9000000 0.8428571 0.9714286 0.77142857
#> [6,] 0.1571429 0.6428571 0.9857143 0.2428571 0.4428571 0.3285714 0.20000000
#>          [,15]     [,16]     [,17]     [,18]     [,19]     [,20]     [,21]
#> [1,] 0.5714286 0.2714286 0.6857143 0.4142857 0.3428571 0.5857143 0.3000000
#> [2,] 0.7142857 0.2571429 0.4285714 0.5000000 0.8714286 0.8428571 0.6142857
#> [3,] 0.1857143 0.5428571 1.0000000 0.9428571 0.4571429 0.3428571 0.7428571
#> [4,] 0.9285714 0.4285714 0.8857143 0.8857143 0.9142857 0.1285714 0.2142857
#> [5,] 0.9428571 0.9571429 0.7000000 0.4857143 0.7714286 0.7000000 0.6428571
#> [6,] 0.4000000 0.7571429 0.6285714 0.5857143 0.7000000 0.1142857 0.9571429
#>          [,22]      [,23]     [,24]     [,25]     [,26]      [,27]      [,28]
#> [1,] 0.9714286 0.37142857 0.8000000 0.9857143 0.6285714 0.04285714 0.25714286
#> [2,] 0.8714286 0.62857143 0.6000000 0.6285714 0.7142857 0.68571429 0.07142857
#> [3,] 0.4857143 0.47142857 0.9857143 0.3571429 0.8428571 0.70000000 0.12857143
#> [4,] 0.5571429 0.85714286 0.9000000 0.5571429 0.3285714 0.27142857 0.00000000
#> [5,] 0.8857143 0.01428571 0.2000000 0.4285714 0.1857143 0.02857143 0.05714286
#> [6,] 0.4571429 0.47142857 0.2571429 0.9285714 0.1428571 0.18571429 0.10000000
#>          [,29]     [,30]      [,31]      [,32]      [,33]     [,34]      [,35]
#> [1,] 0.4428571 0.7571429 0.30000000 0.52857143 0.48571429 0.2285714 0.35714286
#> [2,] 0.5142857 0.3571429 0.05714286 0.40000000 0.72857143 0.4142857 0.64285714
#> [3,] 0.8428571 0.5857143 0.32857143 0.01428571 0.70000000 0.5857143 0.04285714
#> [4,] 0.4142857 0.9285714 0.28571429 0.91428571 0.00000000 0.2571429 0.01428571
#> [5,] 0.5571429 0.5714286 0.24285714 0.07142857 0.01428571 0.1571429 0.75714286
#> [6,] 0.5000000 0.3285714 0.31428571 0.25714286 0.52857143 0.1000000 0.41428571
#>           [,36]      [,37]      [,38]     [,39]     [,40]      [,41]      [,42]
#> [1,] 0.35714286 0.17142857 0.14285714 0.3857143 0.3428571 0.78571429 0.18571429
#> [2,] 0.11428571 0.18571429 0.08571429 0.3857143 0.2571429 0.42857143 0.47142857
#> [3,] 0.94285714 0.75714286 0.61428571 0.8857143 0.4714286 0.62857143 0.07142857
#> [4,] 0.07142857 0.21428571 0.52857143 0.3142857 0.4571429 0.02857143 0.82857143
#> [5,] 0.58571429 0.14285714 0.44285714 0.3285714 0.3857143 0.74285714 0.80000000
#> [6,] 0.87142857 0.01428571 0.35714286 0.9285714 0.9714286 0.01428571 0.11428571
#>          [,43]     [,44]      [,45]      [,46]     [,47]     [,48]      [,49]
#> [1,] 0.9714286 0.6714286 0.47142857 0.37142857 0.3857143 0.6000000 0.51428571
#> [2,] 0.3571429 0.7000000 0.70000000 0.55714286 0.2000000 0.5285714 0.52857143
#> [3,] 0.1285714 0.6285714 0.25714286 0.08571429 0.4857143 0.3285714 0.30000000
#> [4,] 0.2857143 0.8285714 0.04285714 0.50000000 0.4285714 0.5714286 0.48571429
#> [5,] 1.0000000 0.8714286 0.64285714 0.64285714 0.5428571 0.2714286 0.04285714
#> [6,] 0.6142857 0.9000000 0.31428571 0.55714286 0.5285714 0.5142857 0.55714286
#>           [,50]
#> [1,] 0.30000000
#> [2,] 0.45714286
#> [3,] 0.31428571
#> [4,] 0.02857143
#> [5,] 0.50000000
#> [6,] 0.28571429