| Title: | Sleep Duration Estimation from Accelerometer Data |
|---|---|
| Description: | Estimates daily sleep duration from wrist or hip accelerometer data using a Pruned Dynamic Programming (PDP) changepoint algorithm. PDP partitions a minute-level activity series into segments, then identifies transitions between sleep and wakefulness by detecting extended periods of low physical activity. Five cost-function models (Poisson, Normal, Negative binomial, Variance, Exponential) accommodate different activity-count distributions. Optional non-wear detection and sleep diary integration are supported. Reference: Baek, Jonggyu, Banker, Margaret, Jansen, Erica C., She, Xichen, Peterson, Karen E., Pitchford, E. Andrew, Song, Peter X. K. (2021) An Efficient Segmentation Algorithm to Estimate Sleep Duration from Actigraphy Data <doi:10.1007/s12561-021-09309-3>. |
| Authors: | Jonggyu Baek [aut], Margaret Banker [aut], Nathan Szeto [aut, cre], Alice Cleynen [aut], Guillem Rigaill [aut], Michel Koskas [aut] |
| Maintainer: | Nathan Szeto <[email protected]> |
| License: | GPL-3 |
| Version: | 1.0.0 |
| Built: | 2026-05-17 08:32:38 UTC |
| Source: | https://github.com/n-szeto/actisleep |
A dataset containing accelerometry data for a single subject in 1-minute epochs, includes vector magnitude and other measurements.
date: Time and date of a single measurement.
axis1: X-axis movement.
axis2: Y-axis movement.
axis3: Z-axis movement.
steps: Steps taken by the subject.
lux: Amount of light present, measured in lux.
inclineOff: Incline off.
inclineStanding: Incline standing.
inclineSitting: Incline sitting.
inclineLying: Incline lying.
VM: Vector magnitude, calculated from x-, y-, and z-axis measurements.
data("AccelData")data("AccelData")
A data frame with 1351 rows and 11 variables.
Extract the segments data frame from an actisleep_result
## S3 method for class 'actisleep_result' as.data.frame(x, ...)## S3 method for class 'actisleep_result' as.data.frame(x, ...)
x |
An |
... |
Ignored. |
The segments data frame.
Applies a Pruned Dynamic Programming (PDP) segmentation algorithm to wrist or hip accelerometer data to identify transitions between sleep and wakefulness. The algorithm is described in Baek et al. (2021); this function wraps the full analysis pipeline including optional non-wear detection and diary-based validation.
estimate_sleep( data, cost_model = "poisson", subject_id = NA, activity_col = "max_count", threshold_pct = 0.4, segments_per_hour = 3, no_activity_cutoff = 0.7, min_sleep_minutes = 20, min_wake_minutes = 180, min_nap_minutes = 20, detect_nonwear = FALSE, min_wear_minutes = 120, use_diary = FALSE, diary = NULL, default_bedtime = "22:00:00", default_waketime = "08:00:00", tz = "GMT" )estimate_sleep( data, cost_model = "poisson", subject_id = NA, activity_col = "max_count", threshold_pct = 0.4, segments_per_hour = 3, no_activity_cutoff = 0.7, min_sleep_minutes = 20, min_wake_minutes = 180, min_nap_minutes = 20, detect_nonwear = FALSE, min_wear_minutes = 120, use_diary = FALSE, diary = NULL, default_bedtime = "22:00:00", default_waketime = "08:00:00", tz = "GMT" )
data |
Data frame of accelerometer data. Must contain a |
cost_model |
Cost function for the PDP algorithm. Either a string
( |
subject_id |
Optional subject identifier stored in the result. |
activity_col |
Name of the column containing the activity-count
variable to segment (e.g. |
threshold_pct |
Quantile (0–1) used to compute the person-specific
activity threshold; counts at or below this quantile are set to zero
before segmentation. Default |
segments_per_hour |
Number of PDP segments allocated per hour of
recording. Higher values allow finer time resolution at the cost of
computation. Default |
no_activity_cutoff |
Proportion (0–1) of zero-count epochs required
within a segment to classify it as no physical activity (NoPA).
Default |
min_sleep_minutes |
Minimum duration (minutes) for a segment to
qualify as sleep. Default |
min_wake_minutes |
Minimum duration (minutes) for a segment to
qualify as wake. Default |
min_nap_minutes |
Sleep segments shorter than this (minutes) that do
not overlap the diary window are excluded as naps. Default |
detect_nonwear |
Logical; if |
min_wear_minutes |
Minimum number of wear minutes required when
|
use_diary |
Logical; if |
diary |
Data frame with columns |
default_bedtime |
Default bed-time clock string used when
|
default_waketime |
Default wake-time clock string used when
|
tz |
Timezone string applied to all date-time operations.
Default |
Estimate daily sleep duration from accelerometer data
**Algorithm overview**
1. A person-specific activity threshold is computed as the
threshold_pct-th percentile of the activity counts. Counts at
or below this threshold are set to zero.
2. The zeroed series is segmented into pieces using the PDP changepoint algorithm
with the selected cost model.
3. Consecutive segments in which the proportion of zero-count epochs
exceeds no_activity_cutoff are merged and labelled as potential
sleep.
4. Candidate sleep segments are validated against the supplied (or default)
sleep window and filtered by minimum duration.
**Cost models**
| Value | Model | Best for |
|——-|——-|———-|
| "poisson" or 1 | Poisson | Non-negative integer counts |
| "normal" or 2 | Normal | Continuous, roughly symmetric data |
| "negative_binomial" or 3 | Negative binomial | Overdispersed integer counts |
| "variance" or 4 | Variance change | Constant-mean, changing variance |
| "exponential" or 5 | Exponential | Positive continuous waiting times |
An object of class actisleep_result (a list) with two
elements:
segmentsData frame of estimated sleep segments. Columns:
sleep_onset, sleep_offset (character timestamps),
duration_min (difftime),
pct_zero_activity (proportion),
no_activity_cutoff,
is_sleep (0/1),
is_no_activity (0/1),
diary_overlap (0/1),
diary_bedtime, diary_waketime (POSIXct),
n_sleep_segments (integer).
parametersNamed list recording the call parameters:
subject_id, cost_model, threshold_pct,
segments_per_hour, n_segments, valid_accel,
day_of_week.
Use as.data.frame.actisleep_result to extract the segments
data frame directly.
Baek, J., Banker, M., Jansen, E. C., She, X., Peterson, K. E., Pitchford, E. A., & Song, P. X. K. (2021). An Efficient Segmentation Algorithm to Estimate Sleep Duration from Actigraphy Data. Statistics in Biosciences. doi:10.1007/s12561-021-09309-3
read_agd for reading ActiGraph .agd files.
data("AccelData") # estimate_sleep() auto-detects common date formats result <- estimate_sleep(AccelData, activity_col = "VM") print(result) data("AccelData") result <- estimate_sleep(AccelData, activity_col = "VM", detect_nonwear = TRUE) data("AccelData") data("SleepDiary1Day") # Build a diary data frame with bed and wake columns. # estimate_sleep() auto-parses common character date-time formats. diary <- data.frame( bed = SleepDiary1Day$bed, wake = SleepDiary1Day$wake ) result <- estimate_sleep( AccelData, cost_model = "normal", activity_col = "VM", threshold_pct = 0, detect_nonwear = TRUE, segments_per_hour = 2, no_activity_cutoff = 0.45, min_sleep_minutes = 5, use_diary = TRUE, diary = diary ) print(result)data("AccelData") # estimate_sleep() auto-detects common date formats result <- estimate_sleep(AccelData, activity_col = "VM") print(result) data("AccelData") result <- estimate_sleep(AccelData, activity_col = "VM", detect_nonwear = TRUE) data("AccelData") data("SleepDiary1Day") # Build a diary data frame with bed and wake columns. # estimate_sleep() auto-parses common character date-time formats. diary <- data.frame( bed = SleepDiary1Day$bed, wake = SleepDiary1Day$wake ) result <- estimate_sleep( AccelData, cost_model = "normal", activity_col = "VM", threshold_pct = 0, detect_nonwear = TRUE, segments_per_hour = 2, no_activity_cutoff = 0.45, min_sleep_minutes = 5, use_diary = TRUE, diary = diary ) print(result)
Displays a concise, formatted summary of the sleep estimation output.
## S3 method for class 'actisleep_result' print(x, ...)## S3 method for class 'actisleep_result' print(x, ...)
x |
An |
... |
Ignored. |
x invisibly.
Read an ActiGraph .agd file (SQLite format) and return device settings and raw accelerometer data.
read_agd(file, tz = "GMT", sec = 10)read_agd(file, tz = "GMT", sec = 10)
file |
Path to an AGD data file. |
tz |
Timezone string, default |
sec |
Epoch length in seconds used to construct timestamps, default 10. |
A list with two elements:
Data frame of device settings metadata.
Data frame with columns date and all
accelerometer channels (axis1, axis2, axis3, steps, lux, etc.).
This code was written by Alice Cleynen, Guillem Rigaill, and Michel Koskas as part of the Segmentor3IsBack package, which is no longer in CRAN. It has been imported into the ActiSleep package to ensure this package's longevity.
A small dataset containing sleep diary data for a single day and single subject
bed. time at which the subject reported going to sleep on day 1
wake. time at which the subject reported waking up on day 1
data("SleepDiary1Day")data("SleepDiary1Day")
a data frame with 1 row and 2 variables
A small dataset containing sleep diary data for a single week and single subject
FOLIOCC. unique subject id
etapa.
stage.
start_day. first day of measurement
time_bed1. time at which the subject reported going to sleep on day 1
time_wake1. time at which the subject reported waking up on day 1
time_bed2. time at which the subject reported going to sleep on day 2
time_wake2. time at which the subject reported waking up on day 2
time_bed3. time at which the subject reported going to sleep on day 3
time_wake3. time at which the subject reported waking up on day 3
time_bed4. time at which the subject reported going to sleep on day 4
time_wake4. time at which the subject reported waking up on day 4
time_bed5. time at which the subject reported going to sleep on day 5
time_wake5. time at which the subject reported waking up on day 5
time_bed6. time at which the subject reported going to sleep on day 6
time_wake6. time at which the subject reported waking up on day 6
time_bed7. time at which the subject reported going to sleep on day 7
time_wake7. time at which the subject reported waking up on day 7
data("SleepDiary1Week")data("SleepDiary1Week")
a data frame with 1 row and 18 variables
Prints key aggregate statistics: total sleep time, number of sleep segments, and average proportion of zero-activity epochs.
## S3 method for class 'actisleep_result' summary(object, ...)## S3 method for class 'actisleep_result' summary(object, ...)
object |
An |
... |
Ignored. |
object invisibly.