R/prepare-thresholds.R

Defines functions get_K_i_xgboost get_K_i_merge

# --- prepare_thresholds() internals -----------------------------------------
# Step 1 (two alternatives): compute each individual's own CIP value (K_i)

#' Compute each individual's own CIP value (K_i) by direct merge
#'
#' Step 1 of \code{prepare_thresholds()}'s pipeline (the \code{interpolation
#' = NA} path): merges \code{CIP} directly onto \code{.tbl} by
#' \code{CIP_merge_columns} and renames the merged CIP column to
#' \code{K_i_col}. See \code{\link{get_K_i_xgboost}} for the alternative
#' (predicted) path.
#'
#' @param .tbl Tibble being prepared.
#' @param CIP Tibble with population representative cumulative incidence proportions.
#' @param CIP_merge_columns Columns to merge \code{.tbl} and \code{CIP} by.
#' @param CIP_cip_col Name of the CIP value column in \code{CIP}.
#' @param K_i_col Name to give the resulting per-individual CIP column.
#'
#' @returns \code{.tbl} with \code{K_i_col} added.
#' @noRd
get_K_i_merge <- function(.tbl, CIP, CIP_merge_columns, CIP_cip_col, K_i_col) {
  .tbl %>%
    dplyr::left_join(CIP, by = CIP_merge_columns) %>%
    dplyr::rename(!!as.symbol(K_i_col) := !!as.symbol(CIP_cip_col))
}

#' Compute each individual's own CIP value (K_i) via xgboost prediction
#'
#' Step 1 of \code{prepare_thresholds()}'s pipeline (the
#' \code{interpolation = "xgboost"} path): trains an xgboost model on
#' \code{CIP} and predicts each individual's CIP at their exact age, for
#' ages not directly observed in \code{CIP}. Predictions are floored at
#' \code{min_CIP_value} and forced to be monotone increasing in age via
#' \code{cummax()}, since xgboost predictions aren't guaranteed to respect
#' that CIP must be monotonic. See \code{\link{get_K_i_merge}} for the
#' alternative (direct merge) path.
#'
#' @param .tbl Tibble being prepared.
#' @param CIP Tibble with population representative cumulative incidence proportions.
#' @param CIP_merge_columns Columns (besides \code{age_col}) used as predictors.
#' @param CIP_cip_col Name of the CIP value column in \code{CIP}.
#' @param age_col Name of the age column.
#' @param K_i_col Name to give the resulting per-individual CIP column.
#' @param bst.params Named list of parameters passed to \code{xgboost::xgboost()}.
#' @param min_CIP_value Minimum CIP value to allow; predictions are floored at this.
#' @param xgboost_itr Number of boosting iterations (\code{nrounds}).
#'
#' @returns \code{.tbl} with \code{K_i_col} added.
#' @noRd
get_K_i_xgboost <- function(.tbl, CIP, CIP_merge_columns, CIP_cip_col, age_col,
                            K_i_col, bst.params, min_CIP_value, xgboost_itr) {
  # extract cip values
  y <- CIP[[CIP_cip_col]]

  # force the remaining values in cur_cip to be a matrix
  X <- as.matrix(select(CIP, all_of(c(CIP_merge_columns, age_col)), -!!as.symbol(CIP_cip_col)))

  # train xgboost (forward parameters dynamically; params argument deprecated)
  xgb_args <- c(
    list(x = X, y = y, nrounds = xgboost_itr),
    bst.params
  )
  xgb <- do.call(xgboost::xgboost, xgb_args)

  # get the predicted CIP value based on the merge columns.
  .tbl[[K_i_col]] <- .tbl %>%
    select(all_of(c(CIP_merge_columns, age_col))) %>%
    as.matrix() %>%
    predict(xgb, .) %>%
    pmax(min_CIP_value)

  # xgboost predictions are not guaranteed to be monotone increasing in age,
  # even though CIP by definition must be - enforce it. Not applied to the
  # merge path: real CIP table values should already be monotonic, and
  # silently smoothing them there would mask a genuine data problem instead
  # of correcting for the expected imperfection of a predictive model.
  .tbl %>%
    group_by(across(all_of(setdiff(CIP_merge_columns, age_col)))) %>%
    arrange(!!as.symbol(age_col)) %>%
    mutate(!!as.symbol(K_i_col) := cummax(!!as.symbol(K_i_col))) %>%
    ungroup()
}

