View source: R/function_overrides.R View source: R/RcppExports.R
| detect_all_events | R Documentation |
Comprehensive function to detect all types of glycemic events aligned with
international consensus CGM metrics (Battelino et al., 2023). This function
provides a unified interface for detecting multiple event types including
Level 1/2/Extended hypo- and hyperglycemia, Level 1 excluded events, and
rebound hypo-/hyperglycemia summaries.
Rebound hypoglycemia and hyperglycemia definitions follow Hansen and Bibby
(2024).
Events are counted only after the required recovery condition is confirmed;
duration summaries use the event boundary immediately before recovery starts.
Event preprocessing uses cgmguru's independent C++ implementation of an
iglu-compatible day-based grid: each subject is interpolated from the first
observed day's midnight plus one reading interval, rather than from the first
observed timestamp. Larger gaps are masked and removed before event
classification, preserving gap-based segment boundaries. This preprocessing
is specific to event calculation and does not affect grid,
maxima_grid, or excursion.
CGM summary metrics in subject_summary are calculated from the original
raw glucose values by default. Set
summary_metrics_source = "preprocessed" to calculate them from the
internal event-preprocessed grid. Numeric summary outputs are rounded to
summary_digits decimal places; set summary_digits = NULL or
summary_digits = "none" to return unrounded values.
detect_all_events(df, reading_minutes = NULL, sort_time = FALSE,
inter_gap = 45, return_interpolated = FALSE,
summary_metrics_source = c("raw", "preprocessed"),
sensor_wear_ndays = NULL, summary_digits = 2)
df |
A dataframe containing continuous glucose monitoring (CGM) data. Must include columns:
|
reading_minutes |
Time interval between readings in minutes (optional).
Can be a single integer/numeric value (applied to all subjects), a vector
matching data length, or |
sort_time |
Logical. If |
inter_gap |
Maximum gap in minutes to interpolate across. Defaults to 45; larger gaps split event-detection segments. |
return_interpolated |
Logical. If |
summary_metrics_source |
Character. Source glucose values for CGM
summary metrics. Defaults to |
sensor_wear_ndays |
Number of days for fixed-window
|
summary_digits |
Number of decimal places for numeric summary outputs
in |
A list containing:
subject_summary: One row per subject. CGM summary metric columns
are calculated on the original raw glucose values by default; set
summary_metrics_source = "preprocessed" to use the
event-preprocessed glucose grid. Event summaries are included as wide
*_total_episodes columns only. Numeric summary columns are
rounded according to summary_digits.
glycemic_event_summary: One row per subject, event
type, and event level. Contains the full event summary: id,
type, level, total_episodes,
avg_ep_per_day, and
avg_minutes_below_54_per_episode.
interpolated_data: Included when
return_interpolated = TRUE, with columns id, time,
and gl.
subject_summary includes:
id: Subject identifier
TIR: Percent of glucose readings in range 70-180 mg/dL
TITR: Percent of glucose readings in tight range 70-140 mg/dL
TBR70: Percent of glucose readings below 70 mg/dL
TBR54: Percent of glucose readings below 54 mg/dL
TAR180: Percent of glucose readings above 180 mg/dL
TAR250: Percent of glucose readings above 250 mg/dL
CV: Coefficient of variation in percent,
100 * SD / mean_glucose
SD: Sample standard deviation of glucose, mg/dL
mean_glucose: Mean glucose, mg/dL
GMI: Glucose Management Indicator,
3.31 + 0.02392 * mean_glucose
uGMI: Unitless GMI-style metric,
1 / (15.36 / mean_glucose + 0.0425)
GRI: Glycemia Risk Index,
3.0 * VLow + 2.4 * Low + 1.6 * VHigh + 0.8 * High, where
VLow is percent time <54 mg/dL, Low is 54-<70
mg/dL, VHigh is >250 mg/dL, and High is
>180-\leq250 mg/dL
sensor_wear_percent: Percent of expected CGM readings observed,
calculated from the original timestamps using the same automatic range
method as iglu::active_percent() by default. If
sensor_wear_ndays is supplied, this is calculated over the last
N days for each subject.
hypo_lv1_total_episodes: Number of Level 1 hypoglycemia
episodes
hypo_lv2_total_episodes: Number of Level 2 hypoglycemia
episodes
hypo_extended_total_episodes: Number of extended
hypoglycemia episodes
hypo_lv1_excl_total_episodes: Number of Level 1
hypoglycemia episodes that do not overlap a Level 2 episode
hypo_rebound_total_episodes: Number of rebound
hypoglycemia episodes
hyper_lv1_total_episodes: Number of Level 1 hyperglycemia
episodes
hyper_lv2_total_episodes: Number of Level 2 hyperglycemia
episodes
hyper_extended_total_episodes: Number of extended
hyperglycemia episodes
hyper_lv1_excl_total_episodes: Number of Level 1
hyperglycemia episodes that do not overlap a Level 2 episode
hyper_rebound_total_episodes: Number of rebound
hyperglycemia episodes
glycemic_event_summary includes:
id: Subject identifier
type: Event direction, either "hypo" or
"hyper"
level: Event level, one of "lv1", "lv2",
"extended", "lv1_excl", or "rebound"
total_episodes: Number of episodes for the subject, event
direction, and event level
avg_ep_per_day: Average episodes per day for the subject,
event direction, and event level, rounded according to
summary_digits
avg_minutes_below_54_per_episode: For hypoglycemia rows,
average minutes below 54 mg/dL per episode, rounded according to
summary_digits; for hyperglycemia rows, 0
- Hypoglycemia: lv1 (< 70 mg/dL, \geq 15 min), lv2 (< 54 mg/dL, \geq 15 min), extended (< 70 mg/dL, \geq 120 min).
- Hyperglycemia: lv1 (> 180 mg/dL, \geq 15 min), lv2 (> 250 mg/dL, \geq 15 min), extended (> 250 mg/dL, \geq 90 min in 120 min, end \leq 180 mg/dL for \geq 15 min).
- Rebound: hypo rebound is Level 1 hyperglycemia followed by <70 mg/dL within 120 minutes; hyper rebound is Level 1 hypoglycemia followed by >180 mg/dL within 120 minutes.
Battelino, T., et al. (2023). Continuous glucose monitoring and metrics for clinical trials: an international consensus statement. The Lancet Diabetes & Endocrinology, 11(1), 42-57.
Hansen, K. W., and Bibby, B. M. (2024). Rebound hypoglycemia and hyperglycemia in type 1 diabetes. Journal of Diabetes Science and Technology, 18(6), 1392-1398.
detect_hyperglycemic_events, detect_hypoglycemic_events, rebound_events
# Load sample data
library(iglu)
data(example_data_5_subject)
data(example_data_hall)
# Detect all glycemic events; reading_minutes is calculated automatically
# from the timestamp spacing when omitted
all_outputs <- detect_all_events(example_data_5_subject)
print(all_outputs$subject_summary)
print(all_outputs$glycemic_event_summary)
# Detect all events on larger dataset
large_outputs <- detect_all_events(example_data_hall)
print(paste("Total subjects analyzed:", nrow(large_outputs$subject_summary)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.