Skip to content

Commit

Permalink
move data-raw/helpers.R into package
Browse files Browse the repository at this point in the history
clean up license
update targets pipelines to depend on package
  • Loading branch information
yonicd committed Feb 7, 2024
1 parent 8603de6 commit ee42dc9
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ coverage.*
init.sh
workflows.md
images
^\_targets\.R$
^\_targets$
^\_targets\.yaml$
^README\.Rmd$
^data-raw$
^LICENSE\.md$
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Version: 0.0.1
Date: 2023-11-09
Authors@R: person("Jonathan","Sidi" , , "[email protected]", role = c("aut", "cre"))
Description: Reproducible simulated clinical trials for use in R packages.
License: Apache License 2.0 | file LICENSE
License: MIT + file LICENSE
URL: https://github.com/openpharma/clindata
BugReports: https://github.com/openpharma/clindata/issues
Depends: R (>= 4.0)
Imports: tools, utils
Imports: tools, utils, MASS, clusterGeneration
Suggests:
knitr,
withr,
Expand Down
15 changes: 2 additions & 13 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
Copyright 2022 F. Hoffmann-La Roche AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
YEAR: 2024
COPYRIGHT HOLDER: clindata authors
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2024 clindata authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export(cache_dir)
export(cache_init)
export(cache_ls)
export(cache_rm)
export(compute_unstructured_matrix)
export(generate_outcomes)
export(list_data)
export(load_data)
export(mar)
export(mcar)
export(read_data)
43 changes: 19 additions & 24 deletions data-raw/pipelines/helpers.R → R/simulations.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#' @importFrom stats cov2cor plogis rbinom
#' @importFrom clusterGeneration genPositiveDefMat
#' @importFrom MASS mvrnorm

