Skip to content

Conversation

@Gero1999
Copy link
Contributor

@Gero1999 Gero1999 commented Jan 3, 2025

New PKNCA_method_start functions that could perhaps be default/standard. Feel free to give them a look and merge them if they are useful!

Closes #350

# Imputes a start concentration based on the logslope method for IV bolus
PKNCA_impute_method_start_logslope <- function(conc, time, start, end, ..., options = list()) { # nolint

  ret <- data.frame(conc = conc, time = time)
  mask_start <- time %in% start
  if (!any(mask_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)) {
        ret <- rbind(ret, data.frame(time = start, conc = c0))
        ret <- ret[order(ret$time), ]
      }
    }
  }
  ret
}

# Imputes a start concentration using the first available concentration measure
PKNCA_impute_method_start_c1 <- function(conc, time, start, end, ..., options = list()) { # nolint
  ret <- data.frame(conc = conc, time = time)
  mask_start <- time %in% start
  if (!any(mask_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)]
      ret <- rbind(ret, data.frame(time = start, conc = c1))
      ret <- ret[order(ret$time), ]
    }
  }
  ret
}

@Gero1999 Gero1999 marked this pull request as ready for review January 3, 2025 17:03
ret <- data.frame(conc = conc, time = time)
mask_start <- time %in% start
if (!any(mask_start)) {
all_concs <- conc[time >= start & time <= end]
Copy link
Member

Choose a reason for hiding this comment

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

Please use pk.calc.c0.method.c1. That way, you don't need to filter all_concs, all_times, and your is.na() test below can be on c1. That will also ensure that c1 is consistently calculated anywhere it is used within PKNCA.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added an assert before all_concs <- because if any time=NA all_concs will contain NA, let me know what you think of it!

data.frame(conc = c(3, 3, 1), time = 1:3),
ignore_attr = TRUE
)
})
Copy link
Member

Choose a reason for hiding this comment

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

Please add tests for:

  1. All concentrations are NA
  2. All times are NA
  3. All concentrations are 0

(I see that list of tests is needed for the other imputation methods, too. I just added an issue, #361, to add those after this PR is merged.)

Copy link
Contributor Author

@Gero1999 Gero1999 Jan 22, 2025

Choose a reason for hiding this comment

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

Hey sorry for the delay on this @billdenney! Some questions regarding the cases and their expected outputs:

  1. All concentrations are NA

Initially I developed the functions using the same strucuture as PKNCA_impute_method_start_cmin:

PKNCA_impute_method_start_cmin <- function(conc, time, start, end, ..., options = list()) {
  ret <- data.frame(conc = conc, time = time)
  mask_start <- time %in% start
  if (!any(mask_start)) {
    all_concs <- conc[start <= time & time <= end]
    if (!all(is.na(all_concs))) {
      cmin <- min(all_concs, na.rm = TRUE)
      ret <- rbind(ret, data.frame(time = start, conc = cmin))
      ret <- ret[order(ret$time), ]
    }
  }
  ret
}

In this case, all conc = NA will return the same dataset for PKNCA_impute_method_start_cmin

But if for PKNCA_impute_method_start_c1 we want directly the original pk.calc.c0.method.c1 handling the NA case like in the original function, when all conc = NA then start = NA with a warning message

Is this the desired behavior we would like to have for PKNCA_impute_method_start_c1 even if it is different to PKNCA_impute_method_start_cmin? The same with PKNCA_impute_method_start_logslope?

  1. All times are NA

Returns same dataset for PKNCA_impute_method_start_cmin
Returns error for PKNCA_impute_method_start_c1 with default pk.calc.c0.method.c1 behaviour

  1. All concs are 0

Should PKNCA_impute_method_start_logslope return same dataset or NA?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept their original behaviours of these testing cases as on my initial pr. Let me know if you think I need to standardize them. This is probably something we need to consider on the other PKNCA_impute_method_start when doing #361

PKNCA_impute_method_start_logslope

  1. All concentrations are NA:

    • The function does not change the input data. The output will have the same NA concentrations as the input.
  2. All times are NA:

    • The function does not change the input data. The output will have the same NA times as the input.
  3. All concentrations are 0:

    • The function does not change the input data. The output will have the same 0 concentrations as the input.

PKNCA_impute_method_start_c1

  1. All concentrations are NA:

    • The function adds the start time with an NA concentration to the data. The output will have NA concentrations, including the imputed start time.
  2. All times are NA:

    • The function raises an error indicating that the time vector contains missing values.
  3. All concentrations are 0:

    • The function adds the start time with a 0 concentration to the data. The output will have 0 concentrations, including the imputed start time.

@Gero1999
Copy link
Contributor Author

Gero1999 commented Feb 3, 2025

In order to keep working in other issues I will reframe the branch strategy in my fork. Unfortunately, I have no permissions to simply modify the development branch on this pr, so I duplicated it with the correct branch (#383) and close this one.

@Gero1999 Gero1999 closed this Feb 3, 2025
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.

Feature request: New logslope & c1 impute_start methods

2 participants