The latent-variable simulators generate data with known latent
structure: latent profiles (LPA) and classes (LCA), linear regression,
factor models, and sequence clusters governed by per-cluster Markov
chains. Each returns a saqr_sim object exposing
$data and the explicit ground-truth $params,
so parameter recovery can be checked directly.
simulate_lpa()Generate continuous-indicator data with known latent profile structure. All distributional parameters are specified explicitly so that recovery can be verified.
Signature
Example
means <- matrix(c(0, 0, 10, 10), nrow = 2, ncol = 2)
r <- simulate_lpa(means = means, sds = 0.5, props = c(0.5, 0.5), n = 200, seed = 1)
head(r$data)str(r$params)
#> List of 3
#> $ means: num [1:2, 1:2] 0 0 10 10
#> $ sds : num [1:2, 1:2] 0.5 0.5 0.5 0.5
#> $ props: num [1:2] 0.5 0.5lab <- r$data[[grep("class|profile|true", names(r$data), value = TRUE)[1]]]
plot(r$data[[1]], r$data[[2]], col = as.integer(factor(lab)), pch = 19,
xlab = names(r$data)[1], ylab = names(r$data)[2],
main = "simulate_lpa(): two latent profiles")First two indicators coloured by true profile.
simulate_lca()Generate binary-indicator data with known latent class structure. All item response probabilities are specified explicitly.
Signature
Example
item_probs <- matrix(c(0.9, 0.1, 0.9, 0.1,
0.1, 0.9, 0.1, 0.9), nrow = 4, ncol = 2)
r <- simulate_lca(item_probs = item_probs, class_probs = c(0.5, 0.5),
n = 200, seed = 1)
head(r$data)simulate_regression()Generate a linear-regression dataset with fully specified
ground-truth coefficients and predictor standard deviations, designed so
that lm(y ~ ., data = r$data) recovers the true
coefficients at large n.
Signature
Example
coefs <- c("(Intercept)" = 2, x1 = 3, x2 = -1)
r <- simulate_regression(coefs = coefs,
predictor_sds = c(x1 = 1, x2 = 1),
error_sd = 0.5, n = 500, seed = 42)
head(r$data)coef(lm(y ~ ., data = r$data)) # should be close to c(2, 3, -1)
#> (Intercept) x1 x2
#> 1.9798791 3.0200245 -0.9995026
str(r$params)
#> List of 3
#> $ coefs : Named num [1:3] 2 3 -1
#> ..- attr(*, "names")= chr [1:3] "(Intercept)" "x1" "x2"
#> $ predictor_sds: Named num [1:2] 1 1
#> ..- attr(*, "names")= chr [1:2] "x1" "x2"
#> $ error_sd : num 0.5simulate_fa()Generate multivariate normal data from an explicit factor model. The implied covariance matrix \(\Sigma = \Lambda \Phi \Lambda' + \Psi\) is fully specified and stored for direct comparison with estimated covariances.
Signature
Example
# Two orthogonal factors, 6 indicators (3 per factor)
loadings <- matrix(c(0.8, 0.7, 0.6, 0, 0, 0,
0, 0, 0, 0.8, 0.7, 0.6),
nrow = 6, ncol = 2) # byrow = FALSE (default)
r <- simulate_fa(loadings = loadings, n = 500, seed = 1)
head(r$data) # y1..y6r$params$sigma_implied # implied covariance matrix
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 1.00 0.56 0.48 0.00 0.00 0.00
#> [2,] 0.56 1.00 0.42 0.00 0.00 0.00
#> [3,] 0.48 0.42 1.00 0.00 0.00 0.00
#> [4,] 0.00 0.00 0.00 1.00 0.56 0.48
#> [5,] 0.00 0.00 0.00 0.56 1.00 0.42
#> [6,] 0.00 0.00 0.00 0.48 0.42 1.00simulate_seq_clusters()Generate wide-format sequence data where each row is drawn from one of K Markov chains (clusters), each governed by its own transition matrix. Matrices can be supplied explicitly or generated at random.
Signature
args(simulate_seq_clusters)
#> function (trans_list = NULL, props = NULL, n = 300L, seq_length = 20L,
#> init_probs = NULL, n_clusters = 3L, n_states = 10L, states = NULL,
#> seed = NULL)
#> NULLExample
m1 <- matrix(c(0.8, 0.2, 0.3, 0.7), nrow = 2, byrow = TRUE,
dimnames = list(c("A","B"), c("A","B")))
m2 <- matrix(c(0.2, 0.8, 0.7, 0.3), nrow = 2, byrow = TRUE,
dimnames = list(c("A","B"), c("A","B")))
r <- simulate_seq_clusters(trans_list = list(m1, m2), n = 100, seed = 1)
head(r$data)str(r$params)
#> List of 3
#> $ trans_list:List of 2
#> ..$ : num [1:2, 1:2] 0.8 0.3 0.2 0.7
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : chr [1:2] "A" "B"
#> .. .. ..$ : chr [1:2] "A" "B"
#> ..$ : num [1:2, 1:2] 0.2 0.7 0.8 0.3
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : chr [1:2] "A" "B"
#> .. .. ..$ : chr [1:2] "A" "B"
#> $ props : num [1:2] 0.5 0.5
#> $ init_probs:List of 2
#> ..$ : Named num [1:2] 0.5 0.5
#> .. ..- attr(*, "names")= chr [1:2] "A" "B"
#> ..$ : Named num [1:2] 0.5 0.5
#> .. ..- attr(*, "names")= chr [1:2] "A" "B"