Skip to content

Commit

Permalink
Revert to use AVALCAT1 instead of FLAGSUM
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Sep 7, 2023
1 parent 73a1164 commit 685b01d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
19 changes: 9 additions & 10 deletions R/imputation_rule.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#' rule or `"1/2"` to implement 1/2 imputation rule.
#' @param post (`flag`)\cr whether the data corresponds to a post-dose time-point (defaults to `FALSE`).
#' This parameter is only used when `imp_rule` is set to `"1/3"`.
#' @param avalcat_flagvar (`character`)\cr name of variable that indicates whether or not a row in `df` is
#' flagged as `"BLQ"`, `"LTR"`, `"<PCLLOQ"`, or similar (defaults to `"FLAGSUM"`). Variable
#' `avalcat_flagvar` must be present in `df`.
#' @param avalcat_var (`character`)\cr name of variable that indicates whether a row in `df` corresponds
#' to an analysis value in category `"BLQ"`, `"LTR"`, `"<PCLLOQ"`, or none of the above
#' (defaults to `"AVALCAT1"`). Variable `avalcat_var` must be present in `df`.
#'
#' @return A `list` containing statistic value (`val`) and NA level (`na_level`) that should be displayed
#' according to the specified imputation rule.
Expand All @@ -26,32 +26,31 @@
#' AVAL = runif(50, 0, 1),
#' AVALCAT1 = sample(c(1, "BLQ"), 50, replace = TRUE)
#' )
#' df$FLAGSUM <- df$AVALCAT1 == "BLQ"
#' x_stats <- s_summary(df$AVAL)
#' imputation_rule(df, x_stats, "max", "1/3")
#' imputation_rule(df, x_stats, "geom_mean", "1/3")
#' imputation_rule(df, x_stats, "mean", "1/2")
#'
#' @export
imputation_rule <- function(df, x_stats, stat, imp_rule, post = FALSE, avalcat_flagvar = "FLAGSUM") {
checkmate::assert_choice(avalcat_flagvar, names(df))
imputation_rule <- function(df, x_stats, stat, imp_rule, post = FALSE, avalcat_var = "AVALCAT1") {
checkmate::assert_choice(avalcat_var, names(df))
checkmate::assert_choice(imp_rule, c("1/3", "1/2"))
flagsum <- sum(df[[avalcat_flagvar]])
flagsum_ratio <- flagsum / max(1, nrow(df))
n_blq <- sum(df[[avalcat_var]] %in% c("BLQ", "LTR", "<PCLLOQ"))
ltr_blq_ratio <- n_blq / max(1, nrow(df))

# defaults
val <- x_stats[[stat]]
na_level <- "NE"

if (imp_rule == "1/3") {
if (!post && stat == "geom_mean") val <- NA # 1/3_pre_LT, 1/3_pre_GT
if (flagsum_ratio > 1 / 3) {
if (ltr_blq_ratio > 1 / 3) {
if (stat != "geom_mean") na_level <- "ND" # 1/3_pre_GT, 1/3_post_GT
if (!post && !stat %in% c("median", "max")) val <- NA # 1/3_pre_GT
if (post && !stat %in% c("median", "max", "geom_mean")) val <- NA # 1/3_post_GT
}
} else if (imp_rule == "1/2") {
if (flagsum_ratio > 1 / 2 && !stat == "max") {
if (ltr_blq_ratio > 1 / 2 && !stat == "max") {
val <- NA # 1/2_GT
na_level <- "ND" # 1/2_GT
}
Expand Down
9 changes: 4 additions & 5 deletions man/imputation_rule.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/testthat/test-analyze_vars_in_cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ testthat::test_that("analyze_vars_in_cols works with imputation rule", {
VISIT = with_label(as.factor(rep(c(rep("Day 1", 5), rep("Day 2", 4)), 18)), "Visit"),
NFRLT = with_label(as.factor(rep(c(0, seq(0, 42, 6)), 18)), "Nominal Time")
)
df$FLAGSUM <- df$AVALCAT1 == "BLQ"

# 1/3 imputation rule
lyt <- basic_table() %>%
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-imputation_rule.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ df <- data.frame(
VISIT = with_label(as.factor(rep(c(rep("Day 1", 5), rep("Day 2", 4)), 18)), "Visit"),
NFRLT = with_label(as.factor(rep(c(0, seq(0, 42, 6)), 18)), "Nominal Time")
)
df$FLAGSUM <- df$AVALCAT1 == "BLQ"

testthat::test_that("imputation_rule works correctly for 1/3 imputation rule", {
x_stats <- s_summary(df$AVAL[df$NFRLT == 0])
Expand Down

0 comments on commit 685b01d

Please sign in to comment.