Builds a stage-to-stage transition probability matrix and derives fragmentation indices from a staged hypnogram. Works with both full AASM and coarse actigraphy-derived staging.
Arguments
- hypnogram
A
hypnor_hypnogramobject as returned bynew_hypnogram()orread_hypnogram(), or any data frame with at minimumepochandstagecolumns – it will be passed throughnew_hypnogram()automatically if not already ahypnor_hypnogram.- normalise
If
TRUE(default), each row of the transition count matrix is divided by its row sum to give transition probabilities. Rows for a from-stage that is never visited are returned asNArather thanNaN. IfFALSE, raw transition counts are returned.- include_wake
If
TRUE(default),"W"is included as a state in the transition matrix like any other stage (includingW->Wself-transitions). IfFALSE, the matrix is restricted to sleep-stage transitions only: any transition into or out of"W"is excluded before the matrix is built.
Value
A list with two elements:
- matrix
A tibble with one row per from stage: a
fromcolumn plus one numeric column per to stage (transition probabilities or counts).- fragmentation
A one-row tibble with:
- n_transitions
Number of epoch-to-epoch stage changes (self-transitions do not count).
- fragmentation_index
Proportion of epochs that are followed by a different stage.
- wake_transitions
Number of transitions into Wake (proxy for arousal burden).
Details
Fragmentation metrics (n_transitions, fragmentation_index,
wake_transitions) are always computed from the full epoch sequence,
wake included, regardless of include_wake – that argument only
controls the shape of the returned matrix.
Examples
if (FALSE) { # \dontrun{
hyp <- read_hypnogram("night_001.csv")
trans <- compute_transitions(hyp)
trans$matrix
trans$fragmentation
# Sleep-stage transitions only, excluding Wake
compute_transitions(hyp, include_wake = FALSE)
} # }