#' Compute each individual's population prevalence (K_pop)
#'
#' Step 2 of \code{prepare_thresholds()}'s pipeline, shared regardless of
#' how K_i was obtained (see \code{\link{get_K_i_merge}}/
#' \code{\link{get_K_i_xgboost}}) - previously only available for the
#' xgboost path. Either takes the maximum CIP value per stratum from
#' \code{CIP} (\code{Kpop = "useMax"}), or joins a user-supplied
#' \code{Kpop} tibble.
#'
#' @param .tbl Tibble being prepared.
#' @param CIP Tibble with population representative cumulative incidence proportions.
#' @param Kpop Either "useMax", or a tibble with a \code{K_pop_col} column and columns matching the desired strata in \code{.tbl}.
#' @param CIP_merge_columns Columns \code{.tbl} and \code{CIP} share, used for stratification.
#' @param CIP_cip_col Name of the CIP value column in \code{CIP}.
#' @param age_col Name of the age column (excluded from the stratification used to compute K_pop).
#' @param K_pop_col Name to give the resulting population-prevalence column.
#'
#' @returns \code{.tbl} with \code{K_pop_col} added.
#' @noRd
get_K_pop <- function(.tbl, CIP, Kpop, CIP_merge_columns, CIP_cip_col, age_col, K_pop_col) {
  if (is.character(Kpop) && Kpop == "useMax") {
    select(CIP, all_of(setdiff(CIP_merge_columns, age_col)), !!as.symbol(CIP_cip_col)) %>%
      group_by(across(all_of(setdiff(CIP_merge_columns, age_col)))) %>%
      summarise(!!as.symbol(K_pop_col) := max(!!as.symbol(CIP_cip_col)), .groups = "drop") %>%
      left_join(.tbl, ., by = setdiff(CIP_merge_columns, age_col))

  } else if (any(class(Kpop) %in% c("data.frame", "tibble", "matrix", "data.table", "tbl_df", "tbl"))) {
    if (!(K_pop_col %in% colnames(Kpop))) {
      stop("prepare_thresholds: The object Kpop must contain a column named '", K_pop_col,
          "' with population prevalence values for columns also present in .tbl.")
    }
    left_join(.tbl, Kpop)

  } else {
    stop("prepare_thresholds: Kpop must be either 'useMax' or a tibble with columns matching ",
        "the desired strata in .tbl and a column named '", K_pop_col, "' with population prevalence values.")
  }
}

#' Warn if any individual's K_i exceeds their K_pop
#'
#' Step 3 of \code{prepare_thresholds()}'s pipeline. K_pop should always
#' be >= K_i by definition; a violation may indicate a
#' non-population-representative \code{Kpop}, or that \code{K_i} was
#' estimated in an ascertained sample. Uses \code{warning()}, not
#' \code{stop()} - unlike the equivalent check in
#' \code{validate_tbl_input()}, which is a hard mathematical precondition
#' for the Gibbs/PA estimator, this function has no such hard requirement;
#' that downstream \code{stop()} remains the actual safety net. Reuses
#' \code{\link{describe_bad_rows}} from validate.R. There is no
#' personal-id column available in this function's scope, so rows are
#' identified by row number.
#'
#' @param .tbl Tibble being prepared, with \code{K_i_col} and \code{K_pop_col} already present.
#' @param K_i_col Name of the per-individual CIP column.
#' @param K_pop_col Name of the population-prevalence column.
#'
#' @returns \code{invisible(NULL)}. Called only for its warning side effect.
#' @noRd
warn_on_K_i_exceeds_K_pop <- function(.tbl, K_i_col, K_pop_col) {
  bad_indx <- which(.tbl[[K_i_col]] > .tbl[[K_pop_col]])
  detail <- describe_bad_rows(bad_indx, paste("row", seq_len(nrow(.tbl))))
  if (!is.null(detail)) {
    warning("prepare_thresholds: K_i is greater than K_pop for ", detail,
            ". This may indicate non-population-representative K_pop values, ",
            "or that K_i was estimated in an ascertained sample.")
  }
  invisible(NULL)
}

