Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 16 additions & 12 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 4 additions & 2 deletions R/get_map_estimates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -123,14 +123,16 @@ 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,
A_init = NULL,
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)

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion man/check_inputs.Rd

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

4 changes: 2 additions & 2 deletions man/get_map_estimates.Rd

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

5 changes: 0 additions & 5 deletions man/log2pi.Rd

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

31 changes: 31 additions & 0 deletions tests/testthat/test-get_map_estimates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
Loading