Skip to contents

Calculates a baseline glucose value from the last 2 hours of the sleep period preceding wake onset, using the mean and standard deviation of that window. If fewer than min_points valid values are available, the window is expanded; if still insufficient, the entire cycle is used as a fallback.

Usage

compute_baseline(
  glucose,
  timestamps,
  wake_time,
  window_hours = 2,
  min_points = 4
)

Arguments

glucose

Numeric vector of glucose values for the sleep period preceding wake onset.

timestamps

POSIXct vector, same length as glucose.

wake_time

POSIXct scalar, timestamp of wake onset.

window_hours

Numeric, baseline window length in hours before wake. Default 2.

min_points

Integer, minimum number of valid points required before expanding the window. Default 4.

Value

A list with elements mean, sd, and n_points.