Find Maximum Glucose Before Specified Hours
Source:R/cgmguru-functions-docs.R
find_max_before_hours.RdIdentifies the maximum glucose value occurring within a specified time window before a given start point. This function is useful for analyzing glucose patterns preceding specific events or time points.
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)
- start_point_df
A dataframe with column
start_index(R-based index intodf)- hours
Number of hours to look back from the start point
Value
A list containing:
max_index: Tibble with R-based (1-indexed) row numbers of maximum glucose (max_index). The corresponding occurrence time isdf$time[max_index]and glucose isdf$gl[max_index].episode_counts: Tibble with episode counts per subject (id,episode_counts)episode_start: Tibble with all episode starts with columns:id: Subject identifiertime: Timestamp at which the maximum occurs; equivalent todf$time[index]gl: Glucose value at the maximum; equivalent todf$gl[index]index: R-based (1-indexed) row number(s) indfdenoting where the maximum occurs
Examples
# Load sample data
library(iglu)
data(example_data_5_subject)
data(example_data_hall)
# Create start points for demonstration (using row index)
start_index <- seq(1, nrow(example_data_5_subject), by = 100)
start_points <- data.frame(start_index = start_index)
# Find maximum glucose in previous 2 hours
max_before <- find_max_before_hours(example_data_5_subject, start_points, hours = 2)
print(paste("Found", length(max_before$max_index), "maximum points"))
#> [1] "Found 1 maximum points"
# Find maximum glucose in previous 1 hour
max_before_1h <- find_max_before_hours(example_data_5_subject, start_points, hours = 1)
# Analysis on larger dataset
large_start_index <- seq(1, nrow(example_data_hall), by = 200)
large_start_points <- data.frame(start_index = large_start_index)
large_max_before <- find_max_before_hours(example_data_hall, large_start_points, hours = 2)
print(paste("Found", length(large_max_before$max_index), "maximum points in larger dataset"))
#> [1] "Found 1 maximum points in larger dataset"