generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
venn_diagram.R
208 lines (156 loc) · 7.55 KB
/
venn_diagram.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
## =============================================================================
## Purpose: Create venn diagrams
##
## Author: Yinghui Wei
##
## Reviewer: Renin Toms, Venexia Walker
##
## Date: 6 December 2021; updated 10 January 2022; updated 27 January 2022
##
## Data: Post covid vaccinated project study population
##
## Content: to create a Venn diagram for each outcome outlining overlap in
## reporting from different data sources
## Output: Venn diagrams in SVG files, venn_diagram_number_check.csv
## =============================================================================
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
# use for interactive testing
population <- ""
} else {
population <- args[[1]]
}
library(readr)
library(dplyr)
library(stringr)
library(tidyverse)
fs::dir_create(here::here("output", "not-for-review"))
fs::dir_create(here::here("output", "review", "venn-diagrams"))
venn_output <- function(population, group){
# Identify active outcomes ---------------------------------------------------
active_analyses <- readr::read_rds("lib/active_analyses.rds")
# added extra statement to include only those with venn == TRUE - because some diabetes outcomes only use one data source and so venn is not applicable
outcomes <- active_analyses[active_analyses$active==TRUE & active_analyses$venn==TRUE,]$outcome_variable
# Load data ------------------------------------------------------------------
input <- readr::read_rds(paste0("output/venn",population,".rds"))
input_stage1 <- readr::read_rds(paste0("output/input", population,"_stage1_",group,".rds"))
input <- input[input$patient_id %in% input_stage1$patient_id,]
# Create empty table ---------------------------------------------------------
df <- data.frame(outcome = character(),
snomed = numeric(),
hes = numeric(),
death = numeric(),
snomed_hes = numeric(),
snomed_death = numeric(),
hes_death = numeric(),
snomed_hes_death = numeric(),
total = numeric(),
stringsAsFactors = FALSE)
# Populate table and make Venn for each outcome ------------------------------
for (outcome in outcomes) {
# Restrict data to that relevant to the given outcome ----------------------
tmp <- input[!is.na(input[,outcome]),c("patient_id",colnames(input)[grepl(outcome,colnames(input))])]
# Identify and add missing columns -----------------------------------------
complete <- data.frame(patient_id = tmp$patient_id,
snomed = as.Date(NA),
hes = as.Date(NA),
death = as.Date(NA))
colnames(complete) <- c("patient_id",paste0("tmp_",outcome,c("_snomed","_hes","_death")))
complete[,setdiff(colnames(tmp),"patient_id")] <- NULL
notused <- NULL
if (ncol(complete)>1) {
tmp <- merge(tmp, complete, by = c("patient_id"))
notused <- gsub(paste0("tmp_",outcome,"_"),"",setdiff(colnames(complete),"patient_id"))
}
# Calculate the number contributing to each source combo -------------------
tmp$snomed <- !is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
is.na(tmp[,paste0("tmp_",outcome,"_death")])
tmp$hes <- is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
is.na(tmp[,paste0("tmp_",outcome,"_death")])
tmp$death <- is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_death")])
tmp$snomed_hes <- !is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
is.na(tmp[,paste0("tmp_",outcome,"_death")])
tmp$hes_death <- is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_death")])
tmp$snomed_death <- !is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_death")])
tmp$snomed_hes_death <- !is.na(tmp[,paste0("tmp_",outcome,"_snomed")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_hes")]) &
!is.na(tmp[,paste0("tmp_",outcome,"_death")])
df[nrow(df)+1,] <- c(outcome,
snomed = sum(tmp$snomed),
hes = sum(tmp$hes),
death = sum(tmp$death),
snomed_hes = sum(tmp$snomed_hes),
snomed_death = sum(tmp$snomed_death),
hes_death = sum(tmp$hes_death),
snomed_hes_death = sum(tmp$snomed_hes_death),
total = nrow(tmp))
# Remove sources not in study definition from Venn plots -------------------
consider <- c("snomed","hes","death","snomed_hes","snomed_death","hes_death","snomed_hes_death")
if (!is.null(notused)) {
for (i in notused) {
consider <- consider[!grepl(i,consider)]
}
}
# Proceed to create Venn diagram if all source combos exceed 5 -------------
# if (min(as.numeric(df[df$outcome==outcome,consider]))>5) {
# Calculate contents of each Venn cell for plotting ----------------------
index1 <- integer(0)
index2 <- integer(0)
index3 <- integer(0)
if ("snomed" %in% consider) {
index1 <- which(!is.na(tmp[,paste0("tmp_",outcome,"_snomed")]))
}
if ("hes" %in% consider) {
index2 <- which(!is.na(tmp[,paste0("tmp_",outcome,"_hes")]))
}
if ("death" %in% consider) {
index3 <- which(!is.na(tmp[,paste0("tmp_",outcome,"_death")]))
}
index <- list(index1, index2, index3)
names(index) <- c("SNOMED", "Hospital Episodes", "Deaths")
index <- Filter(length, index)
# Fix colours --------------------------------------------------------------
mycol <- c(ifelse("SNOMED" %in% names(index),"thistle",""),
ifelse("Hospital Episodes" %in% names(index),"lightcyan",""),
ifelse("Deaths" %in% names(index),"lemonchiffon",""))
mycol <- mycol[mycol!=""]
# Make Venn diagram --------------------------------------------------------
svglite::svglite(file = paste0("output/review/venn-diagrams/venn_diagram",population,"_",gsub("out_date_","",outcome),".svg"))
g <- ggvenn::ggvenn(
index,
fill_color = mycol,
stroke_color = "white",
text_size = 5,
set_name_size = 5,
fill_alpha = 0.9
) + ggplot2::ggtitle(active_analyses[active_analyses$outcome_variable==outcome,]$outcome) +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5, size = 15, face = "bold"))
print(g)
dev.off()
}
# }
# Save summary file ----------------------------------------------------------
write.csv(df, file = paste0("output/review/venn-diagrams/venn_diagram_number_check.csv"), row.names = F)
}
# Run function using specified commandArgs -------------------------------------
# if(population == "both"){
# venn_output("electively_unvaccinated")
# venn_output("vaccinated")
# } else{
# Run function using outcome group
# }
active_analyses <- read_rds("lib/active_analyses.rds")
active_analyses <- active_analyses %>% filter(active==TRUE)
group <- unique(active_analyses$outcome_group)
for(i in group){
venn_output(population, i)
}