Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apnea Plots
require("tidyverse")
require("ggplot2")
require("dplyr")
## install.packages("tidyverse")
require("lubridate")
pressure_begin6 = as.Date("2019-11-01")
pressure_begin10 = as.Date("2019-12-09")
pressure_begin6_r2 = as.Date("2019-12-23")
pressure_begin10_r2 = as.Date("2020-01-03")
pressure_begin12 = as.Date("2020-01-06")
bad <- c(as.Date("2019-12-19"), as.Date("2019-12-20"), as.Date("2019-12-30"), as.Date("2019-12-23"), as.Date("2019-12-24"), as.Date("2020-01-03"))
pressureClass <- function(date) {
if (date %in% bad)
return ("BAD")
else if ((date >= pressure_begin6) & (date < pressure_begin10))
return("6 to 12")
else if ((date >= pressure_begin10) & (date < pressure_begin6_r2))
return("10 to 12")
else if ((date >= pressure_begin6_r2) & (date < pressure_begin10_r2))
return("6 to 12")
else if ((date >= pressure_begin10_r2) & (date < pressure_begin12))
return("10 to 12")
else if ((date >= pressure_begin12))
return("12")
else
return("Initial 6 to 12")
}
cervicalCollar_begin <- as.Date("2020-01-11")
cervicalCollar_exceptions <- c(as.Date("2020-01-15"))
cervicalCollar <- function(date) {
if ((date >= cervicalCollar_begin) & (!date %in% cervicalCollar_exceptions))
return("Yes")
else
return("No")
}
overallClassification <- function(pressureClass, cervicalCollar) {
if (cervicalCollar == "Yes") {
return (paste(pressureClass, ", with collar", sep = ""))
} else {
return (paste(pressureClass))
}
}
rawDaily <- read.delim("./sleep-daily.csv", header = TRUE, sep = ",",
colClasses = c("Date" = "character", "Total.Time" = "character")) %>%
mutate(
Date = as.Date(Date),
cervicalCollar = as.factor(mapply(cervicalCollar, Date)),
pressureClass = as.factor(mapply(pressureClass, Date)),
Total.Hours = (period_to_seconds(hms(Total.Time)) / (60* 60))) %>%
mutate(
CA.PerHour = CA.Count / Total.Hours,
OA.PerHour = OA.Count / Total.Hours,
H.PerHour = H.Count / Total.Hours,
overallClass = factor(
mapply(overallClassification, pressureClass, cervicalCollar),
levels = c("Initial 6 to 12", "6 to 12", "10 to 12", "12", "12, with collar"))) %>%
subset(select=c("Date", "CA.PerHour", "OA.PerHour", "H.PerHour", "AHI", "pressureClass", "cervicalCollar", "overallClass", "Total.Hours"))
daily <- rawDaily %>%
filter(pressureClass != "BAD") %>%
filter(pressureClass != "Initial 6 to 12") %>%
tidyr::pivot_longer(cols=c("CA.PerHour", "OA.PerHour", "H.PerHour", "AHI"), names_to = "metric") %>%
mutate(metric = as.factor(metric))
dailyWithoutCervicalCollar <- daily %>%
filter(cervicalCollar != "Yes")
ggplot(dailyWithoutCervicalCollar, aes(pressureClass, value)) +
geom_boxplot() +
geom_jitter() +
xlab("CPAP settings") +
ylab("Value") +
labs(title = "Events-per-hour for different event types, by CPAP settings") +
facet_grid(cols = vars(metric))
ggplot(daily, aes(cervicalCollar, value)) +
geom_boxplot() +
geom_jitter() +
xlab("Wearing cervical collar: yes or no") +
ylab("Value") +
labs(title = "Events-per-hour for different event types, by cervical collar status") +
facet_grid(cols = vars(metric))
friendlyApneaLabels <- as_labeller(c(`AHI` = "Overall Score", `OA.PerHour` = "Obstructive Apneas / Hour", `CA.PerHour` = "Central Apneas / Hour", `H.PerHour` = "Hypopneas / Hour"))
ggplot(daily, aes(overallClass, value)) +
geom_boxplot() +
geom_jitter() +
xlab("Sleep Configuration") +
ylab("Score") +
labs(title = "Nightly Apnea Scores for Different Types, by CPAP Setting and Cervical Collar Wearing", caption = "") +
facet_grid(cols = vars(metric), labeller = friendlyApneaLabels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.