#' Compute threshold/lower/upper from K_i, K_pop, and personal_thr
#'
#' Step 4 of \code{prepare_thresholds()}'s pipeline, shared regardless of
#' how K_i was obtained - previously the merge path always used K_i here,
#' ignoring \code{personal_thr} entirely. \code{thr} is computed from
#' \code{K_i} if \code{personal_thr = TRUE}, or from \code{K_pop}
#' otherwise; \code{lower}/\code{upper} are then derived from \code{thr}
#' and \code{status_col}.
#'
#' @param .tbl Tibble being prepared, with \code{K_i_col} and \code{K_pop_col} already present.
#' @param K_i_col Name of the per-individual CIP column.
#' @param K_pop_col Name of the population-prevalence column.
#' @param thr_col Name to give the resulting threshold column.
#' @param lower_col Name to give the resulting lower-threshold column.
#' @param upper_col Name to give the resulting upper-threshold column.
#' @param status_col Name of the case/control status column.
#' @param personal_thr Should \code{thr} be based on \code{K_i} (TRUE) or \code{K_pop} (FALSE)?
#' @param lower_equal_upper Should \code{lower} and \code{upper} be equal for cases?
#'
#' @returns \code{.tbl} with \code{thr_col}, \code{lower_col}, and \code{upper_col} added.
#' @noRd
apply_threshold_mutate <- function(.tbl, K_i_col, K_pop_col, thr_col, lower_col, upper_col,
                                   status_col, personal_thr, lower_equal_upper) {
  .tbl %>%
    mutate(
      !!as.symbol(thr_col) := ifelse(rep(personal_thr, n()),
                          qnorm(!!as.symbol(K_i_col), lower.tail = FALSE),
                          qnorm(!!as.symbol(K_pop_col), lower.tail = FALSE)),
      !!as.symbol(lower_col) := ifelse(!!as.symbol(status_col) == 1, !!as.symbol(thr_col), -Inf),
      !!as.symbol(upper_col) := ifelse(!!as.symbol(status_col) == 1,
                          ifelse(lower_equal_upper, !!as.symbol(thr_col), Inf),
                          !!as.symbol(thr_col))
    )
}

#' Blank out thresholds for individuals with a negative age
#'
#' Step 5 of \code{prepare_thresholds()}'s pipeline, shared regardless of
#' how K_i was obtained (previously duplicated once per interpolation
#' branch). A negative age can occur when the end of follow-up happens
#' before an individual's birth (e.g. a relative diagnosed before a
#' later-born family member existed). Warns and sets
#' \code{lower_col}/\code{upper_col} to \code{-Inf}/\code{Inf}
#' (uninformative) for those rows.
#'
#' @param .tbl Tibble being prepared, with \code{lower_col}/\code{upper_col} already present.
#' @param age_col Name of the age column.
#' @param lower_col Name of the lower-threshold column.
#' @param upper_col Name of the upper-threshold column.
#'
#' @returns \code{.tbl}, with \code{lower_col}/\code{upper_col} blanked out for negative-age rows, if any.
#' @noRd
blank_thresholds_for_negative_ages <- function(.tbl, age_col, lower_col, upper_col) {
  if (any(.tbl[[age_col]] < 0)) {
    warning("prepare_thresholds: Some ages are negative. This may be due to the end of ",
            "follow-up happening before the birth of an individual. Setting their thresholds ",
            "to be uninformative. Please check the input data.")
    .tbl <- .tbl %>%
      mutate(!!as.symbol(lower_col) := ifelse(!!as.symbol(age_col) < 0, -Inf, !!as.symbol(lower_col)),
             !!as.symbol(upper_col) := ifelse(!!as.symbol(age_col) < 0,  Inf, !!as.symbol(upper_col)))
  }
  .tbl
}

