Skip to content

Commit 0b6a3c9

Browse files
roninsightrxclaude
andcommitted
feat: support categorical conditional filtering in sample_covariates_mice
For categorical covariates, the `conditional` argument now accepts a vector of allowed category values (e.g. `list(SEX = c("F"))`) rather than a min/max range. Continuous covariates retain the existing `c(min, max)` range behavior. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8734970 commit 0b6a3c9

File tree

7 files changed

+62
-15
lines changed

7 files changed

+62
-15
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: irxforge
22
Title: Forging data for pharmacometric analyses
3-
Version: 0.0.0.9001
3+
Version: 0.0.0.9002
44
Authors@R: c(
55
person("Ron", "Keizer", email = "ron@insight-rx.com", role = c("cre", "aut")),
66
person("Michael", "McCarthy", email = "michael.mccarthy@insight-rx.com", role = "ctb"),

R/sample_covariates.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#' Sample covariates using a variety of methods
2+
#'
3+
#' Categorical covariates: all covariate sampling methods (except multi-variate
4+
#' normal) support sampling of categorical covariates as well as continuous.
5+
#' In the `mice` sampling method, the categorical covariates have to be
6+
#' provided specifically as a vector of `character` indicating the column names.
7+
#' If not provided, they will otherwise be treated as continuous variables and
8+
#' non-integer values may be sampled.
29
#'
310
#' @param method sampling method, one of `mvtnorm`, `bootstrap`, `mice`, or
411
#' `nhanes`. E.g. `list(AGE = c(60, 80), WT = c(70, 100))`.

R/sample_covariates_mice.R

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#' Sample covariates using multivariate imputation using chained equations
22
#' (mice)
3+
#'
4+
#' In contrast to sampling methods bootstrap and NHANES, categorical covariates
5+
#' need to be specified using the `cat_covs` argument, otherwise they will be
6+
#' treated as continuous.
37
#'
48
#' @param data data.frame (n x p) containing the original, observed,
59
#' time-invariant covariates (ID should not be included) that will be used to
@@ -8,8 +12,11 @@
812
#' covariates in orgCovs.
913
#' @param n_subjects number of simulated subjects, default is the number of
1014
#' subjects in the data.
11-
#' @param conditional list with conditional limits for sampled population, e.g.
12-
#' `list("WT" = c(40, 60), "BMI" = c(15, 25))`.
15+
#' @param conditional list with conditional limits for sampled population. For
16+
#' continuous covariates, specify a numeric vector of length 2 giving the
17+
#' `c(min, max)` range, e.g. `list("WT" = c(40, 60), "BMI" = c(15, 25))`.
18+
#' For categorical covariates (those listed in `cat_covs`), specify a character
19+
#' vector of the allowed category values, e.g. `list("SEX" = c("F"))`.
1320
#' @param cont_method method used to predict continuous covariates within mice,
1421
#' default is `pmm`.
1522
#' @param replicates number of multiple imputations replicates to sample.
@@ -61,11 +68,15 @@ sample_covariates_mice <- function(
6168
seed_covs <- names(conditional)
6269
pool_seed <- data
6370
for(key in seed_covs) {
64-
pool_seed <- dplyr::filter(
65-
pool_seed,
66-
.data[[key]] >= min(conditional[[key]]) &
67-
.data[[key]] <= max(conditional[[key]])
68-
)
71+
if(key %in% cat_covs) {
72+
pool_seed <- dplyr::filter(pool_seed, .data[[key]] %in% conditional[[key]])
73+
} else {
74+
pool_seed <- dplyr::filter(
75+
pool_seed,
76+
.data[[key]] >= min(conditional[[key]]) &
77+
.data[[key]] <= max(conditional[[key]])
78+
)
79+
}
6980
}
7081
mi_data[seed_covs] <- pool_seed[
7182
sample(1:nrow(pool_seed), n_subjects, replace = T), seed_covs

man/sample_covariates.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sample_covariates_bootstrap.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sample_covariates_mice.Rd

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-sample_covariates_mice.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ test_that("n_subjects controls the number of simulated rows", {
6060
expect_equal(nrow(out), 2)
6161
})
6262

63+
test_that("conditional argument filters categorical covariates by value", {
64+
dat <- data.frame(
65+
AGE = c(20, 30, 40, 50, 60),
66+
WT = c(45, 55, 65, 75, 85),
67+
SEX = c("M", "F", "F", "M", "F")
68+
)
69+
cndl <- list(SEX = c("F"))
70+
out <- sample_covariates_mice(
71+
data = dat,
72+
cat_covs = "SEX",
73+
conditional = cndl,
74+
n_subjects = 5,
75+
seed = 1
76+
)
77+
expect_true(all(as.character(out$SEX) == "F"))
78+
})
79+
6380
test_that("conditional argument filters data before sampling", {
6481
dat <- data.frame(
6582
AGE = c(20, 30, 40, 50, 60),

0 commit comments

Comments
 (0)