Skip to contents

Generate a coefficient vector with the specified dimensions and sparsity level.

Usage

generate_coef(.betas = NULL, .p = 1, .s = .p, .betas_name = "betas", ...)

Arguments

.betas

Coefficient vector or function to generate the coefficients. If a scalar is provided, the coefficient vector is a constant vector. If NULL (default), .s non-zero entries in the coefficient vector are equal to 0.5. If a function, must take one or more of n, .s, or .p, and can optionally take .betas_name, along with other user-defined args from ...) and must return a numeric vector of length 1, .s, or .p. If the function take n but not .s or .p, then .s is passed as n.

.p

Number of features.

.s

Sparsity level. Coefficients corresponding to features after the sth position (i.e., positions i = s + 1, ..., p) are set to 0.

.betas_name

Name of coefficient variable to use in error message.

...

Additional user arguments to pass to .betas when .betas is a function.

Value

A vector of length .p.

Examples

# generate constant beta = 0.5 of dimension 10
beta <- generate_coef(.p = 10)

# generate beta ~ N(1, 2) of dimension 10
beta <- generate_coef(rnorm, .p = 10, mean = 1, sd = 2)

# generate beta = [1, 1, 0, 0, 0]
beta <- generate_coef(.betas = 1, .p = 5, .s = 2)

# generate beta = [1, 2, 3]
beta <- generate_coef(.betas = 1:3, .p = 3)

# use a function to generate beta
beta_fun <- function(.s, df) {
  return(rt(n = .s, df = df))
}
beta <- generate_coef(.betas = beta_fun, .p = 10, .s = 3, df = 10)

# we can do the same without wrapping rt
beta <- generate_coef(.betas = rt, .p = 10, .s = 3, df = 10)