-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_events.R
187 lines (160 loc) · 8.09 KB
/
find_events.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
## detect events, first run the import_data.R to have the data and IIDkey
clean.it <- function() {
basic.packages <- c("package:stats","package:graphics",
"package:grDevices","package:utils",
"package:datasets","package:methods",
"package:base")
package.list <- dplyr::setdiff( search()[ifelse(unlist(gregexpr("package:",search()))==1,TRUE,FALSE)] , basic.packages)
if (length(package.list)>0) for(package in package.list) detach(package, character.only=TRUE)
ll <- dplyr::setdiff( ls(envir = globalenv()), ## objects to exclude from cleaning
c("clean.it"))
rm(list = ll, envir = globalenv()); gc() # or sessionInfo()
#devtools::install_github('bbc/bbplot'), requires bioconductor as dependency
if(!require(pacman))install.packages("pacman")
pacman::p_load(tidyverse,purrr, magrittr,readxl,lubridate, here, qwraps2,
tsibble, chron, knitr, kableExtra, magick, webshot)
}
clean.it()
setwd(here("inputs")); data <- readRDS("NIRS_interpolated.rds")
# this function will find the number of unintrerrupted seconds with alarms
seqle <- function(x,incr=1) {
if(!is.numeric(x)) x <- as.numeric(x)
n <- length(x)
y <- x[-1L] != x[-n] + incr
i <- c(which(y|is.na(y)),n)
list(lengths = diff(c(0L,i)),
values = x[head(c(0L,i)+1L,-1L)])
}
sumar <- data %>%
group_by(Patient) %>%
summarise(
Group = first(Group) %>% as.factor(),
"Age (months)" = first(Months),
Weight = first(Weight),
Gender = first(Gender) %>% as.factor(),
#date = first(as.Date(datetime)),
#start = strftime(first(datetime), format="%H:%M:%S"),
#stop = strftime(last(datetime), format="%H:%M:%S"),
'Duration (minutes)' = as.integer(floor(max(minutes))),
#nr_obs = n(),
#'baseline' = first(Rbaseline + Lbaseline)/2,
#'min. abs. ' = min(NIRS_min, na.rm = TRUE),
#'%dev. min.' = min(PROC_min, na.rm = TRUE),
'% dev. Q25' = (quantile(L_proc, 0.25, na.rm = TRUE) + quantile(R_proc, 0.25, na.rm = TRUE))/2,
'% dev. median' = (median(L_proc, na.rm = TRUE) + median(R_proc, na.rm = TRUE))/2,
'% dev. Q75' = (quantile(L_proc, 0.75, na.rm = TRUE) + quantile(R_proc, 0.75, na.rm = TRUE))/2,
'max. consec.(s)' = max(seqle(interp_ID[alarm_min ==1], incr = 1)[[1]]), # here comes the seqle function
'Alarms (seconds)'= length(which(alarm_min==1)),
'% time under baseline' = floor(100*length(which(under_BL==1))/n())
#penalization1 = sum(PROC_min[PROC_min < 0] ^ 2, na.rm = TRUE)/length(which(PROC_min < 0)),
#penalization2 = sum(PROC_min[PROC_min < 0] ^ 2, na.rm = TRUE)/n(),
#penalization3 = sum(PROC_min[PROC_min < 0] ^ 2, na.rm = TRUE)
#test = mean(PROC_min[PROC_min<0], na.rm = TRUE),
#neg_sum = sum(PROC_min[PROC_min < 0], na.rm = TRUE)
#'neg.surface/% time < BL' = 'negative surface'/'% time < BL'
) %>%
dplyr::ungroup() %>%
purrr::modify_if(~is.numeric(.), ~round(., 3)) %>%
arrange(Group, `Age (months)`)
rm(seqle)
# swr = function(string, nwrap=25) { # the nwrap defines the width of the text wrapping
# paste(strwrap(string, width=nwrap), collapse="\n")
# };swr = Vectorize(swr)
## Supplementary Table
library(kableExtra)
sumar %>%
mutate('% time under baseline' = paste0(`% time under baseline`, " %")) %>%
purrr::modify_if(~is.numeric(.), ~round(., 1)) %>%
dplyr::select(-Group) %>%
kable(align = "c", "html", booktabs = T,
caption = "Supplementary descriptive Table of each Patient") %>%
kable_styling(bootstrap_options = c("striped","condensed",
"scale_down",font_size = 5),
full_width = F, position = "center") %>%
#collapse_rows(columns = 2, valign = "middle") %>%
pack_rows("Anaesthesia with surgery", 1, 29, label_row_css = "background-color: #666; color: #fff;") %>%
pack_rows("Anaesthesia without surgery", 30, 50, label_row_css = "background-color: #666; color: #fff;") %>%
pack_rows("Sedation", 51, 81, label_row_css = "background-color: #666; color: #fff;") %>%
row_spec(c(2,31,53), bold = T) %>%
footnote(general = "% dev. Q25/median/Q75 = 1st, 2nd, and 3rd quartile of procentual deviation from baseline.
max. consec. (s) = the duration in seconds of the longest desaturation under -20%.
Alarms (seconds) = how many seconds with a NIRS value below -20% from baseline.")
# Figure 1. NIRS monitoring in patients with events# patients with events (4,24,89) - see summary
g <- data %>%
filter(Patient %in% c(4,24,89)) %>%
filter(PROC_avg < 25) %>%
filter(PROC_min > -35) %>%
left_join(sumar) %>%
transmute(
Patient = as.factor(paste(
Group,
#paste0("Patient# ",Patient),
#paste0(Months," months old ",Gender),
#paste0(`% time under baseline`,"% time under baseline "),
#paste0(`Alarms (seconds)`," seconds under - 20% "),
sep="\n")),
time = as.double(minutes),
'lowest deviation (mininum of left and right NIRS sensors)' = PROC_min/100,
'average deviation (mean of left and right NIRS sensors)' = PROC_avg/100) %>%
pivot_longer(-c(Patient,time))
g %>% ggplot(aes(x = time,y = value,colour = name, group = name)) +
geom_point(size = 0.5, alpha = 0.5, na.rm = TRUE) +
scale_y_continuous(breaks = seq(-0.3,0.2, by = 0.1),labels = scales::percent) +
scale_x_continuous(breaks=seq(0, 240, by = 60)) +
theme_bw(base_size = 12) +
geom_hline(aes(yintercept = 0)) +
geom_hline(aes(yintercept = -0.2)) +
theme(
plot.caption = element_text(size = 12, hjust = 0),
legend.position="bottom",
legend.title = element_blank()
) +
guides(color = guide_legend(override.aes = list(size=5))) +
facet_wrap(.~Patient, scales = "free_x") +
labs(
#title = "Figure 1. NIRS monitoring in children with events",
y = "procentual deviation from baseline (0%)",
x = "time in minutes")
rm(g)
### THE HISTOGRAM
set.seed(111)
calibr <- sumar %>% count(Group) %>% mutate(m = 100/n) %>% pull(m)
boot_data <- rbind(
filter(data, Group == "Anesthesia with surgery") %>%
group_by(Patient) %>% sample_n(size = 3333 * calibr[[1]], replace = TRUE),
filter(data, Group == "Anesthesia without surgery") %>%
group_by(Patient) %>% sample_n(size = 3333 * calibr[[2]], replace = TRUE),
filter(data, Group == "Sedation") %>%
group_by(Patient) %>%sample_n(size = 3333 * calibr[[3]], replace = TRUE))
rm(calibr)
boot_data %>% group_by(Group) %>% count()
library(scales)
boot_data %>%
filter(between(PROC_min, -30, 0)) %>% mutate(PROC_min = PROC_min/100) %>%
#mutate(Group = ifelse(Group %in% c("Anesthesia with surgery", "Anesthesia without surgery"), "Anaesthesia", "Sedation")) %>%
ggplot(aes(x = PROC_min, Group = Group)) +
geom_histogram(bins = 30) +
scale_x_continuous(labels = percent) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides="l") +
geom_vline(aes(xintercept = -0.2), colour="red", linetype = "longdash")+
facet_wrap(~Group) +
theme_bw(base_size = 16) +
labs(#subtitle = "Figure 2. Histogram of the negative procentual NIRS desaturations",
x = "negative procentual deviation from baseline",
y = "number of observations"
#caption = "
#Left from the vertical red lines are samples below -20% from baseline, the threshold for alarms.
#All patients are equally represented by bootstrapping.
#The vertical axis is logarithmic to highlight the outliers."
)+
theme(plot.caption = element_text(size = 12, hjust = 0))
# library("cgam") # constrained general additive models
# autofit_anae_FIO2_HF <- ShapeSelect(NIRS_proc_min ~ shapes(FiO2) + shapes(HF), # computer generated
# data = sample_anae %>% filter(HF < 150), genetic = TRUE)
### connecting NIRS data from the selected patients with the manual table
library(feather)
feather::read_feather(path = here("/inputs/manualtable.feather")) %>%
filter(Patient %in% c(4,24,89)) %>% filter(NIRS_proc_min < 0) %>%
openxlsx::write.xlsx(file = here("outputs/negative_deviations_from_pat_04_24_89.xlsx"))