diff --git a/AGENTS.md b/AGENTS.md index f49d925..dd30ee6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,9 +45,9 @@ remotes::install_github("InsightRX/PKPDsim") The main entry point is `get_map_estimates()` (`R/get_map_estimates.R`). Two separate arguments are easy to confuse: -- **`type`** selects the *estimation approach*. It is **not validated** - anywhere: only these values are special-cased, and any other string falls - through to MAP-like behavior without an error. +- **`type`** selects the *estimation approach*. It is validated via + `match.arg()` against exactly these three values (anything else, including + a different case such as `"MAP"`, errors): - `"map"` (default) — standard MAP Bayesian / empirical Bayes. Uses `calc_ofv_map()`, which adds the prior penalty term. - `"pls"` — penalized least squares: MAP with very flat priors. @@ -60,9 +60,11 @@ separate arguments are easy to confuse: with finite-difference gradients; for models containing step functions (e.g. a lagtime) prefer a non-gradient method such as Nelder-Mead. -Note: `README.md` documents these as `map` / `map_flat_prior` / `ls`. The -special-cased values are `map` / `pls` / `ls` — treat the source as -authoritative. +`README.md` documents the same three values. A caller that previously passed +anything else (e.g. `"map_flat_prior"`, `"no_fit"`) to get `"map"`-like +behavior by fallthrough must now pass `"map"` explicitly — `match.arg()` +rejects it instead. (insightrxr's `fit_wrapper.R` is one such caller; see its +`resolve_map_estimates_type()`/inline equivalent.) Prior strength is controlled independently by **`weight_prior`**, which is given on the SD scale and squared into `weight_prior_var`. Throughout the diff --git a/DESCRIPTION b/DESCRIPTION index 4776bda..16c39e2 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,6 +12,6 @@ Depends: utils, stats Imports: PKPDsim, numDeriv, mvtnorm Suggests: testthat (>= 3.0.0) Remotes: InsightRX/PKPDsim -RoxygenNote: 7.3.3 Encoding: UTF-8 Config/testthat/edition: 3 +Config/roxygen2/version: 8.1.1 diff --git a/NAMESPACE b/NAMESPACE index b11408f..0f60116 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,15 +16,19 @@ export(plot_eta) export(replace_list_elements) export(run_sequential_map) export(weight_by_time) -importFrom(stats,coef) -importFrom(stats,cov) -importFrom(stats,cov2cor) -importFrom(stats,dnorm) -importFrom(stats,filter) -importFrom(stats,glm) -importFrom(stats,lm) -importFrom(stats,predict.lm) -importFrom(stats,weights) -importFrom(utils,setTxtProgressBar) -importFrom(utils,tail) -importFrom(utils,txtProgressBar) +importFrom(stats, + coef, + cov, + cov2cor, + dnorm, + filter, + glm, + lm, + predict.lm, + weights +) +importFrom(utils, + setTxtProgressBar, + tail, + txtProgressBar +) diff --git a/R/get_map_estimates.R b/R/get_map_estimates.R index 9f29bed..444834a 100755 --- a/R/get_map_estimates.R +++ b/R/get_map_estimates.R @@ -49,7 +49,7 @@ #' such as `Nelder-Mead` to avoid estimation failures. #' @param control list of options passed to `optim()` function #' @param allow_obs_before_dose allow observation before first dose? -#' @param type estimation type, options are `map` and `ls` +#' @param type estimation type, one of `"map"` (default), `"pls"`, or `"ls"` #' @param cols column names #' @param residuals show residuals? This requires an additional simulation so #' will be slightly slower. @@ -123,7 +123,7 @@ get_map_estimates <- function( method = "BFGS", control = list(reltol = 1e-5), allow_obs_before_dose = FALSE, - type = "map", + type = c("map", "pls", "ls"), cols = list(x = "t", y = "y"), residuals = TRUE, verbose = FALSE, @@ -131,6 +131,8 @@ get_map_estimates <- function( skip_hessian = FALSE, output_include = list(covariates = FALSE, parameters = FALSE)) { + type <- match.arg(type) + ## get prior weight for scaling of variance term weight_prior_var <- parse_weight_prior(weight_prior, type) diff --git a/README.md b/README.md index 6175250..3be65b5 100755 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ MAP Bayesian fitting for PK(PD) models. ## Description -This R library implements various methods for fitting of individual PK(PD) data. For simulation of PK(PD) profiles it uses the `PKPDsim` [package](https://github.com/insightrx/pkpdsim). The following estimation methods are implemented and available in the `get_map_estimates` function: +This R library implements various methods for fitting of individual PK(PD) data. For simulation of PK(PD) profiles it uses the `PKPDsim` [package](https://github.com/insightrx/pkpdsim). The following estimation methods are implemented and available in the `get_map_estimates` function via the `type` argument: -- `map`: MAP Bayesian: standard maximum a priori estimation of empirical Bayes estimates -- `map_flat_prior`: MAP Bayesian with flat prior: reduced weight of prior distribution, i.e. will put more trust in the data and hence reduce shrinkage (user can control the weighting). -- `ls`: Least Squares: not *truly* least squares but rather special case of MAP Bayesian with flat priors, i.e. with (almost) fully flat prior. +- `map`: MAP Bayesian: standard maximum a priori estimation of empirical Bayes estimates. For MAP estimation with flattened priors, please use the `weight_prior` argument. +- `pls`: An approximate implementation of penalized least squares, or more precisely, MAP Bayesian with a flattened prior. The prior weight is fixed internally to a very small value (regardless of the `weight_prior` argument). +- `ls`: Least squares: unlike `pls`, this is *not* a flat-prior special case of MAP. The objective function has no prior/omega term at all, and the residual error model is forced to `add = 1, prop = 0` regardless of the `error` argument, so it minimizes unweighted squared residuals. ## Installation diff --git a/man/check_inputs.Rd b/man/check_inputs.Rd index 812d7ee..199cac2 100644 --- a/man/check_inputs.Rd +++ b/man/check_inputs.Rd @@ -21,7 +21,7 @@ lower triangle of the covariance matrix of random effects} \item{censoring}{label for column specifying censoring. If value in dataset in this column is < 0 then censoring is assumed 0 then >ULOQ.} -\item{type}{estimation type, options are `map` and `ls`} +\item{type}{estimation type, one of `"map"` (default), `"pls"`, or `"ls"`} } \description{ Some basic input checking diff --git a/man/get_map_estimates.Rd b/man/get_map_estimates.Rd index 87d9a51..8686b76 100755 --- a/man/get_map_estimates.Rd +++ b/man/get_map_estimates.Rd @@ -31,7 +31,7 @@ get_map_estimates( method = "BFGS", control = list(reltol = 1e-05), allow_obs_before_dose = FALSE, - type = "map", + type = c("map", "pls", "ls"), cols = list(x = "t", y = "y"), residuals = TRUE, verbose = FALSE, @@ -115,7 +115,7 @@ such as `Nelder-Mead` to avoid estimation failures.} \item{allow_obs_before_dose}{allow observation before first dose?} -\item{type}{estimation type, options are `map` and `ls`} +\item{type}{estimation type, one of `"map"` (default), `"pls"`, or `"ls"`} \item{cols}{column names} diff --git a/man/log2pi.Rd b/man/log2pi.Rd index 8e1e771..dd849a7 100644 --- a/man/log2pi.Rd +++ b/man/log2pi.Rd @@ -1,16 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/const.R -\docType{data} \name{log2pi} \alias{log2pi} \title{Constant used in some computations} -\format{ -An object of class \code{numeric} of length 1. -} \usage{ log2pi } \description{ Constant used in some computations } -\keyword{datasets} diff --git a/tests/testthat/test-get_map_estimates.R b/tests/testthat/test-get_map_estimates.R index bc407ae..a263268 100644 --- a/tests/testthat/test-get_map_estimates.R +++ b/tests/testthat/test-get_map_estimates.R @@ -1020,4 +1020,35 @@ test_that("Floating point precision issues don't raise warning", { ) ) expect_equal(round(out$parameters$CL, 3), 2.320) +}) + +test_that("unsupported `type` values are rejected", { + expect_error(get_map_estimates(type = "invalid"), "should be one of") + expect_error(get_map_estimates(type = "map_flat_prior"), "should be one of") +}) + +test_that("map, pls, and ls are all accepted as `type`", { + mod <- PKPDsim::new_ode_model("pk_1cmt_iv") + par <- list(CL = 5, V = 50) + reg <- PKPDsim::new_regimen(amt = 1000, times = 0, type = "bolus") + data <- PKPDsim::sim( + mod, + regimen = reg, + parameters = par, + t_obs = c(1, 4, 8, 16), + only_obs = TRUE + ) + for(type in c("map", "pls", "ls")) { + fit <- get_map_estimates( + model = mod, + parameters = par, + omega = c(0.1, 0.02, 0.1), + regimen = reg, + error = list(prop = 0.1, add = 0.1), + data = data, + type = type, + verbose = FALSE + ) + expect_true(!is.null(fit$parameters)) + } }) \ No newline at end of file