NIDA_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 NIDA model

Test_versions_sim <- sample(1:5,N,replace = L)
nClass = 2^K
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 
#>  36 100 124  76  14
Gmats <- array(NA,c(Jt,K,L))
Smats <- array(NA,c(Jt,K,L))
for(k in 1:K){
  Smats[,k,] <- runif(1,.1,.3)
  Gmats[,k,] <- runif(1,.1,.3)
}

Y_sim = simNIDA(Alphas,Smats[1,,1],Gmats[1,,1],Q_matrix,Test_order,Test_versions_sim)

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

output_NIDA_indept = hmcdm(Y_sim,Q_matrix,"NIDA_indept",Test_order,Test_versions_sim,100,30,
                                    R = R)
#> 0
output_NIDA_indept
#> 
#> Model: NIDA_indept 
#> 
#> Sample Size: 350
#> Number of Items: 50
#> Number of Time Points: 5 
#> 
#> Chain Length: 100, burn-in: 30
summary(output_NIDA_indept)
#> 
#> Model: NIDA_indept 
#> 
#> Item Parameters:
#>  ss_EAP gs_EAP
#>  0.2424 0.2760
#>  0.1575 0.1549
#>  0.1933 0.1779
#>  0.1217 0.1556
#> 
#> Transition Parameters:
#>    taus_EAP
#> τ1   0.3153
#> τ2   0.3642
#> τ3   0.2490
#> τ4   0.4943
#> 
#> Class Probabilities:
#>      pis_EAP
#> 0000 0.05127
#> 0001 0.07635
#> 0010 0.07829
#> 0011 0.04397
#> 0100 0.11364
#>    ... 11 more classes
#> 
#> Deviance Information Criterion (DIC): 21857.38 
#> 
#> Posterior Predictive P-value (PPP):
#> M1: 0.494
#> M2:  0.49
#> total scores:  0.6143
a <- summary(output_NIDA_indept)
head(a$ss_EAP)
#>           [,1]
#> [1,] 0.2424173
#> [2,] 0.1575449
#> [3,] 0.1933310
#> [4,] 0.1217117

(3) Check for parameter estimation accuracy

AAR_vec <- numeric(L)
for(t in 1:L){
  AAR_vec[t] <- mean(Alphas[,,t]==a$Alphas_est[,,t])
}
AAR_vec
#> [1] 0.8928571 0.9107143 0.9442857 0.9585714 0.9664286

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.6457143 0.6885714 0.7914286 0.8457143 0.8714286

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

