The statistical simulators generate datasets with fully specified ground-truth parameters for the most common analyses: two-group comparisons, one-way ANOVA, correlated multivariate normal data, Gaussian-mixture clusters, and regression prediction. Each function returns a saqr_sim object with $data (a tidy data.frame) and $params (the true parameters used to generate it), so you can verify that an estimator recovers the truth.

simulate_ttest()

Generate data for a two-group comparison with fully specified ground-truth means and standard deviations, designed so that t.test(score ~ group) recovers the true difference at large n.

Signature

args(simulate_ttest)
#> function (n_a, n_b, mean_a, mean_b, sd_a = 1, sd_b = sd_a, labels = c("A", 
#>     "B"), seed = NULL) 
#> NULL

Example

r <- simulate_ttest(n_a = 50, n_b = 50, mean_a = 100, mean_b = 105, seed = 1)
head(r$data)
t.test(score ~ group, data = r$data)
#> 
#>  Welch Two Sample t-test
#> 
#> data:  score by group
#> t = -27.787, df = 95.793, p-value < 2.2e-16
#> alternative hypothesis: true difference in means between group A and group B is not equal to 0
#> 95 percent confidence interval:
#>  -5.375269 -4.658487
#> sample estimates:
#> mean in group A mean in group B 
#>        100.1004        105.1173
r$params
#> $mean_a
#> [1] 100
#> 
#> $mean_b
#> [1] 105
#> 
#> $sd_a
#> [1] 1
#> 
#> $sd_b
#> [1] 1
#> 
#> $n_a
#> [1] 50
#> 
#> $n_b
#> [1] 50
#> 
#> $cohens_d
#> [1] 5

simulate_anova()

Generate data for a one-way ANOVA with fully specified group means and standard deviations, designed so that aov(score ~ group) recovers the true group effects at large n.

Signature

args(simulate_anova)
#> function (n, means, sds = 1, labels = NULL, seed = NULL) 
#> NULL

Example

r <- simulate_anova(n = 30, means = c(10, 12, 15), seed = 1)
head(r$data)
summary(aov(score ~ group, data = r$data))
#>             Df Sum Sq Mean Sq F value Pr(>F)    
#> group        2  383.5   191.7   238.8 <2e-16 ***
#> Residuals   87   69.9     0.8                   
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
r$params
#> $means
#> G1 G2 G3 
#> 10 12 15 
#> 
#> $sds
#> G1 G2 G3 
#>  1  1  1 
#> 
#> $n
#> G1 G2 G3 
#> 30 30 30 
#> 
#> $labels
#> [1] "G1" "G2" "G3"
#> 
#> $eta_squared
#> [1] 0.8137045

simulate_correlation()

Generate multivariate normal data from an explicit correlation (or covariance) matrix, designed so that cor(r$data) recovers sigma at large n.

Signature

args(simulate_correlation)
#> function (n, sigma, means = NULL, var_names = NULL, seed = NULL) 
#> NULL

Example

R <- matrix(c(1, 0.6, 0.3,
              0.6, 1, 0.5,
              0.3, 0.5, 1), nrow = 3)
r <- simulate_correlation(n = 200, sigma = R, seed = 1)
head(r$data)
cor(r$data)            # should approximate R
#>           x1        x2        x3
#> x1 1.0000000 0.5556851 0.3162869
#> x2 0.5556851 1.0000000 0.4764833
#> x3 0.3162869 0.4764833 1.0000000
r$params$sigma         # the true matrix
#>      [,1] [,2] [,3]
#> [1,]  1.0  0.6  0.3
#> [2,]  0.6  1.0  0.5
#> [3,]  0.3  0.5  1.0

simulate_clusters()

Generate multivariate data from a mixture of Gaussians with fully specified cluster centers and standard deviations — ground truth is known, so clustering algorithms can be benchmarked against it.

Signature

args(simulate_clusters)
#> function (n, centers, sds = 1, props = NULL, seed = NULL) 
#> NULL

Example

centers <- matrix(c(0, 0,
                    5, 5,
                    10, 0), nrow = 3, byrow = TRUE)
r <- simulate_clusters(n = 300, centers = centers, seed = 1)
head(r$data)
str(r$params)
#> List of 4
#>  $ centers: num [1:3, 1:2] 0 5 10 0 5 0
#>  $ sds    : num [1:3, 1:2] 1 1 1 1 1 1
#>  $ props  : num [1:3] 0.333 0.333 0.333
#>  $ n      : Named int [1:3] 100 100 100
#>   ..- attr(*, "names")= chr [1:3] "cluster_1" "cluster_2" "cluster_3"
plot(x2 ~ x1, data = r$data, col = r$data$true_cluster, pch = 19,
     main = "simulate_clusters(): three Gaussian clusters")
First two columns coloured by true cluster.

First two columns coloured by true cluster.

simulate_prediction()

Generate a regression dataset with continuous and categorical predictors, a non-linear term, and known ground-truth coefficients — a richer version of simulate_regression() for testing prediction workflows.

Signature

args(simulate_prediction)
#> function (n, coefs, cat_levels = NULL, cat_effects = NULL, error_sd = 1, 
#>     predictor_means = NULL, predictor_sds = NULL, seed = NULL) 
#> NULL

Example

r <- simulate_prediction(
  n = 200,
  coefs = c("(Intercept)" = 5, x1 = 2, x2 = -1),
  cat_levels = list(treatment = c("control", "drug_a", "drug_b")),
  cat_effects = list(treatment = c(0, 3, 5)),
  error_sd = 2, seed = 42
)
head(r$data)
summary(lm(y ~ ., data = r$data))
#> 
#> Call:
#> lm(formula = y ~ ., data = r$data)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.6238 -1.2751  0.0521  1.3570  4.5064 
#> 
#> Coefficients:
#>                 Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)       4.9896     0.2391  20.866  < 2e-16 ***
#> x1                1.8857     0.1389  13.578  < 2e-16 ***
#> x2               -1.0033     0.1447  -6.933 5.89e-11 ***
#> treatmentdrug_a   3.0066     0.3356   8.958 2.65e-16 ***
#> treatmentdrug_b   4.6081     0.3322  13.870  < 2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.897 on 195 degrees of freedom
#> Multiple R-squared:  0.6975, Adjusted R-squared:  0.6913 
#> F-statistic: 112.4 on 4 and 195 DF,  p-value: < 2.2e-16
str(r$params)
#> List of 6
#>  $ coefs          : Named num [1:3] 5 2 -1
#>   ..- attr(*, "names")= chr [1:3] "(Intercept)" "x1" "x2"
#>  $ cat_effects    :List of 1
#>   ..$ treatment: num [1:3] 0 3 5
#>  $ error_sd       : num 2
#>  $ predictor_means: Named num [1:2] 0 0
#>   ..- attr(*, "names")= chr [1:2] "x1" "x2"
#>  $ predictor_sds  : Named num [1:2] 1 1
#>   ..- attr(*, "names")= chr [1:2] "x1" "x2"
#>  $ r_squared      : num 0.693