check_insulin_totals: Check that the summed insulin matches the daily totals...

View source: R/05_checks.R

check_insulin_totalsR Documentation

Check that the summed insulin matches the daily totals reported by an export

Description

Compares the insulin summed over the individual data points with the daily total reported by the export itself. Only daily totals with source == "reported" are checked: where aidR summed the total itself (source == "summed"), the two agree by construction. In practice this means Glooko (basal and bolus) and Tandem Source (basal only).

Usage

check_insulin_totals(data, types = "basal", rel_tol = 0.2, abs_tol = 1)

Arguments

data

A list with all data found for one individual (the output of parse_data) or for multiple individuals (the output of merge_all). Requires cleaned and standardized data.

types

Character vector with the types of insulin that are checked, any of "basal" and "bolus".

rel_tol

Numeric, the relative tolerance of the comparison.

abs_tol

Numeric, the absolute tolerance of the comparison, in units of insulin. A day is flagged when the absolute difference exceeds both abs_tol and rel_tol times the reported total.

Details

Accepts either the output of parse_data for a single individual or the output of merge_all for multiple individuals. Every individual found in the data is checked separately.

By default, only the basal insulin is checked, which is where the mismatches described below arise. Pass types = c("basal", "bolus") to check the bolus insulin as well.

A large mismatch means that the individual data points do not cover the whole day. The most common cause are AID systems that record basal rates for manual mode only (e.g. Omnipod on Glooko): the basal insulin delivered in automated mode is missing from the basal rates, but is included in the reported total.

Note that the first and the last day of an export are usually only partially covered, so a mismatch on those days is expected.

Value

Invisibly, a data frame with one row per checked individual, day and type, holding the summed and the reported total and whether the two agree. Called for its side effects: a message is issued for every day where they do not.

Examples

path <- system.file("extdata", "glooko_example.zip", package = "aidR")
data <- parse_data(id = "1", paths = path)
check_insulin_totals(data)

# Check bolus insulin as well, and keep the comparison
res <- check_insulin_totals(data, types = c("basal", "bolus"))
res

aidR documentation built on Oct. 28, 2026, 5:13 p.m.