#' Warn if any thresholds ended up NA
#'
#' Step 6 of \code{prepare_thresholds()}'s pipeline, shared regardless of
#' how K_i was obtained (previously duplicated once per interpolation
#' branch). NA thresholds usually mean an individual's age/age-of-onset
#' didn't match any age present in the CIP table.
#'
#' @param .tbl Tibble being prepared, with \code{lower_col}/\code{upper_col} already present.
#' @param lower_col Name of the lower-threshold column.
#' @param upper_col Name of the upper-threshold column.
#'
#' @returns \code{invisible(NULL)}. Called only for its warning side effect.
#' @noRd
warn_on_na_thresholds <- function(.tbl, lower_col, upper_col) {
  if (any(is.na(.tbl[[lower_col]])) | any(is.na(.tbl[[upper_col]]))) {
    warning(paste0("There are ", sum(is.na(select(.tbl, !!as.symbol(lower_col), !!as.symbol(upper_col)))),
                   " NA values in the thresholds. \n Do the age and age of onset values match the ages given in the CIPs?"))
  }
  invisible(NULL)
}

#' Calculate (personalised) thresholds based on CIPs.
#'
#' This function prepares input for \code{estimate_liability} by calculating thresholds based on stratified cumulative incidence proportions (CIPs) with options for interpolation for ages between CIP values. Given a tibble with families and family members and (stratified) CIPs, personalised thresholds will be calculated for each individual present in \code{.tbl}. An individual may be in multiple families, but only once in the same family.
#'
#' @details
#' As of version 1.2.0, \code{personal_thr} defaults to \code{TRUE}
#' (previously \code{FALSE}), and \code{interpolation = NA} (the raw-merge
#' path, i.e. no interpolation) now also computes \code{K_i}/\code{K_pop}
#' and respects \code{personal_thr} - previously that path always behaved
#' as though \code{personal_thr = TRUE} regardless of what was passed, and
#' never computed \code{K_i}/\code{K_pop} at all. If your code relies on
#' the previous default behavior (population-level thresholds from
#' \code{interpolation = NA}), pass \code{personal_thr = FALSE} explicitly
#' to reproduce it.
#'
#' @param .tbl Tibble with  family and personal id columns, as well as CIP_merge_columns and status.
#' @param CIP Tibble with population representative cumulative incidence proportions. CIP must contain columns from \code{CIP_merge_columns} and \code{cIP_cip_col}.
#' @param age_col Name of column with age at the end of follow-up or age at diagnosis for cases.
#' @param CIP_merge_columns The columns the CIPs are subset by, e.g. CIPs by birth_year, sex.
#' @param CIP_cip_col Name of column with CIP values.
#' @param Kpop Takes either "useMax" to use the maximum value in the CIP strata as population prevalence, or a tibble with population prevalence values based on other information. If a tibble is provided, it must contain columns from \code{.tbl} and a column named "K_pop" with population prevalence values. Defaults to "UseMax".
#' @param status_col Column that contains the status of each family member. Coded as 0 or FALSE (control) and 1 or TRUE (case).
#' @param thr_col Name of column to create for threshold. Defaults to "thr".
#' @param lower_col Name of column to create for lower threshold. Defaults to "lower".
#' @param upper_col Name of column to create for upper threshold. Defaults to "upper".
#' @param K_i_col Name of column to create for individual cumulative incidence proportion. Defaults to "K_i".
#' @param K_pop_col Name of column to create for population prevalence within an individuals CIP strata. Defaults to "K_pop".
#' @param lower_equal_upper Should the upper and lower threshold be the same for cases? Can be used if CIPs are detailed, e.g. stratified by birth year and sex.
#' @param personal_thr Should thresholds be based on stratified CIPs (K_i, TRUE) or population prevalence (K_pop, FALSE)? Defaults to TRUE.
#' @param interpolation Type of interpolation, defaults to NA.
#' @param bst.params List of parameters to pass on to xgboost. See xgboost documentation for details.
#' @param min_CIP_value Minimum cip value to allow. Too low values may lead to numerical instabilities.
#' @param xgboost_itr Number of iterations to run xgboost for.
#'
#'
#' @importFrom stats qnorm predict
#' @importFrom dplyr all_of mutate select %>% left_join group_by ungroup arrange across
#'
#' @return Tibble with (personlised) thresholds for each family member (lower & upper), the calculated cumulative incidence proportion for each individual (K_i), and population prevalence within an individuals CIP strata (K_pop; max value in stratum). The threshold and other potentially relevant information can be added to the family graphs with \code{familywise_attach_attributes}.
#'
#' @examples
#' tbl = data.frame(
#' fid = c(1, 1, 1, 1),
#' pid = c(1, 2, 3, 4),
#' role = c("o", "m", "f", "pgf"),
#' sex = c(1, 0, 1, 1),
#' status = c(0, 0, 1, 1),
#' age = c(22, 42, 48, 78),
#' birth_year = 2023 - c(22, 42, 48, 78),
#' aoo = c(NA, NA, 43, 45))
#'
#' cip = data.frame(
#' age = c(22, 42, 43, 45, 48, 78),
#' birth_year = c(2001, 1981, 1975, 1945, 1975, 1945),
#' sex = c(1, 0, 1, 1, 1, 1),
#' cip = c(0.1, 0.2, 0.3, 0.3, 0.3, 0.4))
#'
#' prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age", interpolation = NA)
#'
#' @export

