Count and format stages transitions.
transitions(
hypnogram,
stages = c("AWA", "REM", "N1", "N2", "N3", "NREM"),
format = "vector"
)
A hypnogram dataframe. Dataframe must contain begin
(POSIXt
), end
(POSIXt
) and event
(character
) columns.
Stages to include in transitions Defaults to c("N1", "N2", "N3", "N4", "REM")
.
Set the return format. 'vector', 'dataframe' or 'heatmap'.
Count of stages transitions in selected format.
Swihart BJ, Punjabi NM, Crainiceanu CM. Modeling sleep fragmentation in sleep hypnograms: An instance of fast, scalable discrete-state, discrete-time analyses. Comput Stat Data Anal. 2015 Sep;89:1-11. doi: 10.1016/j.csda.2015.03.001. PMID: 27182097; PMCID: PMC4865264.
tryCatch({
download.file("https://rsleep.org/data/hypnodensity.csv", "hypnodensity.csv")
hypnodensity <- read.csv2("hypnodensity.csv")
unlink("hypnodensity.csv")
events <- hypnogram(hypnodensity)
transitions(events)
transitions(events, format = "dataframe")
transitions(events, format = "heatmap")
# 3 Dimensions sleep transitions
levels(events$event)[levels(events$event)=="N1"] <- "NREM"
levels(events$event)[levels(events$event)=="N2"] <- "NREM"
levels(events$event)[levels(events$event)=="N3"] <- "NREM"
round(
transitions(
events,
format = "dataframe")/(
sum(transitions(events)))*100,2)
}, error = function(e) {
print("Error executing this example, check your internet connection.")
})
#> AWA REM NREM
#> AWA 8.16 0.61 3.06
#> REM 1.77 18.29 5.91
#> NREM 1.84 7.07 53.30