Skip to content

Add impute_method parameter to PK calculation functions and refactor imputation logic - #6

Draft
Gero1999 with Copilot wants to merge 3 commits into
456-impute-execution-orderfrom
copilot/add-impute-method-to-functions
Draft

Add impute_method parameter to PK calculation functions and refactor imputation logic#6
Gero1999 with Copilot wants to merge 3 commits into
456-impute-execution-orderfrom
copilot/add-impute-method-to-functions

Conversation

Copilot AI commented Oct 14, 2025

Copy link
Copy Markdown

Summary

This PR refactors how data imputation is handled in PKNCA by moving imputation logic from pk.nca.interval into individual PK calculation functions. Previously, imputation was applied externally before calling parameter functions. Now, each function that handles BLQ (below limit of quantification) values can also handle imputation internally, providing better control and consistency.

Changes

New Parameters Added

Added impute_method, conc.group, and time.group parameters to all functions with conc.blq argument:

In R/auc.R:

  • pk.calc.auxc()

In R/half.life.R:

  • pk.calc.half.life()

In R/interpolate.conc.R:

  • interp.extrap.conc()
  • interpolate.conc()
  • extrapolate.conc()
  • interp.extrap.conc.dose()

Implementation Details

  1. Imputation after BLQ cleaning: The impute_method is now applied after clean.conc.blq() processing within each function, ensuring proper data cleaning order.

  2. Group-level data support: Added optional conc.group and time.group parameters to support imputation functions like PKNCA_impute_method_start_predose that require access to group-level concentration and time data.

  3. Refactored pk.nca.interval: Removed the imputation logic that was applied before calling parameter functions. Instead, impute_method, conc.group, and time.group are now passed as arguments to the parameter functions, which handle imputation internally.

Backward Compatibility

All new parameters have default values that maintain existing behavior:

  • impute_method = NA_character_
  • conc.group = NULL
  • time.group = NULL

Functions continue to work as before when imputation is not specified.

Example Usage

# Imputation is now handled within the function
result <- pk.calc.auxc(
  conc = my_conc,
  time = my_time,
  impute_method = "start_conc0",
  conc.group = group_conc,
  time.group = group_time,
  check = TRUE
)

Benefits

  • Consistent API: All parameter functions that handle BLQ values now have a uniform interface for imputation
  • Better control: Each function applies imputation at the appropriate point in its workflow (after BLQ cleaning)
  • Flexibility: Functions can access group-level data when needed for advanced imputation methods
  • Maintainability: Imputation logic is encapsulated within the functions that use it rather than being scattered across the codebase

Fixes the requirement to refactor imputation from being applied outside parameter functions to being passed as an argument and applied internally.

Original prompt

in all R functions in auc.R, pk.calc.simple.R, half.life.R, interorpolate.conc.R that contain an argument conc.blq, add an argument called impute_method

the impute_method (which is a function or a list of PKNCA_impute functions) should be applied after the clean.conc.blq

later adjust pk.nca.interval (pk.calc.all.R) so instead of using the impute metod outside the do.call of the parameter function, is used as another argument


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 14, 2025 18:49
Co-authored-by: Gero1999 <68994823+Gero1999@users.noreply.github.com>
Co-authored-by: Gero1999 <68994823+Gero1999@users.noreply.github.com>
Copilot AI changed the title [WIP] Add impute_method argument to relevant R functions Add impute_method parameter to PK calculation functions and refactor imputation logic Oct 14, 2025
Copilot AI requested a review from Gero1999 October 14, 2025 18:58
Gero1999 pushed a commit that referenced this pull request Apr 15, 2026
…lve bugs #6 and #7

Replace all single | and & operators with || and && in scalar if()
conditions across 24 R source files for idiomatic short-circuit
evaluation. Vectorized uses of | and & (inside all(), any(), or
column-level mask construction) are unchanged.

Add comments to auc_integrate.R and cleaners.R documenting that
%in% 0 for BLQ detection is intentionally exact equality, since
BLQ values are cleaned to exactly 0 upstream and a tolerance cannot
be used without domain knowledge of what a "low" concentration means.

All 2292 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants