Functions to generate simulated pre/post test data from known LCA parameters for validation and parameter recovery studies. Simulate Pre-Post Test Data (No DK Model)
simulate_lca(
n,
n_items = 1,
gg = 0.35,
gk = 0.3,
kk = 0.35,
gamma = 0.25,
difficulty = NULL,
base_rate = 0.25,
seed = NULL,
return_classes = FALSE
)Integer. Number of individuals to simulate.
Integer. Number of test items. Default 1.
Numeric. Proportion in guess->guess state (stable ignorance). Default 0.35.
Numeric. Proportion in guess->know state (LEARNED). Default 0.30.
Numeric. Proportion in know->know state (stable knowledge). Default 0.35.
Numeric. Probability of guessing correctly. Can be scalar (same for all items) or vector of length n_items. Default 0.25.
Numeric vector. Optional difficulty-link scores. If provided, gamma is computed as base_rate + (1 - base_rate) * plogis(-difficulty). Higher difficulty = harder item (lower gamma). Ignored if NULL.
Numeric. Minimum guessing probability (random chance). Used when difficulty is specified. Default 0.25 (1/4 for 4-choice items).
Optional integer. Random seed for reproducibility.
Logical. If TRUE, also return true latent class assignments. Default FALSE for backward compatibility.
List with components:
Data frame of pre-test responses (0/1 for each item)
Data frame of post-test responses (0/1 for each item)
(If return_classes=TRUE) Factor with levels "gg", "gk", "kk"
(If return_classes=TRUE) Logical vector: TRUE if individual is in gk class
Generates simulated pre/post test data from a latent class model with known parameters. Useful for parameter recovery validation studies.
The model simulates three latent classes: - **gg (guess->guess)**: Don't know at both times. Responses are random guesses. - **gk (guess->know)**: Learned between tests. Random guess pre, correct post. - **kk (know->know)**: Know at both times. Correct responses at both times.
Parameters must satisfy: gg + gk + kk = 1 (constraint enforced automatically).
When difficulty is specified, gamma values are derived using a logistic transformation: gamma_i = base_rate + (1 - base_rate) * plogis(-difficulty_i). This means: - difficulty = 0: gamma = base_rate + 0.5 * (1 - base_rate) (middle) - difficulty -> +Inf: gamma -> base_rate (hard item, random guessing) - difficulty -> -Inf: gamma -> 1 (easy item, always correct)
# Simulate data with 30% learning
sim <- simulate_lca(n = 500, gg = 0.35, gk = 0.30, kk = 0.35, gamma = 0.25, seed = 123)
fit <- item_lca_fit(sim$pre, sim$post)
fit$params["gk", ] # Should be close to 0.30
#> [1] 0.2746931
# Multi-item simulation
sim_multi <- simulate_lca(n = 500, n_items = 3, seed = 456)
# Item-specific gamma (vector)
sim_vec <- simulate_lca(n = 500, n_items = 3, gamma = c(0.2, 0.25, 0.3), seed = 789)
# Difficulty-link scores
sim_irt <- simulate_lca(n = 500, n_items = 3, difficulty = c(1, 0, -1), seed = 101)
# Return true class assignments for validation
sim_classes <- simulate_lca(n = 500, gk = 0.30, seed = 123, return_classes = TRUE)
table(sim_classes$true_class)
#>
#> gg gk kk
#> 180 143 177
mean(sim_classes$learned) # Should be close to 0.30
#> [1] 0.286