Calculate Person-Years by Age Strata
Source:R/calculate_person_years_by_age_strata.R
calculate_person_years_by_age_strata.Rd
This function calculates person-years of follow-up by age strata for cohort studies. It handles age transitions during follow-up by splitting person-time contributions across age groups as individuals age during the observation period.
Usage
calculate_person_years_by_age_strata(
.df,
age_cut_points = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90, 95, 100, 150)
)
Arguments
- .df
A data frame containing individual-level follow-up data
- age_cut_points
Numeric vector of age breakpoints defining the age strata. Default is 5-year age groups from 0 to 100+:
c(0, 5, 10, ..., 95, 100, 150)
. This matches the WHO 2000-2025 Standard Population age groups. The final age group is displayed as "[100+)" regardless of the upper cut point value.
Value
A tibble containing:
age_group: Age group labels (factor)
person_years: Total person-years of follow-up in each age group
events: Number of events in each age group
Details
The function requires the following columns in .df
:
patient_id: Unique identifier for each individual
dob: Date of birth (Date class)
study_entry_date: Date of study entry (Date class)
study_exit_date: Date of study exit/end of follow-up (Date class)
event_status: Event indicator (integer: 1 = event occurred, 0 = censored)
The function automatically handles individuals who transition between age groups during follow-up by splitting their person-time contributions appropriately.
Examples
# Example data
cohort_data <- data.frame(
patient_id = 1:3,
dob = as.Date(c("1960-01-15", "1955-06-20", "1972-09-01")),
study_entry_date = as.Date(c("2020-03-01", "2021-01-01", "2019-01-01")),
study_exit_date = as.Date(c("2024-06-15", "2025-12-31", "2022-04-10")),
event_status = c(1L, 0L, 1L)
)
# Calculate person-years by 5-year age groups
result <- calculate_person_years_by_age_strata(cohort_data)
# Custom age groups
custom_ages <- c(0, 20, 40, 60, 80, 100)
result_custom <- calculate_person_years_by_age_strata(cohort_data, custom_ages)