Here's an existing function I wrote that we can tidy/adapt. One thing to decide is whether we should do this in run_eval() or leave it as a function the user could apply to their NONMEM data set if they need it. The former is probably better, since it allows us to assume TAD is always available.
add_time_after_dose <- function(nonmem_data) {
nonmem_data |>
dplyr::group_by(ID, OCC) |>
dplyr::mutate(
TDOSE = dplyr::if_else(AMT > 0 | EVID == 1 | EVID == 4, TIME, NA),
DOSEEND = TDOSE + LENGTH
) |>
tidyr::fill(TDOSE, DOSEEND) |>
dplyr::mutate(
TAD = TIME - TDOSE,
TAI = dplyr::if_else(AMT == 0 | EVID == 0 | EVID == 2, TIME - DOSEEND, 0),
.after = RATE
) |>
dplyr::select(-TDOSE, -DOSEEND) |>
dplyr::ungroup()
}