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 
#>  34 107 135  60  14
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.2639       0.6337       0.5053       0.6014       0.7882
#>        0.5614       0.1759       0.6107       0.6420       0.6912
#>        0.5691       0.6889       0.5826       0.2728       0.8570
#>        0.6423       0.5356       0.2754       0.6794       0.7189
#>        0.1017       0.3603       0.5540       0.5713       0.5796
#>    ... 45 more items
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.1525
#> τ2   0.3176
#> τ3   0.4911
#> τ4   0.5699
#> 
#> Class Probabilities:
#>       pis_EAP
#> 0000 0.156438
#> 0001 0.009126
#> 0010 0.055847
#> 0011 0.031968
#> 0100 0.086305
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 22744.8 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.5052
#> M2:  0.49
#> total scores:  0.6124
a <- summary(output_rRUM_indept)
head(a$r_stars_EAP)
#>           [,1]      [,2]      [,3]      [,4]
#> [1,] 0.2639004 0.6337238 0.5053409 0.6014084
#> [2,] 0.5614006 0.1759072 0.6106623 0.6419966
#> [3,] 0.5691449 0.6888898 0.5825672 0.2728218
#> [4,] 0.6422957 0.5356001 0.2753518 0.6793860
#> [5,] 0.1017327 0.3603076 0.5539542 0.5713264
#> [6,] 0.6760022 0.3345030 0.2083058 0.5790801

(3) Check for parameter estimation accuracy

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

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8521429 0.9100000 0.9414286 0.9535714 0.9600000

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.5228571 0.7085714 0.8142857 0.8342857 0.8600000

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

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