|
| 1 | +set.seed(123L) |
| 2 | + |
| 3 | +d_sim_placebo <- local({ |
| 4 | + |
| 5 | + emax_fn <- function(exposure, emax, ec50, e0, gamma = 1) { |
| 6 | + e0 + emax * (exposure^gamma) / (ec50^gamma + exposure^gamma) |
| 7 | + } |
| 8 | + |
| 9 | + # simulation model for the exposures includes a dosing model, but is very |
| 10 | + # simple. exposure scales linearly with dose, and has a slightly-truncated |
| 11 | + # lognormal distribution conditional on dose |
| 12 | + generate_exposure <- function(dose, n, meanlog = 4, sdlog = 0.5) { |
| 13 | + dose * stats::qlnorm( |
| 14 | + p = stats::runif(n, min = .01, max = .99), |
| 15 | + meanlog = meanlog, |
| 16 | + sdlog = sdlog |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + # continuous covariates all have the same range (0 to 10) and follow a |
| 21 | + # scaled beta distribution within that range |
| 22 | + continuous_covariate <- function(n) { |
| 23 | + stats::rbeta(n, 2, 2) * 10 |
| 24 | + } |
| 25 | + |
| 26 | + # binary covariates are Bernoulli variates |
| 27 | + binary_covariate <- function(n, p) { |
| 28 | + as.numeric(stats::runif(n) <= p) |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + # simulation functions ---------------------------------------------------- |
| 33 | + |
| 34 | + simulate_data <- function(seed = 123) { |
| 35 | + set.seed(seed) |
| 36 | + |
| 37 | + par <- list( |
| 38 | + # parameters for continuous response |
| 39 | + emax_1 = 10, |
| 40 | + ec50_1 = 4000, |
| 41 | + e0_1 = 5, |
| 42 | + gamma_1 = 1, |
| 43 | + sigma_1 = .5, |
| 44 | + coef_a1 = .5, |
| 45 | + coef_b1 = 0, |
| 46 | + coef_c1 = 0, |
| 47 | + coef_d1 = 0, |
| 48 | + |
| 49 | + # parameters for binary response |
| 50 | + emax_2 = 5, |
| 51 | + ec50_2 = 8000, |
| 52 | + e0_2 = -4.5, |
| 53 | + gamma_2 = 1, |
| 54 | + coef_a2 = .5, |
| 55 | + coef_b2 = 0, |
| 56 | + coef_c2 = 0, |
| 57 | + coef_d2 = 1 |
| 58 | + ) |
| 59 | + |
| 60 | + # conditional on a dose group, generate data; include multiple exposure metrics |
| 61 | + # but treat the first one as source of ground truth. exposures are strongly |
| 62 | + # correlated but on the same scale |
| 63 | + make_dose_data <- function(dose, n, par) { |
| 64 | + tibble::tibble( |
| 65 | + dose = dose, |
| 66 | + exposure_1 = generate_exposure(dose, n = n), |
| 67 | + exposure_2 = 0.7 * exposure_1 + 0.3 * generate_exposure(dose, n = n), |
| 68 | + |
| 69 | + # add continuous and binary covariates |
| 70 | + cnt_a = continuous_covariate(n = n), |
| 71 | + cnt_b = continuous_covariate(n = n), |
| 72 | + cnt_c = continuous_covariate(n = n), |
| 73 | + bin_d = binary_covariate(n = n, p = .5), |
| 74 | + bin_e = binary_covariate(n = n, p = .7), |
| 75 | + |
| 76 | + # response 1 is continuous |
| 77 | + response_1 = emax_fn( |
| 78 | + exposure_1, |
| 79 | + emax = par$emax_1, |
| 80 | + ec50 = par$ec50_1, |
| 81 | + e0 = par$e0_1, |
| 82 | + gamma = par$gamma_1 |
| 83 | + ) + |
| 84 | + par$coef_a1 * cnt_a + |
| 85 | + par$coef_b1 * cnt_b + |
| 86 | + par$coef_c1 * cnt_c + |
| 87 | + par$coef_d1 * bin_d + |
| 88 | + stats::rnorm(n, 0, par$sigma_1), |
| 89 | + |
| 90 | + # response 2 is binary; start with the predictor |
| 91 | + bin_pred = emax_fn( |
| 92 | + exposure_1, |
| 93 | + emax = par$emax_2, |
| 94 | + ec50 = par$ec50_2, |
| 95 | + e0 = par$e0_2, |
| 96 | + gamma = par$gamma_2 |
| 97 | + ) + |
| 98 | + par$coef_a2 * cnt_a + |
| 99 | + par$coef_b2 * cnt_b + |
| 100 | + par$coef_c2 * cnt_c + |
| 101 | + par$coef_d2 * bin_d, |
| 102 | + |
| 103 | + # convert |
| 104 | + bin_prob = 1 / (1 + exp(-bin_pred)), |
| 105 | + response_2 = as.numeric(stats::runif(n) < bin_prob) |
| 106 | + ) |> |
| 107 | + dplyr::select(-bin_pred, -bin_prob) # remove intermediate variables |
| 108 | + } |
| 109 | + |
| 110 | + # create data set assuming three dosing groups and placebo |
| 111 | + dat <- dplyr::bind_rows( |
| 112 | + make_dose_data(dose = 0, n = 100, par = par), |
| 113 | + make_dose_data(dose = 100, n = 100, par = par), |
| 114 | + make_dose_data(dose = 200, n = 100, par = par), |
| 115 | + make_dose_data(dose = 300, n = 100, par = par) |
| 116 | + ) |
| 117 | + |
| 118 | + return(dat) |
| 119 | + } |
| 120 | + |
| 121 | + # generate data ----------------------------------------------------------- |
| 122 | + |
| 123 | + d_sim_placebo <- simulate_data() |
| 124 | + d_sim_placebo <- d_sim_placebo |> |
| 125 | + dplyr::mutate(id = dplyr::row_number()) |> |
| 126 | + dplyr::relocate(response_1, response_2, .after = exposure_2) |> |
| 127 | + dplyr::relocate(id, 1) |> |
| 128 | + dplyr::rename( |
| 129 | + exp_1 = exposure_1, |
| 130 | + exp_2 = exposure_2, |
| 131 | + rsp_1 = response_1, |
| 132 | + rsp_2 = response_2 |
| 133 | + ) |
| 134 | + |
| 135 | + d_sim_placebo |
| 136 | +}) |
| 137 | + |
| 138 | +readr::write_csv(d_sim_placebo, "data-raw/d_sim_placebo.csv") |
| 139 | +usethis::use_data(d_sim_placebo, overwrite = TRUE) |
| 140 | + |
0 commit comments