Generates synthetic classification data by drawing independent multivariate normal samples for each class. Useful when you want explicit control over each class's mean and covariance structure.
Arguments
- means
A list of numeric vectors, one per class, specifying the class means. Each vector must have length equal to the number of features.
- covs
A list of covariance matrices, one per class.
- ns
A numeric vector of sample sizes, one per class.
- class_names
Optional character vector of class labels (length equal to
length(ns)). Defaults to"Class 1","Class 2", etc.- seed
Optional integer for reproducibility. The global random seed is restored after the call.
- noise_ratio
Numeric in [0, 1). Proportion of
sum(ns)to add as uniform background noise (randomly labeled).- test_ratio
Numeric in [0, 1). If greater than 0, generates an additional independent test dataset. This is not a split of the training data.
Value
If test_ratio == 0 (default): a data frame with a Sim class column
and feature columns (X1, X2, ...). If test_ratio > 0: a list with
$train and $test data frames.
Details
Test data
When test_ratio > 0, an additional independent test dataset is generated by
drawing fresh samples of size round(ns * test_ratio) for each class using the
same means and covs. This is not a split of the training data. The training
set has sum(ns) observations; the test set has sum(round(ns * test_ratio))
independently generated observations.
Examples
# \donttest{
means <- list(c(0, 0), c(3, 3), c(0, 5))
covs <- list(diag(2), diag(2), diag(2))
ns <- c(60, 60, 60)
train_df <- simu_n(means, covs, ns, seed = 1)
head(train_df)
#> Sim X1 X2
#> 1 Class 1 -2.40161776 -0.6264538
#> 2 Class 1 0.03924000 0.1836433
#> 3 Class 1 -0.68973936 -0.8356286
#> 4 Class 1 -0.02800216 1.5952808
#> 5 Class 1 0.74327321 0.3295078
#> 6 Class 1 -0.18879230 -0.8204684
# With independent test data
sim <- simu_n(means, covs, ns, seed = 1, test_ratio = 0.3)
nrow(sim$train) # 180
#> [1] 180
nrow(sim$test) # 54 (independently generated, not split from train)
#> [1] 54
# }