Skip to contents

Simulate correlated regressor values

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 1 3 -0.08888801 -0.1189768 -1.926488
#> 2 2 3 -2.31672614 -0.9196652 -1.345979
#> 3 2 3  0.14146463  0.4040866 -1.947112
#> 4 2 2 -2.86028032 -1.0175959 -1.195731
#> 5 0 3 -1.32352049 -0.6800921 -1.761985
#> 6 0 3 -4.03115485 -0.2170961 -1.695337

Dirichlet distribution

x <- c(0.5, 0.3, 0.2)
concentration <- 1:3

# compute density
ddirichlet(x = x, concentration = concentration)
#> [1] 0.72
ddirichlet(x = x, concentration = concentration, log = TRUE)
#> [1] -0.3285041

# sample
rdirichlet(concentration = 1:3)
#> [1] 0.2042475 0.5823353 0.2134172
rdirichlet(n = 4, concentration = 1:2)
#>           [,1]      [,2]
#> [1,] 0.5540008 0.4459992
#> [2,] 0.5017851 0.4982149
#> [3,] 0.2623289 0.7376711
#> [4,] 0.1016348 0.8983652