Skip to content

Commit

Permalink
Remove nlmixr_data_simplify() in favor of rxode2::etTrans()
Browse files Browse the repository at this point in the history
  • Loading branch information
billdenney committed Mar 8, 2024
1 parent b35d141 commit b7dc51c
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 190 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(nlmixr_data_simplify)
export(nlmixr_object_simplify)
export(tar_nlmixr)
export(tar_nlmixr_multimodel)
Expand Down
72 changes: 0 additions & 72 deletions R/simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,75 +52,3 @@ nlmixr_object_simplify_zero_initial_helper <- function(object) {
}
object
}

#' Standardize and simplify data for nlmixr2 estimation
#'
#' This function is typically not needed by end users.
#'
#' The standardization keeps columns that rxode2 and nlmixr2 use along with the
#' covariates. Column order is standardized (rxode2 then nlmixr2 then
#' alphabetically sorted covariates), and rxode2 and nlmixr2 column names are
#' converted to lower case.
#'
#' @inheritParams nlmixr2est::nlmixr
#' @param object an nlmixr_ui object (e.g. the output of running
#' \code{nlmixr(object = model)}
#' @return The data with the nlmixr2 column lower case and on the left and the
#' covariate columns on the right and alphabetically sorted.
#' @family Simplifiers
#' @export
nlmixr_data_simplify <- function(data, object, table = list()) {
nlmixr_cols <-
c(
# rxode2 columns
c("id", "time", "amt", "rate", "dur", "evid", "cmt", "dvid", "ss", "ii", "addl"),
# nlmixr2 columns
c("dv", "mdv", "cens", "limit")
)
# nlmixr pays attention to the columns in a case-insensitive way for the
# standard columns. Verify that the data has case-insensitive column names
# for these columns (for example not "ADDL" and "addl").
mask_nlmixr_cols <- tolower(names(data)) %in% nlmixr_cols
nlmixr_names <- names(data)[mask_nlmixr_cols]
mask_duplicated <- duplicated(tolower(nlmixr_names))
if (any(mask_duplicated)) {
stop(
"The following column(s) are duplicated when lower case: ",
paste0("'", nlmixr_names[mask_duplicated], "'", collapse = ", ")
)
}
if (!is.null(object$ui)) {
covVec <- object$ui$all.covs
} else {
covVec <- object$all.covs
}
cov_names <- nlmixr_data_simplify_cols(data, cols = covVec, type = "covariate")
keep_names <- nlmixr_data_simplify_cols(data, cols = table$keep, type = "keep")
# Simplifying the nlmixr_names column names to always be lower case ensures
# that upper/lower case column name changes will not affect the need to rerun.
# Also, standardizing the column name order to always be the same will prevent
# the need to rerun, so cov_names is sorted.

# Sorting so that they are in order, unique so that duplication between
# covariates and keep do not try to duplicate columns in the output data.
add_col_names <- sort(unique(c(cov_names, keep_names)))

# Drop names from nlmixr_names from the added names
add_col_names <- setdiff(add_col_names, nlmixr_names)

stats::setNames(
object = data[, c(nlmixr_names, add_col_names), drop = FALSE],
nm = c(tolower(nlmixr_names), add_col_names)
)
}