a$DIC
#>              Transition Response_Time Response    Joint    Total
#> D_bar          2225.892            NA 17080.30 1871.502 21177.69
#> D(theta_bar)   2131.043            NA 16492.45 1874.514 20498.00
#> DIC            2320.740            NA 17668.15 1868.490 21857.38
head(a$PPP_total_scores)
#>           [,1]      [,2]      [,3]       [,4]      [,5]
#> [1,] 0.4428571 0.1000000 0.5857143 0.91428571 0.9714286
#> [2,] 0.5714286 0.8285714 0.1571429 0.05714286 0.6857143
#> [3,] 0.6142857 0.9857143 0.2428571 0.37142857 0.3000000
#> [4,] 0.4857143 0.7000000 0.5142857 0.51428571 0.9714286
#> [5,] 0.7714286 0.7000000 0.3571429 0.28571429 0.8428571
#> [6,] 0.5428571 0.6000000 0.7285714 0.70000000 0.2428571
head(a$PPP_item_means)
#> [1] 0.28571429 0.70000000 0.22857143 0.08571429 0.18571429 0.34285714
head(a$PPP_item_ORs)
#>      [,1]       [,2] [,3]      [,4]      [,5]      [,6]      [,7]      [,8]
#> [1,]   NA 0.04285714  0.7 0.4571429 0.1714286 0.4142857 0.2857143 0.9000000
#> [2,]   NA         NA  0.4 0.3857143 0.1285714 0.7571429 0.2428571 0.2714286
#> [3,]   NA         NA   NA 0.3857143 0.3285714 0.4428571 0.4285714 0.5857143
#> [4,]   NA         NA   NA        NA 0.8000000 0.3857143 0.1000000 0.7428571
#> [5,]   NA         NA   NA        NA        NA 0.2142857 0.5142857 0.8285714
#> [6,]   NA         NA   NA        NA        NA        NA 0.5428571 0.8142857
#>           [,9]     [,10]     [,11]     [,12]      [,13]     [,14]     [,15]
#> [1,] 0.4142857 0.6285714 0.9714286 0.4714286 0.35714286 0.9571429 0.7142857
#> [2,] 0.2714286 0.6142857 0.9571429 0.6428571 0.51428571 0.5857143 0.1857143
#> [3,] 0.7000000 0.6571429 0.3000000 0.8714286 0.17142857 0.5857143 0.4285714
#> [4,] 0.4285714 0.8142857 0.4142857 0.6571429 0.71428571 0.5571429 0.8428571
#> [5,] 0.8428571 0.7571429 0.2571429 0.5000000 0.15714286 0.8571429 0.2428571
#> [6,] 0.3857143 0.7000000 0.5857143 0.7428571 0.08571429 0.6428571 0.2000000
#>          [,16]     [,17]      [,18]     [,19]     [,20]      [,21]      [,22]
#> [1,] 0.4000000 0.1571429 0.84285714 0.8142857 0.1714286 1.00000000 0.58571429
#> [2,] 0.0000000 0.0000000 0.95714286 0.7000000 0.6571429 0.52857143 0.50000000
#> [3,] 0.2714286 0.6571429 0.11428571 0.1571429 1.0000000 0.67142857 0.31428571
#> [4,] 0.8571429 0.5000000 0.02857143 0.7428571 0.5000000 0.58571429 0.35714286
#> [5,] 0.8142857 0.2714286 0.44285714 0.5142857 0.3000000 0.80000000 0.01428571
#> [6,] 0.8000000 0.2428571 0.17142857 0.5857143 0.9142857 0.01428571 0.38571429
#>           [,23]     [,24]      [,25]     [,26]     [,27]      [,28]     [,29]
#> [1,] 0.10000000 0.9714286 0.91428571 0.8000000 0.6571429 0.81428571 0.6857143
#> [2,] 0.28571429 0.4285714 0.38571429 0.4857143 0.1571429 0.04285714 0.2714286
#> [3,] 0.25714286 0.1285714 0.05714286 0.5000000 0.7142857 0.82857143 0.9428571
#> [4,] 0.80000000 0.9142857 0.82857143 0.8285714 0.7857143 1.00000000 0.9857143
#> [5,] 0.08571429 0.1428571 0.72857143 0.4000000 0.5571429 0.04285714 0.2000000
#> [6,] 0.40000000 0.9857143 0.38571429 0.4285714 0.4142857 0.35714286 0.5285714
#>           [,30]     [,31]      [,32]     [,33]     [,34]     [,35]      [,36]
#> [1,] 0.90000000 0.2571429 0.95714286 0.8285714 0.9000000 0.9571429 0.44285714
#> [2,] 0.12857143 0.1714286 0.05714286 0.6571429 0.1000000 0.4000000 0.20000000
#> [3,] 0.78571429 0.8428571 0.88571429 0.3285714 0.7571429 0.9857143 0.92857143
#> [4,] 0.81428571 0.1857143 0.95714286 0.6571429 0.3857143 0.1285714 0.82857143
#> [5,] 0.01428571 0.7571429 0.08571429 0.5000000 0.5428571 0.4428571 0.38571429
#> [6,] 0.85714286 0.5571429 0.17142857 0.3285714 0.1000000 0.2857143 0.05714286
#>           [,37]      [,38]      [,39]      [,40]     [,41]     [,42]      [,43]
#> [1,] 0.84285714 0.70000000 0.57142857 0.92857143 0.0000000 0.8285714 0.57142857
#> [2,] 0.81428571 0.28571429 0.02857143 0.05714286 0.6714286 0.6857143 0.02857143
#> [3,] 0.02857143 0.92857143 0.38571429 0.38571429 0.7571429 0.9714286 0.77142857
#> [4,] 0.81428571 0.74285714 0.61428571 0.10000000 0.5571429 0.7000000 0.44285714
#> [5,] 0.25714286 0.04285714 0.31428571 0.92857143 0.8857143 0.4285714 0.11428571
#> [6,] 0.20000000 0.21428571 0.01428571 0.57142857 0.3571429 0.1142857 0.24285714
#>          [,44]      [,45]     [,46]     [,47]      [,48]     [,49]      [,50]
#> [1,] 0.7714286 0.04285714 0.2857143 0.9714286 0.61428571 0.8000000 0.98571429
#> [2,] 0.8571429 0.71428571 0.5428571 0.1571429 0.41428571 0.1714286 0.88571429
#> [3,] 0.9142857 0.47142857 0.9428571 0.7571429 0.65714286 0.3571429 0.87142857
#> [4,] 0.6857143 0.61428571 0.2000000 0.3000000 0.50000000 0.1571429 0.01428571
#> [5,] 0.4000000 0.14285714 0.1428571 0.7428571 0.07142857 0.1142857 0.67142857
#> [6,] 0.7714286 0.28571429 0.7857143 0.3000000 0.45714286 0.6857143 0.40000000