prepare_thresholds = function(.tbl,
                              CIP,
                              age_col,
                              CIP_merge_columns = c("sex", "birth_year", "age"),
                              CIP_cip_col = "cip",
                              Kpop = "useMax",
                              K_i_col = "K_i",
                              K_pop_col = "K_pop",
                              status_col = "status",
                              thr_col = "thr",
                              lower_col = "lower",
                              upper_col = "upper",
                              lower_equal_upper = FALSE,
                              personal_thr = TRUE,
                              interpolation = NA,
                              bst.params = list(
                                max_depth = 10,
                                base_score = 0,
                                nthread = 4,
                                min_child_weight = 10
                                ),
                              min_CIP_value = 1e-5,
                              xgboost_itr = 30
) {

# Checking input data -----------------------------------------------------
  # check if CIP_merge_columns are in both .tbl and CIP:
  if (any(!(CIP_merge_columns %in% colnames(.tbl)))) {
    stop(paste0("prepare_thresholds: The following columns are not present in the provided .tbl: ", paste(setdiff(CIP_merge_columns, colnames(.tbl)), collapse = ", ")))
  }

  if (any(!(CIP_merge_columns %in% colnames(CIP)))) {
    stop(paste0("prepare_thresholds: The following columns are not present in the provided CIP: ", paste(setdiff(CIP_merge_columns, colnames(CIP)), collapse = ", ")))
  }

  # checking whether the non-age columns used for merging in .tbl are present in CIP
  # this allows CIP to only hold relevant stratification info.
  # interpolation can be done on the age column.
  overlap_test_tbl = sapply(setdiff(CIP_merge_columns, age_col), function(x) {
    any(!(.tbl[[x]] %in% CIP[[x]]))
  })
  if ( any(overlap_test_tbl) ) {
    warning(paste0("prepare_thresholds: The following CIP_merge_columns are not completely overlapping in CIP and .tbl:",
                   paste(setdiff(CIP_merge_columns, age_col)[overlap_test_tbl], collapse = ", ")))
  }

  # is the parameters passed to xgboost a named list?
  if (is.null(names(bst.params)) || any(names(bst.params) == "")) {
    stop("bst.params must be a *named* list of xgboost parameters.")
  }


# -------------------------------------------------------------------------


  # TODO:
  # ADD linear interpolation

  # interpolation with xgboost or merge on raw values?
  if (!is.na(interpolation) && interpolation != "xgboost") stop("Invalid choice of interpolation method. Must be NA or xgboost.")

  # Step 1: get each individual's own CIP value (K_i), by whichever method.
  .tbl <- if (is.na(interpolation)) {
    get_K_i_merge(.tbl, CIP, CIP_merge_columns, CIP_cip_col, K_i_col)
  } else {
    get_K_i_xgboost(.tbl, CIP, CIP_merge_columns, CIP_cip_col, age_col,
                    K_i_col, bst.params, min_CIP_value, xgboost_itr)
  }

  # Steps 2-6: identical regardless of how K_i was obtained.
  .tbl <- get_K_pop(.tbl, CIP, Kpop, CIP_merge_columns, CIP_cip_col, age_col, K_pop_col)
  warn_on_K_i_exceeds_K_pop(.tbl, K_i_col, K_pop_col)
  .tbl <- apply_threshold_mutate(.tbl, K_i_col, K_pop_col, thr_col, lower_col, upper_col,
                                 status_col, personal_thr, lower_equal_upper)
  .tbl <- blank_thresholds_for_negative_ages(.tbl, age_col, lower_col, upper_col)
  warn_on_na_thresholds(.tbl, lower_col, upper_col)

  return(.tbl)
}