nlmixr_data_simplify_cols <- function(data, cols, type) {
missing_col <- setdiff(cols, names(data))
if (length(missing_col) > 0) {
stop(
"The following ", type, " column(s) are missing from the data: ",
paste0("'", missing_col, "'", collapse = ", ")
)
}
cols
}
8 changes: 4 additions & 4 deletions R/tar_nlmixr.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#' `paste(name, "data_simple", sep = "_tar_")` (e.g. "pheno_tar_data_simple") as
#' the simplified data object.
#'
#' For the way that the objects are simplified, see `nlmixr_object_simplify()`
#' and `nlmixr_data_simplify()`. To see how to write initial conditions to work
#' with targets, see `nlmixr_object_simplify()`.
#' For the way that the objects are simplified, see `nlmixr_object_simplify()`.
#' To see how to write initial conditions to work with targets, see
#' `nlmixr_object_simplify()`.
#'
#' @inheritParams nlmixr2est::nlmixr
#' @inheritParams targets::tar_target
Expand Down Expand Up @@ -86,7 +86,7 @@ tar_nlmixr_raw <- function(name, object, data, est, control, table, object_simpl
name = data_simple_name,
command =
substitute(
nlmixr_data_simplify(object = object_simple, data = data, table = table),
rxode2::etTrans(inData = data, obj = object_simple, keep = table$keep),
list(
object_simple = as.name(object_simple_name),
data = data,
Expand Down
35 changes: 0 additions & 35 deletions man/nlmixr_data_simplify.Rd

This file was deleted.

4 changes: 0 additions & 4 deletions man/nlmixr_object_simplify.Rd

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

6 changes: 3 additions & 3 deletions man/tar_nlmixr.Rd

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

71 changes: 0 additions & 71 deletions tests/testthat/test-simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,6 @@ model_simple <-
nlmixr_object_simplify(pheno)
))

test_that("nlmixr_data_simplify", {
# Columns are kept in the correct order
expect_equal(
names(nlmixr_data_simplify(data = nlmixr2data::pheno_sd, object = model_simple)),
c("id", "time", "amt", "dv", "mdv", "evid", "WT")
)
# table's 'keep' argument is respected
expect_equal(
names(nlmixr_data_simplify(
data = nlmixr2data::pheno_sd,
object = model_simple,
table = nlmixr2est::tableControl(keep = "APGR")
)),
c("id", "time", "amt", "dv", "mdv", "evid", "APGR", "WT")
)
# duplication between table's 'keep' argument and covariates does not
# duplicate columns
expect_equal(
names(nlmixr_data_simplify(
data = nlmixr2data::pheno_sd,
object = model_simple,
table = nlmixr2est::tableControl(keep = "WT")
)),
c("id", "time", "amt", "dv", "mdv", "evid", "WT")
)
# duplication between table's 'keep' argument and nlmixr2 columns does not add
# them
expect_equal(
names(nlmixr_data_simplify(
data = nlmixr2data::pheno_sd,
object = model_simple,
table = nlmixr2est::tableControl(keep = "EVID")
)),
c("id", "time", "amt", "dv", "mdv", "evid", "WT")
)
})

test_that("nlmixr_data_simplify expected errors", {
bad_data_lower_case <- nlmixr2data::pheno_sd
bad_data_lower_case$id <- bad_data_lower_case$ID
expect_error(
nlmixr_data_simplify(data = bad_data_lower_case, object = model_simple),
regexp = "The following column(s) are duplicated when lower case: 'id'",
fixed = TRUE
)
bad_data_no_cov <- nlmixr2data::pheno_sd
bad_data_no_cov$WT <- NULL
expect_error(
nlmixr_data_simplify(data = bad_data_no_cov, object = model_simple),
regexp = "The following covariate column(s) are missing from the data: 'WT'",
fixed = TRUE
)
})

test_that("nlmixr_object_simplify_zero_initial", {
pheno <- function() {
ini({
Expand Down Expand Up @@ -115,20 +61,3 @@ test_that("nlmixr_object_simplify_zero_initial", {
new_model <- nlmixr_object_simplify_zero_initial(pheno)
expect_equal(body(new_model), body(pheno_0))
})

test_that("re-estimating a model works with covariates (#9)", {
bad_data_lower_case <- nlmixr2data::pheno_sd
bad_data_lower_case$id <- bad_data_lower_case$ID
fit_estimated <-
suppressMessages(
nlmixr2est::nlmixr(
object = model_simple,
data = nlmixr2data::pheno_sd,
est = "focei",
control = list(eval.max = 1)
)
)
expect_true(
"WT" %in% names(nlmixr_data_simplify(data = nlmixr2data::pheno_sd, object = fit_estimated))
)
})

0 comments on commit b7dc51c

Please sign in to comment.