Skip to contents

This function simulates regressor values from various marginal distributions with custom correlations.

Usage

correlated_regressors(
  labels,
  n = 100,
  marginals = list(),
  correlation = diag(length(labels)),
  verbose = FALSE
)

Arguments

labels

[character()]
Unique labels for the regressors.

n

[integer(1)]
The number of values per regressor.

marginals

[list()]
Optionally marginal distributions for regressors. If not specified, standard normal marginal distributions are used.

Each list entry must be named according to a regressor label, and the following distributions are currently supported:

discrete distributions
  • Poisson: list(type = "poisson", lambda = ...)

  • categorical: list(type = "categorical", p = c(...))

continuous distributions
  • normal: list(type = "normal", mean = ..., sd = ...)

  • uniform: list(type = "uniform", min = ..., max = ...)

correlation

[matrix()]
A correlation matrix of dimension length(labels), where the (p, q)-th entry defines the correlation between regressor labels[p] and labels[q].

verbose

[logical(1)]
Print information about the simulated regressors?

Value

A data.frame with n rows and length(labels) columns.

References

This function heavily depends on the {SimMultiCorrData} package.

See also

Examples

labels <- c("P", "C", "N1", "N2", "U")
n <- 100
marginals <- list(
  "P" = list(type = "poisson", lambda = 2),
  "C" = list(type = "categorical", p = c(0.3, 0.2, 0.5)),
  "N1" = list(type = "normal", mean = -1, sd = 2),
  "U" = list(type = "uniform", min = -2, max = -1)
)
correlation <- matrix(
  c(1, -0.3, -0.1, 0, 0.5,
    -0.3, 1, 0.3, -0.5, -0.7,
    -0.1, 0.3, 1, -0.3, -0.3,
    0, -0.5, -0.3, 1, 0.1,
    0.5, -0.7, -0.3, 0.1, 1),
  nrow = 5, ncol = 5
)
data <- correlated_regressors(
  labels = labels, n = n, marginals = marginals, correlation = correlation
)
head(data)
#>   P C        N1         N2         U
#> 1 0 2 -2.675432 -0.1984833 -1.388726
#> 2 4 1 -0.848971  0.1171901 -1.136329
#> 3 1 3 -4.562365 -0.3521832 -1.711120
#> 4 2 3 -2.771756 -0.3023198 -1.645906
#> 5 3 2 -3.859895  1.1111917 -1.480093
#> 6 0 3 -1.256646 -1.1932266 -1.610647