Calculates mean sleep metrics separately for all nights, weekday nights,
and weekend/holiday nights. Output column names and derived metrics mirror
compute_sleep_metrics() from Julia Ribeiro da Silva Vallim's
pipeline_functions_fix27.py.
Usage
compute_sleep_metrics(x, ...)
# S3 method for class 'zeitr_result'
compute_sleep_metrics(
x,
min_tib_h = 5,
tz = "UTC",
holidays = x$holidays,
free_days = x$free_days,
...
)
# Default S3 method
compute_sleep_metrics(
x,
min_tib_h = 5,
tz = "UTC",
holidays = NULL,
free_days = c("Saturday", "Sunday"),
...
)Arguments
- x
A
zeitr_resultobject or atibbleof nightly sleep statistics as returned byrun_pipeline_native()orrun_pipeline(). Must contain at minimum the columnsis_nap,bed_time,get_up_time,tbt,tst,sol,soi,waso,eff.- ...
Not used; reserved for forward compatibility with future methods.
- min_tib_h
numeric(1). Minimum total in-bed time (hours) for a night to be included. Default is5.0(matching the Python reference).- tz
character(1). Time zone for extracting clock hours from timestamps. Default is"UTC".- holidays
Holidays to treat as free days in addition to the days in
free_days. Accepts three forms, which can be mixed in the same vector:Dateobjects or"YYYY-MM-DD"strings for year-specific dates (e.g.as.Date("2019-03-04")for one Carnival day)."DD-MM"strings for dates that recur every year (e.g."25-12"for Christmas,"01-01"for New Year). Default isNULL. Whenxis azeitr_result, defaults tox$holidaysautomatically. A warning is emitted whenNULL; suppress withoptions(zeitR.no_holidays_warn = FALSE).
- free_days
A character vector of day names (
"Monday"through"Sunday", case-insensitive) or ISO integers (1 = Monday ... 7 = Sunday) identifying which days of the week are unconditionally treated as free days. Default isc("Saturday", "Sunday"). Whenxis azeitr_result, defaults tox$free_daysautomatically.
Value
A named list with metrics for three groups (overall, wd =
workday, fd = free day):
n_overall,n_wd,n_fdNight counts.
sleep_onset_h,sleep_offset_hCircular mean onset and arithmetic mean offset in decimal hours.
fpr_tib_hMean TBT in hours.
fps_hMean free period sleep (TBT - SOL - SOI) in hours.
tst_hMean TST in hours.
latencia_min,inertia_minMean SOL and SOI in minutes.
waso_minMean WASO in minutes.
sleep_eff_pctMean sleep efficiency in percent (0-100).
tst_24h_hSame as
tst_h(24-h TST for main sleep only).dp_midsleep_min,dp_tst_minSD of mid-sleep and TST in minutes.
Workday and free-day metrics carry the suffix _wd and _fd.
Details
Dispatches on the class of x:
data.frame/tibble– treated as a nights table (same behaviour as the original function signature).zeitr_result– extractsx$nightsand inheritsx$holidaysandx$free_daysautomatically.
A night is assigned to the free-day group when the get-up date falls on
one of the days in free_days or matches an entry in holidays. Day-of-week
is determined via the ISO 8601 weekday number (format(date, "%u")) rather
than weekdays(), which is locale-dependent. All remaining nights are
workday nights.
sleep_onset_h uses a circular mean (values >= 12 h are shifted by -24 h
before averaging, then wrapped to [0, 24)). sleep_offset_h uses a plain
arithmetic mean.
fps_h (free period sleep) equals fpr_tib_h - (latencia_min + inertia_min) / 60 – TBT net of sleep onset latency and sleep inertia.
dp_midsleep_min and dp_tst_min are standard deviations of per-night
mid-sleep (minutes) and TST (minutes), respectively.
Examples
if (FALSE) { # \dontrun{
# From a zeitr_result: holidays and free_days forwarded automatically
result <- run_pipeline_native("recordings/P001.txt",
tz = "America/Sao_Paulo",
holidays = my_holidays,
free_days = c("Saturday", "Sunday"))
sm <- compute_sleep_metrics(result, tz = "America/Sao_Paulo")
# Non-standard schedule: Friday + Saturday as free days
sm <- compute_sleep_metrics(result$nights,
tz = "America/Sao_Paulo",
holidays = my_holidays,
free_days = c("Friday", "Saturday"))
sm$tst_h # mean TST in hours
sm$sleep_onset_h # mean sleep onset (circular, decimal hours)
sm$dp_midsleep_min # within-person SD of mid-sleep in minutes
} # }