# Helper function for randomly generating unstructured covariance matrix.
#' @export
compute_unstructured_matrix <- function(
vars = seq(from = 1, by = 0.5, length.out = 10)) {
n_visits <- length(vars)
Expand All @@ -14,19 +11,32 @@ compute_unstructured_matrix <- function(
return(us_mat)
}

pad_number <- function(x, width = 2, pad = "0") {
fmt <- sprintf("%%%s%ss", pad, width)
sprintf(fmt, x)
}

# MCAR helper function.
#' @export
mcar <- function(data, col, p) {
missing_vec <- stats::rbinom(nrow(data), 1, p)
data[missing_vec == 1, col] <- NA_real_
data
}

# Helper function for generating data.
#' @export
generate_outcomes <- function(model_mat, cov_mat, effect_coefs, effect_var) {

# Generate the outcomes.
n_visits <- nrow(cov_mat)
n_obs <- nrow(model_mat) / n_visits

rbetas <- MASS::mvrnorm(1, effect_coefs, diag(effect_var))
mu <- as.numeric(model_mat %*% rbetas)

resid <- as.numeric(MASS::mvrnorm(n_obs, rep(0, n_visits), cov_mat))
y_hat <- mu + resid
as.vector(y_hat)
}

# MAR helper function.
#' @export
mar <- function(data, type, fn = NULL) {
# Compute missingness probabilities.

Expand All @@ -51,18 +61,3 @@ mar <- function(data, type, fn = NULL) {
# Only keep non-missing visits.
data[missing_ind == 0, ]
}

# Helper function for generating data.
generate_outcomes <- function(model_mat, cov_mat, effect_coefs, effect_var) {

# Generate the outcomes.
n_visits <- nrow(cov_mat)
n_obs <- nrow(model_mat) / n_visits

rbetas <- MASS::mvrnorm(1, effect_coefs, diag(effect_var))
mu <- as.numeric(model_mat %*% rbetas)

resid <- as.numeric(MASS::mvrnorm(n_obs, rep(0, n_visits), cov_mat))
y_hat <- mu + resid
as.vector(y_hat)
}
2 changes: 1 addition & 1 deletion data-raw/_targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library(targets)
# Set target options:
tar_option_set(
# packages that your targets need to run
packages = c("usethis", "tibble", "dplyr", "stats", "clusterGeneration", "MASS")
packages = c("usethis", "tibble", "dplyr", "stats", "clindata")
)

pipelines_path <- here::here("data-raw/pipelines")
Expand Down
11 changes: 5 additions & 6 deletions data-raw/pipelines/bcva/bcva.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
source(here::here("data-raw/pipelines/bcva/bcva-helpers.R"))
source(here::here("data-raw/pipelines/helpers.R"))

# BCVA data-generating process.

Expand All @@ -15,7 +14,7 @@ pipe_bcva <- list(
)
}),
# Generate Covariate Matrix
tar_target(bcva_outcome_covar_mat, compute_unstructured_matrix()),
tar_target(bcva_outcome_covar_mat, clindata::compute_unstructured_matrix()),
# Generate the covariates.
tar_target(bcva_covars_tbl, {
bcva_generate_covariates(
Expand All @@ -25,7 +24,7 @@ pipe_bcva <- list(
}),
# Generate the outcomes.
tar_target(bcva_outcomes, {
generate_outcomes(
clindata::generate_outcomes(
model_mat = bcva_model_mat(bcva_covars_tbl),
cov_mat = bcva_outcome_covar_mat,
effect_coefs = bcva_coefs(
Expand All @@ -48,18 +47,18 @@ pipe_bcva <- list(
}),
# Delete observations at random.
tar_target(bcva_tbl_missing, {
mar(bcva_tbl, type = bcva_scenario$missing_type, bcva_lin_pred)
clindata::mar(bcva_tbl, type = bcva_scenario$missing_type, bcva_lin_pred)
}),
# Format to resemble BCVA dataset.
tar_target(bcva_data, {
bcva_tbl_missing |>
dplyr::transmute(
USUBJID = factor(participant),
VISITN = visit_num,
AVISIT = paste0("VIS", pad_number(visit_num)),
AVISIT = paste0("VIS", sprintf("%02s", visit_num)),
AVISIT = factor(
AVISIT,
levels = paste0("VIS", pad_number(seq_len(10)))
levels = paste0("VIS", sprintf("%02s", seq_len(10)))
),
ARMCD = ifelse(trt == 1, "TRT", "CTL"),
RACE = ifelse(strata == 1, "Black",
Expand Down
7 changes: 3 additions & 4 deletions data-raw/pipelines/fev/fev.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source(here::here('data-raw/pipelines/helpers.R'))
source(here::here('data-raw/pipelines/fev/fev-helpers.R'))

# FEV1 data-generating process.
Expand All @@ -16,7 +15,7 @@ pipe_fev <- list(
tar_target(fev_outcome_covar_mat, {
v11 <- rnorm(1, 40, 0.1)
vars <- v11 * c(1, 2/3, 1/3, 5/2)
compute_unstructured_matrix(vars)
clindata::compute_unstructured_matrix(vars)
}),
# Generate the covariates.
tar_target(fev_covars_tbl, {
Expand All @@ -27,7 +26,7 @@ pipe_fev <- list(
}),
# Generate the outcomes.
tar_target(fev_outcomes, {
generate_outcomes(
clindata::generate_outcomes(
model_mat = fev_model_mat(fev_covars_tbl),
cov_mat = fev_outcome_covar_mat,
effect_coefs = fev_coefs(trt_coef = fev_scenario$trt_coef),
Expand All @@ -43,7 +42,7 @@ pipe_fev <- list(
}),
# Delete observations at random.
tar_target(fev_data, {
mcar(fev_tbl, "FEV1", fev_scenario$missing_percent)
clindata::mcar(fev_tbl, "FEV1", fev_scenario$missing_percent)
}),
tar_target(fev_deploy, {
usethis::use_data(fev_data, overwrite = TRUE)
Expand Down

0 comments on commit ee42dc9

Please sign in to comment.