generated from opensafely/research-template
/
FlowChart.R
192 lines (159 loc) · 12.1 KB
/
FlowChart.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
188
189
190
191
192
library(data.table)
ncores <- parallel::detectCores(logical = T) - 1
data.table::setDTthreads(ncores)
procedures <- c('Abdominal','Cardiac','Obstetrics','Orthopaedic','Thoracic', 'Vascular')
source(here::here("analysis","Utils.R"))
dt.tv <- data.table::setDT(feather::read_feather(here::here("output","cohort_long.feather")))
data.table::setkey(dt.tv,patient_id,tstart,tstop)
##########################
n.ops <- rnd(dt.tv[start ==0 & any.op == T & is.finite(admit.date),lapply(.SD,function(x) sum(x == T)), .SDcols = c(procedures)])
n.ops.VTE <- rnd(dt.tv[(postcovid.VTE.cohort) & start ==0 & is.finite(admit.date) & any.op.VTE == T,lapply(.SD,function(x) sum(x == T)), .SDcols = c(procedures)])
n.ops.COVID <- rnd(dt.tv[(postop.covid.cohort) & start ==0 & is.finite(admit.date) & any.op.COVID == T,lapply(.SD,function(x) sum(x == T)), .SDcols = c(procedures)])
n.pats <- rnd(length(unique(dt.tv[,patient_id])))
n.pats.study <- rnd(length(unique(dt.tv[start ==0 & is.finite(admit.date) & any.op == T & admit.date <= end.fu,patient_id])))
n.pats.late <- rnd(length(unique(dt.tv[(admit.date > gp.end | !is.finite(gp.end)) & any.op == T,patient_id])))
n.covid.90 <- rnd(dt.tv[,max(event == 1 & start >=0 & end <=90 & any.op.COVID == T, na.rm = T) , keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.covid.90.censored <- rnd(dt.tv[,max(event == 1 & start >=0 & end <=90 & (postop.covid.cohort) & any.op.COVID == T, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.covid.30.censored <- rnd(dt.tv[,max(event == 1 & start >=0 & end <=30 & (postop.covid.cohort) & any.op.COVID == T, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.covid.7.censored <- rnd(dt.tv[,max(event == 1 & start >=0 & start >=0 & end <=7 &(postop.covid.cohort) & any.op.COVID == T, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.VTE.90 <- rnd(dt.tv[any.op.VTE == T,max(event.VTE == 1 & start >=0 & end <=90 , na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.VTE.90.censored <- rnd(dt.tv[,max(event.VTE == 1 & start >=0 & end <=90 & (postcovid.VTE.cohort) & any.op.VTE == T, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.VTE.30.censored <- rnd(dt.tv[,max(event.VTE == 1 & start >=0 & end <=30 & (postcovid.VTE.cohort) & any.op.VTE == T, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.VTE.7.censored <- rnd(dt.tv[,max(event.VTE == 1 & start >=0 & end <=7 & (postcovid.VTE.cohort) & any.op.VTE == T, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.surv.90 <- rnd(dt.tv[start >= 0 & any.op == T,max(died == 1 & end <=90, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.surv.90.censored <- rnd(dt.tv[start >= 0 & any.op == T,max(died == 1 & end <=90, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.surv.30.censored <- rnd(dt.tv[start >= 0 & any.op == T,max(died == 1 & end <=30, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
n.surv.7.censored <- rnd(dt.tv[start >= 0 & any.op == T,max(died == 1 & end <=7, na.rm = T), keyby = .(patient_id, end.fu)][,tail(.SD), keyby = .(patient_id, end.fu)][,sum(V1==1)])
start.date <- dt.tv[ start ==0 & is.finite(admit.date) & any.op == T, min(as.Date(as.integer(admit.date), origin = as.Date('1970-01-01')))]
last.date <- dt.tv[ start ==0 & is.finite(admit.date) & any.op == T, max(as.Date(as.integer(admit.date), origin = as.Date('1970-01-01')))]
p <- ggplot2::ggplot(data = data.frame(x = 1:100, y = 1:100), ggplot2::aes(x , y)) +
ggplot2::scale_x_continuous(minor_breaks = seq(10, 100, 10)) +
ggplot2::scale_y_continuous(minor_breaks = seq(10, 100, 10)) +
ggplot2::theme_void()
p <- p + ggplot2::geom_rect(xmin = 32, xmax=67, ymin=94, ymax=100, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=97,label= paste0(n.pats,' Patients with coded procedures \n between ',start.date,' & ', last.date), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 32, xmax=68, ymin=81.5, ymax=90, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=86,label= paste0(n.pats.study,' Patients with procedures within \n registration period for a \n primary care practice contributing \n to SystmOne'), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 70, xmax=101, ymin=86, ymax=98, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 85, y=92,label= paste0(n.pats - n.pats.study,' Patients excluded: \n Procedures when not registered \n to a primary care practice \n contributing to SystmOne'), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 28, xmax=71, ymin=62, ymax=79, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=71,label= paste0(sum(n.ops)," Procedures in categories \n(not mutually exclusive):\n",paste(paste0(names(n.ops),':'),n.ops, collapse = ' procedures \n '),' procedures'), size=2.5)
p <- p +
ggplot2::geom_segment(
x=50, xend=50, y=94, yend=90,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
ggplot2::geom_segment(
x=50, xend=69.7, y=92, yend=92,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
ggplot2::geom_segment(
x=50, xend=50, y=81.5, yend=79,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
ggplot2::geom_segment(
#middle arrow
x=50, xend=50, y=62, yend=55,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Left arrow
ggplot2::geom_segment(
x=17, xend=17, y=58.5, yend=55,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Right arrow
ggplot2::geom_segment(
x=83, xend=83, y=58.5, yend=55,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Horizontal line
ggplot2::geom_segment(
x=17, xend=83, y=58.5, yend=58.5,
size=0.15, linejoin = "mitre", lineend = "butt")
##########################################
# COVID counts
p <- p + ggplot2::geom_rect(xmin = 0, xmax=33, ymin=49, ymax=55, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 17, y=52.2,label= paste0(sum(n.ops.COVID)," Procedures \n with no COVID-19 positivity \n in prior 7 days"), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 0, xmax=33, ymin=41, ymax=47, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 17, y=44.2,label= paste0("COVID-19 positivity within 90 days: \n ",n.covid.90," events\n",round(100 * n.covid.90/sum(n.ops.COVID), digits = 2),"%" ), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 0, xmax=33, ymin=28, ymax=39, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 17, y=33.2,label= paste0("COVID-19 positivity within 90 days \n censored for \n non COVID-19 readmissions: \n ",n.covid.90.censored," events\n",round(100 * n.covid.90.censored/sum(n.ops.COVID), digits = 2),"%" ), size=2.5)
p <- p + ggplot2::geom_rect(xmin =0, xmax=33, ymin=13, ymax=26, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 17, y=19.2,label= paste0("COVID-19 positivity within 30 days \n censored for \n non COVID-19 readmissions: \n ",n.covid.30.censored," events\n",round(100 * n.covid.30.censored/sum(n.ops.COVID), digits = 2),"%" ), size=2.5)
#######################################
# VTE counts
p <- p + ggplot2::geom_rect(xmin = 34, xmax=66, ymin=49, ymax=55, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=52.2,label= paste0(sum(n.ops.VTE)," Procedures \n for post COVID VTE \n analysis"), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 34, xmax=66, ymin=41, ymax=47, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=44.2,label= paste0("VTE within 90 days: \n ",n.VTE.90," events\n",round(100 * n.VTE.90/sum(n.ops.VTE), digits = 2),"%" ), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 34, xmax=66, ymin=28, ymax=39, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=33.2,label= paste0("VTE within 90 days \n censored for \n readmissions: \n ",n.VTE.90.censored," events\n",round(100 * n.VTE.90.censored/sum(n.ops.VTE), digits = 2),"%" ), size=2.5)
p <- p + ggplot2::geom_rect(xmin =34, xmax=66, ymin=13, ymax=26, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 50, y=19.2,label= paste0("VTE within 30 days \n censored for \n readmissions: \n ",n.VTE.30.censored," events\n",round(100 * n.VTE.30.censored/sum(n.ops.VTE), digits = 2),"%" ), size=2.5)
#########################################
# VTE counts
p <- p + ggplot2::geom_rect(xmin = 67, xmax=100, ymin=49, ymax=55, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 83, y=52.2,label= paste0(sum(n.ops)," Procedures \n for post COVID mortality\n analysis"), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 67, xmax=100, ymin=41, ymax=47, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 83, y=44.2,label= paste0("Deaths within 90 days: \n ",n.surv.90," events\n",round(100 * n.surv.90/sum(n.ops), digits = 2),"%" ), size=2.5)
p <- p + ggplot2::geom_rect(xmin = 67, xmax=100, ymin=28, ymax=39, color='black',
fill='white', size=0.25) +
ggplot2::annotate('text', x= 83, y=33.2,label= paste0("Deaths within 30 days\n ",n.surv.30.censored," events\n",round(100 * n.surv.30.censored/sum(n.ops), digits = 2),"%" ), size=2.5)
########################################
p <- p +
ggplot2::geom_segment(
# Centre top arrow
x=50, xend=50, y=49, yend=47,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Left top arrow
ggplot2::geom_segment(
x=17, xend=17, y=49, yend=47,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Right top arrow
ggplot2::geom_segment(
x=83, xend=83, y=49, yend=47,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
ggplot2::geom_segment(
# Centre middle arrow
x=50, xend=50, y=41, yend=39,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
#Left middle arrow
ggplot2::geom_segment(
x=17, xend=17, y=41, yend=39,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Right middle arrow
ggplot2::geom_segment(
x=83, xend=83, y=41, yend=39,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Centre lower arrow
ggplot2::geom_segment(
x=50, xend=50, y=28, yend=26,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed")) +
# Left lower arrow
ggplot2::geom_segment(
x=17, xend=17, y=28, yend=26,
size=0.15, linejoin = "mitre", lineend = "butt",
arrow = ggplot2::arrow(length = ggplot2::unit(1, "mm"), type= "closed"))
ggplot2::ggsave(p,width = 6, height = 8, units = 'in', dpi = 'retina', filename = "Flowchart.pdf", device='pdf', path = here::here("output"))
save(p, file = here::here("output","flowchart.RData"))