Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: AUC start 0 (which also needs Feature: C0 imputation) #138

Open
wants to merge 60 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0d68014
bug: force doses times to be AFRLT.dose (at ARRLT = 0)
Gero1999 Nov 5, 2024
b8cd1ea
data: correct PKROUTE -> ROUTE
Gero1999 Nov 5, 2024
1aef135
enhancement: create function to create impute_c0 column
Gero1999 Nov 5, 2024
ec3e109
enhancement: include additional C0 imputation strategies
Gero1999 Nov 5, 2024
50fb2e1
feature: intervals include imputate strategy with duplicated rows apa…
Gero1999 Nov 6, 2024
82521f5
bug: PKNCAconc formula adapted for PKNCA to consider predoses for imp…
Gero1999 Nov 6, 2024
135152d
bug: solved classification of doses in create_c0_impute
Gero1999 Nov 6, 2024
18f4ee2
bug: perfectionated formulas to consider different analytes appropiately
Gero1999 Nov 6, 2024
7e93a4f
style: solve most lintr issues
Gero1999 Nov 7, 2024
0a5ba6d
refactor: delete report files that should not have been called
Gero1999 Nov 7, 2024
7dbfd74
refactor: nolint in customized PKNCA_impute_method functions
Gero1999 Nov 7, 2024
43d9f9e
wordlist: add PKNCA_impute_method functions
Gero1999 Nov 7, 2024
429a1e6
documentation: functions in R/PKNCA_impute_method_additions.R
Gero1999 Nov 7, 2024
cb1ee9c
feature: c0 input & when no checked use as interval start 1st observa…
Gero1999 Nov 8, 2024
86102f1
bug: solved PKNCA warnings "No concentration data"
Gero1999 Nov 11, 2024
45e1e52
bug: solved issue of duplicated tmax in results
Gero1999 Nov 11, 2024
2bab21b
refactor: NCA results with all dose and conc columns first
Gero1999 Nov 12, 2024
a622e61
bug: fixed impute_method_start_c1 unneded argument issue
Gero1999 Nov 13, 2024
c345c9b
refactor: delete unused functions in reshape
Gero1999 Nov 14, 2024
769b80d
refactor: input selectors for analyte and dosno put together in UI an…
Gero1999 Nov 14, 2024
79d4da4
refactor: change strategy for input of AUC intervals
Gero1999 Nov 15, 2024
768b972
bug: fixed new intervals strategy and align with PKNCA running
Gero1999 Nov 15, 2024
bf8844b
revert feat: back to old UI function for AUC intervals
Gero1999 Nov 21, 2024
4411231
fix: interval default start since dose, aucint for manual intervals, …
Gero1999 Nov 22, 2024
5c5be0a
fix: when C0 is not imputed, start is at C1
Gero1999 Nov 27, 2024
e211bd7
lintr: code cleaning
Gero1999 Nov 28, 2024
4f99610
fix: merge and solve nca.R/outputs.R conflicts
Gero1999 Nov 28, 2024
06fca2f
fix: issue in slope_selector with new name for input$cyclenca
Gero1999 Nov 28, 2024
a6bddfc
refactor: make own function to create intervals with dose data
Gero1999 Dec 2, 2024
9dfb2a6
fix: adapt lambda_slope_plot to use type_interval column instead of e…
Gero1999 Dec 2, 2024
8ad9bf1
fix: adapt create_c0_impute_column to not use artificial time_dose co…
Gero1999 Dec 2, 2024
7abe8f2
fix bug: correct call for mydata$dose instead of mydose
Gero1999 Dec 2, 2024
9f7fb16
fix: solve issues when C0 is not imputed to start at C1
Gero1999 Dec 3, 2024
0da010b
lintr: lint_dir cleaning
Gero1999 Dec 3, 2024
c85f76a
fix oldrel: deleted line used for testing
Gero1999 Dec 3, 2024
978c802
fix: update roxygen2 documentation for format_data.R functions
Gero1999 Dec 3, 2024
b9c26df
fix: spellcheck added words to WORDLIST
Gero1999 Dec 3, 2024
446cd63
fix: roxygen2 for missing documetnation PKNCA_impute_method_additions.R
Gero1999 Dec 3, 2024
ad4d55a
fix: solved issue app crashes with auc rule sets
Gero1999 Dec 5, 2024
c5d834c
fix: created pknca_trigger to run slope_selector when apply/save changes
Gero1999 Dec 5, 2024
fda231d
refactor: data selection tab with arranged dataset by subject
Gero1999 Dec 5, 2024
baecfb8
aesthetics: add hovering message and new label in checkbox C0 imputat…
Gero1999 Dec 6, 2024
f29cff0
fix: lintr, roxygen doc and clast added to default params in NCA
Gero1999 Dec 6, 2024
54315df
fix: reporting all parameters instead of just auc related for intervals
Gero1999 Dec 9, 2024
de139f7
documentation: roxygen update
Gero1999 Dec 10, 2024
09ba650
fix: delete partial_auc_input.R from R/ folder and leave in inst/shin…
Gero1999 Dec 19, 2024
4d608ac
fix: add tests and prevent input errors in format_data.R functions
Gero1999 Dec 19, 2024
91ddd4b
merge: merged main without conflicts
Gero1999 Dec 19, 2024
1485d3a
update: merge main without conflicts
Gero1999 Dec 20, 2024
21b1b68
fix: issue with select_analyte after merge
Gero1999 Dec 20, 2024
2a4d8b6
test: add tests for PKNCA_impute_method_additions.R and prevent NA in…
Gero1999 Dec 20, 2024
db2b8e8
refactor: create_c0_impute testing, column preventions and renaming (…
Gero1999 Dec 27, 2024
61d3ae1
fix: lintr code cleaning and roxygen documentation
Gero1999 Dec 27, 2024
0fd56f7
fix: update description and wordlist
Gero1999 Dec 30, 2024
cc65e0a
fix: impossible test deleted and improved results column order display
Gero1999 Dec 30, 2024
ed64b88
style: add details to function description and rename PKNCA_impute_me…
Gero1999 Jan 3, 2025
b9d7f36
style: time == start & link on PKNCA custom function guidelines
Gero1999 Jan 3, 2025
3f28c24
test: modify tests from expect_s3_class to expect_no_error for nested…
Gero1999 Jan 3, 2025
67f7593
style: delete unnecessary variable assignment in function
Gero1999 Jan 3, 2025
48d6a4c
documentation: update roxygen2
Gero1999 Jan 3, 2025
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
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: aNCA
Title: (Pre-)clinical NCA in a dynamic shiny app
Title: (Pre-)Clinical NCA in a Dynamic Shiny App
Version: 0.0.0.9000
Authors@R: c(
person("Ercan", "Suekuer", , "[email protected]", role = c("aut", "cre"),
Expand Down Expand Up @@ -55,7 +55,8 @@ Imports:
tidyr,
tools,
utils,
zip
zip,
rlang
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Expand Down
7 changes: 6 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Generated by roxygen2: do not edit by hand

export(PKNCA_impute_method_start_c1)
export(PKNCA_impute_method_start_logslope)
export(anonymize_pk_data)
export(apply_filters)
export(as_factor_preserve_label)
export(calculate_summary_stats)
export(create_conc)
export(create_dose)
export(create_dose_intervals)
export(create_start_impute)
export(filter_breaks)
export(flexible_violinboxplot)
export(format_data)
export(general_lineplot)
export(general_meanplot)
export(geometric_mean)
Expand Down Expand Up @@ -36,6 +39,7 @@ importFrom(PKNCA,PKNCA.options)
importFrom(PKNCA,PKNCAconc)
importFrom(PKNCA,PKNCAdata)
importFrom(PKNCA,PKNCAdose)
importFrom(PKNCA,pk.calc.c0)
importFrom(PKNCA,pk.nca)
importFrom(PKNCA,pknca_units_table)
importFrom(dplyr,across)
Expand Down Expand Up @@ -84,6 +88,7 @@ importFrom(reactable,renderReactable)
importFrom(reactable.extras,dropdown_extra)
importFrom(reactable.extras,text_extra)
importFrom(rio,export_list)
importFrom(rlang,sym)
importFrom(rmarkdown,render)
importFrom(shinyBS,bsModal)
importFrom(shinyFiles,shinyDirChoose)
Expand Down
75 changes: 75 additions & 0 deletions R/PKNCA_impute_method_additions.R
Gero1999 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' This function imputes the start concentration using the log slope method.
#'
#' @param conc Numeric vector of concentrations.
#' @param time Numeric vector of times corresponding to the concentrations.
#' @param start Numeric value indicating the start/dose time.
#' @param end Numeric value indicating the end time.
#' @param ... Additional arguments (currently not used).
#' @param options List of options (currently not used).
#'
#' @return A data frame with imputed start concentration.
#' @details
#' This function adheres to the structure required by the `PKNCA` package to work with its
#' functionalities. For more information, see the
#' [PKNCA Data Imputation Vignette](https://cran.r-project.org/web/packages/PKNCA/vignettes).
#' @export
#'
#' @examples
#' conc <- c(5, 4, 3, 2, 1)
#' time <- c(1, 2, 3, 4, 5)
#' start <- 0
#' end <- 4
#' PKNCA_impute_method_start_logslope(conc, time, start, end)

PKNCA_impute_method_start_logslope <- function(conc, time, start, end, ..., options = list()) { # nolint

d_conc_time <- data.frame(conc = conc, time = time)
if (!any(time == start)) {
all_concs <- conc[time >= start & time <= end]
all_times <- time[time >= start & time <= end]
if (!all(is.na(all_concs))) {
c0 <- PKNCA::pk.calc.c0(all_concs, all_times, method = "logslope")
if (!is.na(c0)) {
d_conc_time <- rbind(d_conc_time, data.frame(time = start, conc = c0))
d_conc_time <- d_conc_time[order(d_conc_time$time), ]
}
}
}
d_conc_time
}

#' This function imputes the start concentration using the first concentration after dose
#'
#' @param conc Numeric vector of concentrations.
#' @param time Numeric vector of times corresponding to the concentrations.
#' @param start Numeric value indicating the start/dose time.
#' @param end Numeric value indicating the end time.
#' @param ... Additional arguments (currently not used).
#' @param options List of options (currently not used).
#'
#' @return A data frame with imputed start concentration.
#' @details
#' This function adheres to the structure required by the `PKNCA` package to work with its
#' functionalities.For more information, see the
#' [PKNCA Data Imputation Vignette](https://cran.r-project.org/web/packages/PKNCA/vignettes).
#' @export
#'
#' @examples
#' conc <- c(1, 2, 3, 4, 5)
#' time <- c(1, 2, 3, 4, 5)
#' start <- 0
#' end <- 4
#' PKNCA_impute_method_start_c1(conc, time, start, end)
PKNCA_impute_method_start_c1 <- function(conc, time, start, end, ..., options = list()) { # nolint
d_conc_time <- data.frame(conc = conc, time = time)
if (!any(time == start)) {
all_concs <- conc[time >= start & time <= end]
all_times <- time[time >= start & time <= end]
if (!all(is.na(all_concs))) {
c1 <- all_concs[which.min(all_times)]
d_conc_time <- rbind(d_conc_time, data.frame(time = start, conc = c1))
d_conc_time <- d_conc_time[order(d_conc_time$time), ]
}
}
d_conc_time
}
102 changes: 102 additions & 0 deletions R/create_start_impute.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#' Create C0 Impute Column
#'
#' Defines an impute column in the intervals of the PKNCAdata object based on data
#'
#' @param mydata A PKNCAdata object containing concentration and dose data.
#' @return A PKNCAdata object with updated intervals table including start imputation strategies.
#' @import dplyr
#' @importFrom rlang sym
#' @importFrom PKNCA pk.calc.c0
#' @export
#'
#' @examples
#' \dontrun{
#' mydata <- create_start_impute(mydata)
#' }
create_start_impute <- function(mydata) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Would suggest naming the argument data instead of mydata, or something more suggestive (pknca_data, data_object or similar).


# Define columns
conc_column <- mydata$conc$columns$conc
time_column <- mydata$conc$columns$time
analyte_column <- mydata$conc$columns$groups$group_analyte
route_column <- mydata$dose$columns$route
duration_column <- mydata$dose$columns$duration
drug_column <- "DRUG" # At some point should be added at least manually in PKNCA mydata object
conc_group_columns <- unname(unlist(mydata$conc$columns$groups))
dose_group_columns <- unname(unlist(mydata$dose$columns$groups))
group_columns <- unique(c(conc_group_columns, dose_group_columns))

# Define dose number (DOSNO) if not present in dose data
if (!"DOSNO" %in% mydata$dose$data) {
mydata$dose$data <- mydata$dose$data %>%
group_by(across(all_of(c(unname(unlist(mydata$dose$columns$groups)))))) %>%
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: You already have dose_group_columns variable available.

Suggested change
group_by(across(all_of(c(unname(unlist(mydata$dose$columns$groups)))))) %>%
group_by(across(all_of(dose_group_columns))) %>%

mutate(DOSNO = row_number()) %>%
ungroup()
}

mydata_with_int <- merge(
x = mydata$conc$data %>%
dplyr::select(any_of(c(conc_group_columns, conc_column, time_column))),
y = mydata$dose$data %>%
dplyr::select(any_of(c(dose_group_columns, route_column,
duration_column, "DOSNO", "DRUG")))
) %>%
merge(mydata$intervals)

# Define dosing drug as analyte if not present
if (!"DRUG" %in% colnames(mydata_with_int)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Should this check take drug_column instead?

Suggested change
if (!"DRUG" %in% colnames(mydata_with_int)) {
if (!drug_column %in% colnames(mydata_with_int)) {

if (length(analyte_column) == 1) {
mydata_with_int <- mydata_with_int %>%
mutate(DRUG = !!sym(analyte_column))
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Call the mutate function without piping the variable.

drug_column <- "DRUG"
} else {
mydata_with_int <- mydata_with_int %>%
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Call the mutate function without piping the variable.

mutate(DRUG = "A", ANALYTE = "A")
drug_column <- "DRUG"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: drug_column is already defined on line 24 and seems to be constant anyway, why re-assign it?

analyte_column <- "ANALYTE"
}
}

# Process imputation strategy based on each interval
new_intervals <- mydata_with_int %>%
group_by(across(all_of(c(group_columns, "DOSNO", "start", "end")))) %>%
arrange(across(all_of(c(group_columns, time_column)))) %>%
dplyr::mutate(
vr_avals = paste0("c(", paste0(!!sym(conc_column), collapse = ","), ")"),
vr_times = paste0("c(", paste0(!!sym(time_column), collapse = ","), ")")
Comment on lines +64 to +66
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: From what I can see, there is no reason for collapsing a vector into a string, only to use eval() to turn this back into a vector? Seems to me like we can just pass those directly to a function call, like

PKNMCA::pk.calc.c0(
  conc = !!sym(conc_column),
  time = !!sym(time_column),
 ...
)

Is there anything I am missing, why this would not work?

) %>%
arrange((!!sym(time_column) - start) < 0,
(!!sym(time_column) - start)) %>%
Comment on lines +68 to +69
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I would avoid hanging indents with very complex pipelines, they become hard to read pretty quickly.

Suggested change
arrange((!!sym(time_column) - start) < 0,
(!!sym(time_column) - start)) %>%
arrange(
(!!sym(time_column) - start) < 0,
(!!sym(time_column) - start)
) %>%

slice(1) %>%
ungroup() %>%
rowwise() %>%
mutate(
impute = case_when(
!!sym(time_column) == start & !is.na(!!sym(conc_column)) ~ NA,
DOSNO == 1 &
(tolower(!!sym(route_column)) == "extravascular" |
!!sym(duration_column) > 0 |
!!sym(analyte_column) != !!sym(drug_column)) ~ "start_conc0",
DOSNO > 1 &
(tolower(!!sym(route_column)) == "extravascular" |
!!sym(duration_column) > 0 |
!!sym(analyte_column) != !!sym(drug_column)) ~ "start_predose",
tolower(!!sym(route_column)) == "intravascular" &
!!sym(duration_column) == 0 &
!!sym(analyte_column) == !!sym(drug_column) &
!is.na(PKNCA::pk.calc.c0(conc = eval(parse(text = vr_avals)),
time = eval(parse(text = vr_times)),
time.dose = start,
method = "logslope")) ~ "start_logslope",
tolower(!!sym(route_column)) == "intravascular" &
!!sym(duration_column) == 0 &
!!sym(analyte_column) == !!sym(drug_column) ~ "start_c1"
)
) %>%
Comment on lines +73 to +95
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue:
This is quite hard to digest and know exactly what is going on. On top of that, you have several repeating or opposite checks that can impact performance. I would suggest either:

- Breaking down the checks into separate function/s

mutate(impute = get_imputation_strategy(.))

or

mutate(
  impute = case_when(
    is_conc0(!!sym(route_column), !!sym(duration_column), ...) ~ "start_conc0",
    ...
  )
)

- or creating columns for each check and then using that

mutate(
  is_first_dose = DOSENO == 1,
  is_extravascular = tolower(!!sym(route_column)) == "extravascular",
  ...
) %>%
mutate(
  inpute = case_when(
    is_first_dose & (is_extravascular | ...) ~ "start_conc0",
    ...
  )
)

# Select only the columns of interest
select(any_of(c(names(mydata$intervals), "impute")))

mydata$intervals <- new_intervals
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I do not think there is a need to assign a new variable, why not just do

mydata$intervals <- mydata_with_int %>%
  ...

and remove this line.


return(mydata)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick, style: no need for return() statement.

Suggested change
return(mydata)
mydata

}
Loading
Loading