# --- Example: Simulation (2PLT) ---
set.seed(2025)
n_persons <- 500
n_testlets <- 3
items_per_testlet <- 3
n_items <- n_testlets * items_per_testlet
# 1. Generate Parameters
# Discrimination (a): Varying -> 2PLT
a_true <- runif(n_items, 0.8, 1.5)
# Difficulty (b)
b_true <- seq(-1, 1, length.out = n_items)
# Testlet Variances (Sigma)
sigma_true <- c(1.0, 1.5, 2.0)
# 2. Generate Person Params
theta_true <- rnorm(n_persons, 0, 1)
gamma_matrix <- matrix(0, nrow = n_persons, ncol = n_testlets)
for(d in 1:n_testlets) {
gamma_matrix[, d] <- rnorm(n_persons, 0, sigma_true[d])
}
# 3. Generate Responses
resp_matrix <- matrix(0, nrow = n_persons, ncol = n_items)
colnames(resp_matrix) <- paste0("Item_", 1:n_items)
group_list <- list()
idx_counter <- 1
for(d in 1:n_testlets) {
indices <- idx_counter:(idx_counter + items_per_testlet - 1)
group_list[[d]] <- indices
for(i in indices) {
# 2PLT Model: a * (theta + gamma - b)
lin <- a_true[i] * (theta_true + gamma_matrix[, d] - b_true[i])
prob <- 1 / (1 + exp(-lin))
resp_matrix[, i] <- rbinom(n_persons, 1, prob)
}
idx_counter <- idx_counter + items_per_testlet
}
df_sim <- as.data.frame(resp_matrix)
# 4. Run Estimation
# We use "2PLT" because data was generated with varying 'a'
res <- trt_binary(
data = df_sim,
group = group_list,
model = "2PLT",
method = "EM",
control = list(max_iter = 20, verbose = FALSE)
)
head(res$item_params)
head(res$person_params)
Run the code above in your browser using DataLab