Skip to contents

A helper function to generate synthetic covariates and a latent continuous response identical to the logic used within SimulationFramework. Covariates may be supplied directly via X_mat or drawn randomly via cov_draw_method; exactly one of the two must be non-NULL.

Usage

generate_covariate_dataset(
  n,
  p,
  cond_exp_func_model = c("linear", "nonlinear"),
  norm_sq_beta_vec = 1,
  X_mat = NULL,
  cov_draw_method = stats::rnorm,
  cov_draw_method_args = list(mean = 0, sd = 1)
)

Arguments

n

Integer. Sample size (number of rows).

p

Integer. Number of covariates (number of columns).

cond_exp_func_model

Character scalar. Either "linear" (latent response is a weighted linear combination of covariates) or "nonlinear" (Friedman 1991 function applied to the first five covariates; requires p >= 5).

norm_sq_beta_vec

Positive numeric scalar. The desired squared Euclidean norm of the coefficient vector, i.e. sum(beta^2). The coefficient vector (or the overall Friedman scale) is rescaled so that this quantity equals norm_sq_beta_vec. Default 1.

X_mat

Numeric matrix of dimensions n x p, or NULL (default). When supplied, this matrix is used directly as the covariate matrix and cov_draw_method must be NULL.

cov_draw_method

A function used to draw n * p i.i.d. covariate values, or NULL. The function must accept the number of draws as its first positional argument followed by any named arguments in cov_draw_method_args. Default stats::rnorm. Must be NULL when X_mat is supplied.

cov_draw_method_args

Named list of additional arguments forwarded to cov_draw_method beyond the sample-size first argument. Default is list(mean = 0, sd = 1).

Value

A list with two elements: X (a data frame of covariates) and y_cont (a numeric vector of the latent continuous response).

Details

Two conditional-expectation models are supported, both rescaled so that the latent response y_cont has a controlled signal magnitude:

"linear"

\(y_i = x_i^\top \beta\), with \(\beta\) a fixed evenly spaced sequence from \(1\) to \(-1\) across the \(p\) covariates (seq(1, -1, length.out = p)) — i.e. the first covariate gets the strongest positive effect, the last the strongest negative effect, and (for even \(p\)) covariates near the middle get effects near zero — rescaled so that \(\|\beta\|_2^2 = \code{norm\_sq\_beta\_vec}\).

"nonlinear"

The Friedman (1991) MARS benchmark function applied to the first five covariates (requires \(p \ge 5\); covariates beyond the fifth do not enter the response at all), $$y_i = c \left(10 \sin(\pi x_{i1} x_{i2}) + 20 (x_{i3} - 0.5)^2 + 10 x_{i4} + 5 x_{i5}\right),$$ with the overall scale \(c\) chosen so that \(c^2 \sum_k \beta_{\mathrm{friedman},k}^2 = \code{norm\_sq\_beta\_vec}\) for the nominal term coefficients \(\beta_{\mathrm{friedman}} = (10, 20, 10, 5)\) (the \(\sin\) term's coefficient is not part of this nominal vector, so the realized \(\|c \cdot (10,20,10,5)\|_2^2\) may not exactly equal norm_sq_beta_vec once the \(\sin\) term's own variance is accounted for — norm_sq_beta_vec calibrates the linear-in-covariate part of the scale, not the full response variance). This function assumes each covariate lies roughly in \([0, 1]\), matching Friedman's original specification (uniform covariates on \([0, 1]\)); covariates drawn or supplied on a very different scale, including the default stats::rnorm draws, will not reproduce the intended signal shape.

References

Friedman, J. H. (1991). "Multivariate Adaptive Regression Splines." The Annals of Statistics, 19(1), 1-67, doi:10.1214/aos/1176347963 , for the nonlinear benchmark function used when cond_exp_func_model = "nonlinear".

Examples

generate_covariate_dataset(n = 10, p = 5)
#> $X
#>              x1          x2         x3         x4          x5
#>           <num>       <num>      <num>      <num>       <num>
#>  1:  0.84434471 -0.26126488 -0.4423177 -0.2068205 -0.29116014
#>  2: -0.98107576  2.07378365 -1.6380077  0.7250432  0.77617245
#>  3: -0.13922141 -0.77883022 -0.6414012  1.3987190  0.29572563
#>  4:  2.18543791  1.13153251 -1.5570357 -1.5905551 -0.51719955
#>  5: -0.01282801 -0.42134513  1.9231637  1.3044971  1.75723302
#>  6: -0.30530893 -1.02174737 -1.8568296  0.1960117  0.16172682
#>  7: -0.58421346  1.21830538 -2.1061184 -0.3393434 -0.05468438
#>  8:  0.77126850 -1.79976067  0.6976485  1.2651441  0.52897160
#>  9:  2.10618936 -0.30824994  0.9074444  0.9397737  0.38973452
#> 10:  0.41215704  0.01551524 -0.1959882  1.2779525 -0.70577141
#> 
#> $y_cont
#>  [1]  0.7009395 -0.6848722 -0.9636862  2.5700977 -1.6652441 -0.6804686
#>  [7]  0.1576682 -0.8159660  0.6909216  0.3078223
#>