# \donttest{
# --- Example: Simulation (Mixed Categories GRT) ---
set.seed(42)
N <- 500; J <- 16
# Define Groups (4 Testlets)
groups <- list(c(1:4), c(5:8), c(9:12), c(13:16))
# Define Categories (Binary, 3-cat, 4-cat, Mixed)
# Items 1-4: 2 cats; 5-8: 3 cats; 9-12: 4 cats; 13-16: mixed
cats <- c(rep(2, 4), rep(3, 4), rep(4, 4), 3, 5, 3, 5)
# 1. Generate Parameters
theta <- rnorm(N)
# Gamma for 4 testlets (SD = 0.8)
gamma <- matrix(rnorm(N * 4, 0, 0.8), N, 4)
a <- rlnorm(J, 0, 0.2)
b_list <- vector("list", J)
# Generate Thresholds based on category count
for(j in 1:J) {
n_thresh <- cats[j] - 1
if(n_thresh == 1) {
b_list[[j]] <- rnorm(1)
} else {
# Spread thresholds
b_list[[j]] <- sort(rnorm(1) + seq(-1, 1, length.out=n_thresh))
}
}
# 2. Generate Responses (GRT Logic)
resp <- matrix(NA, N, J)
colnames(resp) <- paste0("Item_", 1:J)
for(i in 1:N) {
for(j in 1:J) {
# Identify Testlet ID
tid <- which(sapply(groups, function(x) j %in% x))
eff <- theta[i] + gamma[i, tid]
# Calculate Probabilities (Graded Response)
K <- cats[j]
probs <- numeric(K)
P_prev <- 1
for(k in 1:(K-1)) {
term <- a[j] * (eff - b_list[[j]][k])
P_star <- 1 / (1 + exp(-term))
probs[k] <- P_prev - P_star
P_prev <- P_star
}
probs[K] <- P_prev
# Sample Response
resp[i, j] <- sample(0:(K-1), 1, prob = probs)
}
}
df_sim <- as.data.frame(resp)
# 3. Run Estimation
fit <- trt_poly(
data = df_sim,
group = groups,
model = "GRT",
method = "EM",
control = list(max_iter = 20, verbose = FALSE)
)
head(fit$item_params)
head(fit$person_params)
# }
Run the code above in your browser using DataLab