maxima_grid: Combined Maxima Detection and GRID Analysis

View source: R/function_overrides.R View source: R/RcppExports.R

maxima_gridR Documentation

Combined Maxima Detection and GRID Analysis

Description

Fast method for postprandial glucose peak detection combining GRID algorithm with local maxima analysis. Detects meal-induced glucose peaks by identifying GRID events (rapid glucose increases) and mapping them to corresponding local maxima within a search window. Local maxima are defined as points where glucose values increase or remain constant for two consecutive points before the peak, and decrease or remain constant for two consecutive points after the peak.

The 7-step algorithm: (1) finds GRID points indicating meal starts (2) identifies modified GRID points after minimum duration (3) locates maximum glucose within the subsequent time window (4) detects all local maxima using the two-consecutive-point criteria (5) refines peaks from local maxima candidates (6) maps GRID points to peaks within 4-hour constraint (7) redistributes overlapping peaks.

Usage

maxima_grid(df, threshold = 130, gap = 60, hours = 2)

Arguments

df

A dataframe containing continuous glucose monitoring (CGM) data. Must include columns:

  • id: Subject identifier (string or factor)

  • time: Time of measurement (POSIXct)

  • gl: Glucose value (integer or numeric, mg/dL)

threshold

GRID slope threshold in mg/dL/hour for event classification (default: 130)

gap

Gap threshold in minutes for event detection (default: 60). This parameter defines the minimum time interval between consecutive GRID events.

hours

Time window in hours for maxima analysis (default: 2)

Value

A list containing:

  • results: Tibble with combined maxima and GRID analysis results, with columns:

    • id: Subject identifier

    • grid_time: Timestamp of GRID event detection (POSIXct)

    • grid_gl: Glucose value at GRID event (mg/dL)

    • maxima_time: Timestamp of peak glucose (POSIXct)

    • maxima_glucose: Peak glucose value (mg/dL)

    • time_to_peak_min: Time from GRID event to peak in minutes

    • grid_index: R-based (1-indexed) row number of GRID event; grid_time == df$time[grid_index], grid_gl == df$gl[grid_index]

    • maxima_index: R-based (1-indexed) row number of peak; maxima_time == df$time[maxima_index], maxima_glucose == df$gl[maxima_index]

  • episode_counts: Tibble with episode counts per subject (id, episode_counts)

Algorithm (7 steps)

1) GRID → 2) modified GRID → 3) window maxima → 4) local maxima → 5) refine peaks → 6) map GRID to peaks (\leq 4h) → 7) redistribute overlapping peaks.

See Also

grid, mod_grid, find_local_maxima, find_new_maxima, transform_df

Other GRID pipeline: detect_between_maxima(), find_local_maxima(), find_max_after_hours(), find_max_before_hours(), find_min_after_hours(), find_min_before_hours(), find_new_maxima(), grid(), mod_grid(), start_finder(), transform_df()

Examples

# Load sample data
library(iglu)
data(example_data_5_subject)
data(example_data_hall)

# Combined analysis on smaller dataset
maxima_result <- maxima_grid(example_data_5_subject, threshold = 130, gap = 60, hours = 2)
print(maxima_result$episode_counts)
print(maxima_result$results)

# More sensitive analysis
sensitive_maxima <- maxima_grid(example_data_5_subject, threshold = 120, gap = 30, hours = 1)
print(sensitive_maxima$episode_counts)
print(sensitive_maxima$results)

# Analysis on larger dataset
large_maxima <- maxima_grid(example_data_hall, threshold = 130, gap = 60, hours = 2)
print(large_maxima$episode_counts)
print(large_maxima$results)

cgmguru documentation built on Nov. 6, 2025, 1:07 a.m.