#' Prepare thresholds for multiple phenotypes
#'
#' This function is a wrapper around \code{prepare_thresholds} to prepare thresholds for multiple phenotypes at once.
#'
#' @param .tbl Tibble with family and personal id columns, as well as CIP_merge_columns and status for each phenotype.
#' @param CIP_list List of tibbles with population representative cumulative incidence proportions. Each tibble must contain columns from \code{CIP_merge_columns} and \code{cIP_cip_col}.
#' @param phen_names Vector of phenotype names. Used to identify status columns and to name output columns.
#' @param CIP_merge_columns The columns the CIPs are subset by, e.g. CIPs by birth_year, sex. and age_col.
#' @param CIP_cip_col Name of column with CIP values.
#' @param age_col Name of column with age at the end of follow-up or age at diagnosis for cases.
#' @param age_eof_base Base name of age at end of follow-up column. The actual column name is constructed by appending the phenotype name. Defaults to "age_eof".
#' @param status_col_base Base name of status column. The actual column name is constructed by appending the phenotype name. Defaults to "status".
#' @param lower_base Base name of lower threshold column. The actual column name is constructed by appending the phenotype name. Defaults to "lower".
#' @param upper_base Base name of upper threshold column. The actual column name is constructed by appending the phenotype name. Defaults to "upper".
#' @param thr_col Base name of threshold column. The actual column name is constructed by appending the phenotype name. Defaults to "thr".
#' @param K_i_col Base name of individual cumulative incidence proportion column. The actual column name is constructed by appending the phenotype name. Defaults to "K_i".
#' @param K_pop_col Base name of population prevalence column. The actual column name is constructed by appending the phenotype name. Defaults to "K_pop".
#' @param Kpop_list List of population prevalence tibbles or "useMax" for each phenotype. If a tibble is provided, it must contain columns from \code{.tbl} and a column named "K_pop" with population prevalence values. Defaults to "UseMax".
#' @param personal_thr Should thresholds be based on stratified CIPs or population prevalence?
#' @param lower_equal_upper Should the upper and lower threshold be the same for cases? Can be used if CIPs are detailed, e.g. stratified by birth year and sex.
#' @param interpolation Type of interpolation, defaults to "xgboost".
#' @param bst.params List of parameters to pass on to xgboost. See xgboost documentation for details.
#' @param min_CIP_value Minimum cip value to allow. Too low values may lead to numerical instabilities.
#' @param xgboost_itr Number of iterations to run xgboost for.
#'
#' @importFrom dplyr %>% left_join select all_of
#' @importFrom stringr str_subset
#'
#' @return Tibble with (personlised) thresholds for each family member (lower & upper), the calculated cumulative incidence proportion for each individual (K_i), and population prevalence within an individuals CIP strata (K_pop; max value in stratum) for each phenotype. The threshold and other potentially relevant information can be added to the family graphs with \code{familywise_attach_attributes}.
#'
#' @examples
#' # TODO: create simple example
#'
#' @export
prepare_thresholds_multi = function(
    .tbl,
    CIP_list,
    phen_names,
    CIP_merge_columns = c("sex", "birth_year", "age"),
    CIP_cip_col = "cip",
    age_col = "age",
    age_eof_base = "age_eof",
    status_col_base = "status",
    lower_base = "lower",
    upper_base = "upper",
    thr_col = "thr",
    K_i_col = "K_i",
    K_pop_col = "K_pop",
    Kpop_list = "useMax",
    personal_thr = TRUE,
    lower_equal_upper = FALSE,
    interpolation = "xgboost",
    bst.params = list(
      max_depth = 10,
      base_score = 0,
      nthread = 4,
      min_child_weight = 10
    ),
    min_CIP_value = 1e-5,
    xgboost_itr = 30) {


  res = lapply(seq_along(phen_names), function(i) {
    cur_status_col = paste0(status_col_base, "_", phen_names[i])
    cur_lower_col  = paste0(lower_base, "_", phen_names[i])
    cur_upper_col  = paste0(upper_base, "_", phen_names[i])
    cur_thr_col    = paste0(thr_col, "_", phen_names[i])
    cur_K_i_col    = paste0(K_i_col, "_", phen_names[i])
    cur_K_pop_col  = paste0(K_pop_col, "_", phen_names[i])
    cur_age_eof_col= paste0(age_eof_base, "_", phen_names[i])

    # extract the correct Kpop entry, or set useMax
    if (Kpop_list == "useMax") {
      Kpop = "useMax"
    } else {
      Kpop = Kpop_list[[i]]
    }

    # Calculate thresholds for current phenotype
    tmp_res = prepare_thresholds(.tbl = .tbl %>% rename(!!as.symbol(age_col) := !!as.symbol(cur_age_eof_col)),
                                 CIP = CIP_list[[i]],
                                 age_col = age_col,
                                 CIP_merge_columns = CIP_merge_columns,
                                 CIP_cip_col = CIP_cip_col,
                                 Kpop = Kpop,
                                 status_col = cur_status_col,
                                 lower_col  = cur_lower_col,
                                 upper_col  = cur_upper_col,
                                 thr_col    = cur_thr_col,
                                 K_i_col    = cur_K_i_col,
                                 K_pop_col  = cur_K_pop_col,
                                 lower_equal_upper = lower_equal_upper,
                                 personal_thr = personal_thr,
                                 interpolation = interpolation,
                                 bst.params = bst.params,
                                 min_CIP_value = min_CIP_value,
                                 xgboost_itr = xgboost_itr) %>%
      rename(!!as.symbol(cur_age_eof_col) := !!as.symbol(age_col))

    # identify all columns with current phenotype name
    cur_phen_cols = str_subset(colnames(tmp_res), phen_names[i])
    # identify all columns with other phenotype names
    other_phen_cols = lapply(phen_names[-i], function(x) {
      str_subset(colnames(tmp_res), x)
    }) %>% unlist()

    # make sure we keep the required columns, lower, upper, thr, K_i, K_pop
    none_required_cur_phen_cols = setdiff(
      c(cur_phen_cols, other_phen_cols),
      c(cur_age_eof_col, cur_lower_col, cur_upper_col, cur_thr_col, cur_K_i_col, cur_K_pop_col))

    # remove all unnecessary columns
    tmp_res %>%
      select(-all_of(none_required_cur_phen_cols))

  })


  # find all columns, independent of outcomes, to merge on:
  to_merge_on = lapply(seq_along(phen_names), function(i) {
    str_subset(colnames(res[[i]]), phen_names[i], negate = T)
  }) %>% purrr::reduce(base::intersect)

  # merge all threshold results into one tibble and return it:
  purrr::reduce(res, left_join, by = to_merge_on)

}

Try the LTFGRS package in your browser

Any scripts or data that you put into this service are public.

LTFGRS documentation built on Nov. 20, 2026, 5:10 p.m.