Calculate lifetime risk of adverse outcomes accounting for competing risks.
- Person-year calculations
- Age-specific incidence rates
- Cumulative incidence with competing risks
- Age-adjusted rates
- Flexible group analysis (overall or subgroups)
- Scalable comparisons (any number of groups)
- Publication-quality plots
- Export results to CSV
- Thorough test suite for reliability
You can install the development version of lifetimeRisk from GitHub with:
# install.packages("remotes")
remotes::install_github("msegar/lifetimeRisk")Your data should contain:
ids: Unique identifier for each individualentryage: Age at which the individual entered the studysurvage: Age at which the individual was last observedstatus: Primary event indicator (1 if occurred, 0 if not)astatus: Competing event indicator (1 if occurred, 0 if not)- Grouping variables (optional): Any variables for subgroup analysis
Edge cases:
- All columns must be present and correctly named
- No missing values in required columns
- Ages should be numeric and within the analysis range
library(lifetimeRisk)
library(data.table)set.seed(123)
n <- 1000
sample_data <- data.frame(
ids = 1:n,
entryage = round(runif(n, min = 45, max = 65)),
followup_time = round(runif(n, min = 2, max = 20)),
status = rbinom(n, 1, 0.3),
astatus = rbinom(n, 1, 0.2),
sex = sample(c("Male", "Female"), n, replace = TRUE),
race = sample(c("White", "Black", "Hispanic", "Asian"), n, replace = TRUE,
prob = c(0.6, 0.2, 0.15, 0.05))
)
sample_data$survage <- sample_data$entryage + sample_data$followup_time
sample_data$status <- ifelse(sample_data$astatus == 1, 0, sample_data$status)
sample_data <- sample_data[, c("ids", "entryage", "survage", "status", "astatus", "sex", "race")]result_overall <- pie_analysis(
data = sample_data,
min_age = 45,
max_age = 90,
age_group_width = 5,
group_var = NULL,
age_free = 45
)
summary(result_overall)result_sex <- pie_analysis(
data = sample_data,
min_age = 45,
max_age = 90,
age_group_width = 5,
group_var = "sex",
group_levels = c("Male", "Female"),
age_free = 45
)
summary(result_sex)result_race <- pie_analysis(
data = sample_data,
min_age = 45,
max_age = 90,
age_group_width = 5,
group_var = "race",
group_levels = NULL,
age_free = 45
)
summary(result_race)library(ggplot2)
p <- plot_lifetime_risk(result_sex, adjusted = TRUE, label_names = c("Male" = "Men", "Female" = "Women"))
print(p)create_lifetime_csv(result_sex, adjusted = TRUE, output_file = "lifetime_risk_sex.csv")lt_overall <- create_lifetime_risk_table(
data = sample_data,
index_ages = c(45, 55, 65),
max_age = 90,
group_var = NULL
)
print(lt_overall)res <- calculate_disease_free_survival(
data = sample_data,
time_event_col = "survage",
time_death_col = "survage",
event_col = "status",
death_col = "astatus",
index_age = 45,
n_bootstrap = 100
)
print(res)pie_analysis(): Main function for person-year and lifetime risk analysis (overall or by group)create_person_year_data(): Expand data to person-year format for analysiscalculate_age_specific_rates(): Compute age-specific incidence ratescalculate_cumulative_incidence(): Compute cumulative incidence and confidence intervalscreate_lifetime_risk_table(): Summarize lifetime risk from different starting agesplot_lifetime_risk(): Plot cumulative incidence curves with confidence intervalscreate_lifetime_csv(): Export lifetime risk estimates to CSVcalculate_disease_free_survival(): Estimate disease-free and overall survival (restricted mean)get_number_at_risk_from_analysis(): Extract number at risk at specific ages from analysis
A comprehensive test suite is provided in tests/testthat/.
Run all tests with:
devtools::test()- All statistical results are validated against the original SAS PIE macro.
- Bootstrapping uses a fixed seed for reproducibility.
- Extensive tests cover edge cases and regression.
- See function documentation via
?function_namein R. - For detailed examples, see the vignettes and test files.
If you use lifetimeRisk in your research, please cite:
Segar, MW. (2025). lifetimeRisk: R package to calculate lifetime risk of adverse outcomes [Computer software]. Zenodo. https://doi.org/10.5281/zenodo.17117439
See LICENSE file for details.