Skip to content

These functions can be used to generate simulated data for supervised (classification and regression) and unsupervised modeling applications.

Usage

sim_classification(
  num_samples = 100,
  method = "caret",
  intercept = -5,
  num_linear = 10
)

sim_regression(
  num_samples = 100,
  method = "sapp_2014_1",
  std_dev = NULL,
  factors = FALSE
)

sim_noise(
  num_samples,
  num_vars,
  cov_type = "exchangeable",
  outcome = "none",
  num_classes = 2,
  cov_param = 0
)

Arguments

num_samples

Number of data points to simulate.

method

A character string for the simulation method. For classification, the single current option is "caret". For classification, values can be "sapp_2014_1", "sapp_2014_2", "van_der_laan_2007_1", or "van_der_laan_2007_2". See Details below.

intercept

The intercept for the linear predictor.

num_linear

Number of diminishing linear effects.

std_dev

Gaussian distribution standard deviation for residuals. Default values are shown below in Details.

factors

A single logical for whether the binary indicators should be encoded as factors or not.

num_vars

Number of noise predictors to create.

cov_type

The multivariate normal correlation structure of the predictors. Possible values are "exchangeable" and "toeplitz".

outcome

A single character string for what type of independent outcome should be simulated (if any). The default value of "none" produces no extra columns. Using "classification" will generate a class column with num_classes values, equally distributed. A value of "regression" results in an outcome column that contains independent standard normal values.

num_classes

When outcome = "classification", the number of classes to simulate.

cov_param

A single numeric value for the exchangeable correlation value or the base of the Toeplitz structure. See Details below.

Details

These functions provide several supervised simulation methods (and one unsupervised). Learn more by method:

method = "caret"

This is a simulated classification problem with two classes, originally implemented in caret::twoClassSim() with all numeric predictors. The predictors are simulated in different sets. First, two multivariate normal predictors (denoted here as two_factor_1 and two_factor_2) are created with a correlation of about 0.65. They change the log-odds using main effects and an interaction:

 intercept - 4 * two_factor_1 + 4 * two_factor_2 + 2 * two_factor_1 * two_factor_2 

The intercept is a parameter for the simulation and can be used to control the amount of class imbalance.

The second set of effects are linear with coefficients that alternate signs and have a sequence of values between 2.5 and 0.025. For example, if there were four predictors in this set, their contribution to the log-odds would be

 -2.5 * linear_1 + 1.75 * linear_2 -1.00 * linear_3 + 0.25 * linear_4

(Note that these column names may change based on the value of num_linear).

The third set is a nonlinear function of a single predictor ranging between [0, 1] called non_linear_1 here:

 (non_linear_1^3) + 2 * exp(-6 * (non_linear_1 - 0.3)^2) 

The fourth set of informative predictors are copied from one of Friedman's systems and use two more predictors (non_linear_2 and non_linear_3):

 2 * sin(non_linear_2 * non_linear_3) 

All of these effects are added up to model the log-odds.

method = "sapp_2014_1"

This regression simulation is from Sapp et al. (2014). There are 20 independent Gaussian random predictors with mean zero and a variance of 9. The prediction equation is:


predictor_01 + sin(predictor_02) + log(abs(predictor_03)) +
 predictor_04^2 + predictor_05 * predictor_06 +
 ifelse(predictor_07 * predictor_08 * predictor_09 < 0, 1, 0) +
 ifelse(predictor_10 > 0, 1, 0) + predictor_11 * ifelse(predictor_11 > 0, 1, 0) +
 sqrt(abs(predictor_12)) + cos(predictor_13) + 2 * predictor_14 + abs(predictor_15) +
 ifelse(predictor_16 < -1, 1, 0) + predictor_17 * ifelse(predictor_17 < -1, 1, 0) -
 2 * predictor_18 - predictor_19 * predictor_20

The error is Gaussian with mean zero and variance 9.

method = "sapp_2014_2"

This regression simulation is also from Sapp et al. (2014). There are 200 independent Gaussian predictors with mean zero and variance 16. The prediction equation has an intercept of one and identical linear effects of log(abs(predictor)).

The error is Gaussian with mean zero and variance 25.

method = "van_der_laan_2007_1"

This is a regression simulation from van der Laan et al. (2007) with ten random Bernoulli variables that have a 40% probability of being a value of one. The true regression equation is:


