README.md

Table of Contents

KFRE Risk Prediction Tools (R)

kfre is an R implementation of helpers around the Kidney Failure Risk Equation (KFRE), including:

Installation

install.packages("kfre")

GitHub (Development)

# install.packages("remotes")
remotes::install_github("lshpaner/kfre_r")

Dependencies

Core imports: R6, stats, ggplot2, pROC, precrec Suggested for tests/vignettes: testthat (>= 3.0.0), knitr, rmarkdown

Quick Start

1. Toy data

toy <- data.frame(
  age = c(55, 72),
  sex_txt = c("male", "female"),
  eGFR = c(45, 28),
  uACR = c(120, 800),
  dm = c(1, 0),
  htn = c(1, 1),
  albumin = c(4.2, 3.4),
  phosphorous = c(3.3, 4.6),
  bicarbonate = c(24, 22),
  calcium = c(9.1, 9.8),
  stringsAsFactors = FALSE
)

cols <- list(
  age = "age",
  sex = "sex_txt",
  eGFR = "eGFR",
  uACR = "uACR",
  dm = "dm",
  htn = "htn",
  albumin = "albumin",
  phosphorous = "phosphorous",
  bicarbonate = "bicarbonate",
  calcium = "calcium"
)

2. Vectorized predictions with RiskPredictor

rp <- RiskPredictor$new(df = toy, columns = cols)

# 4-variable KFRE (2-year), North America constants
p4_2y <- rp$predict_kfre(
  years = 2, is_north_american = TRUE,
  use_extra_vars = FALSE, num_vars = 4
)

# 6-variable KFRE (5-year)
p6_5y <- rp$predict_kfre(
  years = 5, is_north_american = TRUE,
  use_extra_vars = TRUE, num_vars = 6
)

# 8-variable KFRE (2-year)
p8_2y <- rp$predict_kfre(
  years = 2, is_north_american = TRUE,
  use_extra_vars = TRUE, num_vars = 8
)

p4_2y
p6_5y
p8_2y

3. Single-person predictions

# Male, 55yo, 2-year risk (4-var)
rp$kfre_person(
  age = 55, is_male = TRUE,
  eGFR = 45, uACR = 120,
  is_north_american = TRUE, years = 2
)

# Female, 72yo, 5-year risk (6-var)
rp$kfre_person(
  age = 72, is_male = FALSE,
  eGFR = 28, uACR = 800,
  is_north_american = TRUE, years = 5,
  dm = 0, htn = 1
)

# Female, 72yo, 2-year risk (8-var)
rp$kfre_person(
  age = 72, is_male = FALSE,
  eGFR = 28, uACR = 800,
  is_north_american = TRUE, years = 2,
  albumin = 3.4, phosphorous = 4.6, bicarbonate = 22, calcium = 9.8
)

4. Add KFRE risk columns to a data.frame

toy_kfre <- add_kfre_risk_col(
  df = toy,
  age_col = "age",
  sex_col = "sex_txt",
  eGFR_col = "eGFR",
  uACR_col = "uACR",
  dm_col = "dm",
  htn_col = "htn",
  albumin_col = "albumin",
  phosphorous_col = "phosphorous",
  bicarbonate_col = "bicarbonate",
  calcium_col = "calcium",
  num_vars = c(4, 6, 8),
  years = c(2, 5),
  is_north_american = TRUE,
  copy = TRUE
)

names(toy_kfre)
head(toy_kfre)
# Adds:
# kfre_4var_2year, kfre_4var_5year,
# kfre_6var_2year, kfre_6var_5year,
# kfre_8var_2year, kfre_8var_5year

5. CKD staging & ESRD outcome labels

# ESRD outcome within 2 years (duration is in days → converted to years)
out <- data.frame(
  eGFR = c(95, 25),
  ESRD_flag = c(1, 1),
  followup_days = c(200, 1000)
)

out <- class_esrd_outcome(
  df = out,
  col = "ESRD_flag",
  years = 2,
  duration_col = "followup_days",
  prefix = "esrd",
  create_years_col = TRUE
)
# Adds: ESRD_duration_years and esrd_2_year_outcome

# CKD stage labels
out <- class_ckd_stages(
  df = out,
  egfr_col = "eGFR",
  stage_col = "stage",
  combined_stage_col = "stage_combined"
)

table(out$stage)
table(out$stage_combined)

6. uPCR → uACR conversion

df_pcr <- data.frame(
  sex = c("female","male","female"),
  dm  = c(1,0,1),
  htn = c(1,1,0),
  pcr = c(150, 600, 50)
)

acr <- upcr_uacr(
  df_pcr,
  sex_col = "sex",
  diabetes_col = "dm",
  hypertension_col = "htn",
  upcr_col = "pcr",
  female_str = "female"
)

acr

7. Evaluation metrics (AUC-ROC, AP, Brier…)

Your data.frame must include:

met <- eval_kfre_metrics(
  df = toy_kfre,                 # must contain truth + prediction columns
  n_var_list = c(4, 6, 8),
  outcome_years = c(2, 5),
  decimal_places = 4
)

met
# Rows: Metrics; Cols: "{2_year|5_year}_{4|6|8}_var_kfre"

8. Plot ROC / PR curves

# Basic: compute & plot both ROC and PR (no files written)
plot_kfre_metrics(
  df = toy_kfre,
  num_vars = c(4, 6, 8),
  plot_type = "all_plots",
  mode = "both",              # compute + plot
  show_years = c(2, 5)
)

# Save to disk (PNG/SVG)
plot_kfre_metrics(
  df = toy_kfre,
  num_vars = c(4, 6),
  plot_type = "auc_roc",
  mode = "both",
  show_years = c(2, 5),
  save_plots = TRUE,
  image_path_png = "plots",
  image_prefix = "kfre"
)

Running Tests

If you’ve cloned the repo:

library(devtools)
devtools::load_all(".")
devtools::test()

You should see unit tests for both the end-to-end flow and the evaluation utilities.

API surface (exports)

Notes on parity with Python

The R implementations are designed to mirror the Python versions (naming, shapes, and expected columns). Where packages differ (e.g., ROC/PR computation), we use pROC and precrec to maintain metric parity.

References

License

kfre is distributed under the MIT License. See LICENSE for more information.



Try the kfre package in your browser

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

kfre documentation built on Aug. 28, 2025, 9:09 a.m.