2 * predictor_01 * predictor_10 + 4 * predictor_02 * predictor_07 +
  3 * predictor_04 * predictor_05 - 5 * predictor_06 * predictor_10 +
  3 * predictor_08 * predictor_09 + predictor_01 * predictor_02 * predictor_04 -
  2 * predictor_07 * (1 - predictor_06) * predictor_02 * predictor_09 -
  4 * (1 - predictor_10) * predictor_01 * (1 - predictor_04)

The error term is standard normal.

method = "van_der_laan_2007_2"

This is another regression simulation from van der Laan et al. (2007) with twenty Gaussians with mean zero and variance 16. The prediction equation is:


predictor_01 * predictor_02 + predictor_10^2 - predictor_03 * predictor_17 -
 predictor_15 * predictor_04 + predictor_09 * predictor_05 + predictor_19 -
 predictor_20^2 + predictor_09 * predictor_08

The error term is also Gaussian with mean zero and variance 16.

sim_noise()

This function simulates a number of random normal variables with mean zero. The values can be independent if cov_param = 0. Otherwise the values are multivariate normal with non-diagonal covariance matrices. For cov_type = "exchangeable", the structure has unit variances and covariances of cov_param. With cov_type = "toeplitz", the covariances have an exponential pattern (see example below).

References

van der Laan, Mark J., Polley, Eric C and Hubbard, Alan E.. "Super Learner" Statistical Applications in Genetics and Molecular Biology, vol. 6, no. 1, 2007. DOI: 10.2202/1544-6115.1309.

Stephanie Sapp, Mark J. van der Laan & John Canny (2014) Subsemble: an ensemble method for combining subset-specific algorithm fits, Journal of Applied Statistics, 41:6, 1247-1259, DOI: 10.1080/02664763.2013.864263

Examples

set.seed(1)
sim_regression(100)
#> # A tibble: 100 × 21
#>    outcome predictor_01 predictor_02 predictor_03 predictor_04
#>      <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
#>  1    8.49       -1.88        -1.86        1.23          2.68 
#>  2   19.3         0.551        0.126       5.07         -3.14 
#>  3   57.7        -2.51        -2.73        4.76          5.91 
#>  4   -8.41        4.79         0.474      -0.993        -1.15 
#>  5   43.0         0.989       -1.96       -6.86          4.96 
#>  6   72.3        -2.46         5.30        7.49          4.54 
#>  7   -3.36        1.46         2.15        2.00          0.249
#>  8   -3.82        2.21         2.73        1.62          1.70 
#>  9   22.7         1.73         1.15       -0.0402       -3.07 
#> 10   25.7        -0.916        5.05        1.53          0.969
#> # … with 90 more rows, and 16 more variables: predictor_05 <dbl>,
#> #   predictor_06 <dbl>, predictor_07 <dbl>, predictor_08 <dbl>,
#> #   predictor_09 <dbl>, predictor_10 <dbl>, predictor_11 <dbl>,
#> #   predictor_12 <dbl>, predictor_13 <dbl>, predictor_14 <dbl>,
#> #   predictor_15 <dbl>, predictor_16 <dbl>, predictor_17 <dbl>,
#> #   predictor_18 <dbl>, predictor_19 <dbl>, predictor_20 <dbl>
sim_classification(100)
#> # A tibble: 100 × 16
#>    class  two_factor_1 two_factor_2 non_linear_1 non_linear_2 non_linear_3
#>    <fct>         <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
#>  1 class…      -2.27        -1.18         -0.245       0.888        0.354 
#>  2 class…      -0.537        0.420         0.621       0.170        0.374 
#>  3 class…       3.23         2.39          0.163       0.957        0.840 
#>  4 class…       3.42         0.236         0.110       0.788        0.134 
#>  5 class…       0.571       -0.100        -0.487       0.377        0.541 
#>  6 class…      -1.61        -0.0701        0.393       0.789        0.515 
#>  7 class…       2.74         2.51         -0.751       0.459        0.931 
#>  8 class…       0.0763      -0.679         0.307       0.220        0.771 
#>  9 class…       0.0591      -2.35          0.402       0.566        0.712 
#> 10 class…      -1.39        -2.10         -0.887       0.0154       0.0784
#> # … with 90 more rows, and 10 more variables: linear_01 <dbl>,
#> #   linear_02 <dbl>, linear_03 <dbl>, linear_04 <dbl>, linear_05 <dbl>,
#> #   linear_06 <dbl>, linear_07 <dbl>, linear_08 <dbl>, linear_09 <dbl>,
#> #   linear_10 <dbl>