generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable2.R
162 lines (120 loc) · 7.03 KB
/
table2.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
# Load libraries ---------------------------------------------------------------
print('Load libraries')
library(readr)
library(dplyr)
library(magrittr)
# Specify redaction threshold --------------------------------------------------
print('Specify redaction threshold')
threshold <- 6
# Source common functions ------------------------------------------------------
print('Source common functions')
source("analysis/utility.R")
# Specify arguments ------------------------------------------------------------
print('Specify arguments')
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
cohort <- "vax"
focus <- "severity"
} else {
cohort <- args[[1]]
focus <- args[[2]]
}
# Load active analyses ---------------------------------------------------------
print('Load active analyses')
active_analyses <- readr::read_rds("lib/active_analyses.rds")
table2_names <- gsub("out_date_","",unique(active_analyses[active_analyses$cohort=={cohort},]$name))
if (focus=="severity") {
table2_names <- table2_names[grepl("-day0_main-",table2_names) | grepl("-day0_sub_covid_hospitalised",table2_names) | grepl("-day0_sub_covid_nonhospitalised",table2_names)]
}
if (focus=="history") {
table2_names <- table2_names[grepl("-day0_sub_history_",table2_names)]
}
active_analyses <- active_analyses[active_analyses$name %in% table2_names,]
# Make empty table 2 -----------------------------------------------------------
print('Make empty table 2')
table2 <- data.frame(name = character(),
cohort = character(),
exposure = character(),
outcome = character(),
analysis = character(),
unexposed_person_days = numeric(),
unexposed_events = numeric(),
exposed_person_days = numeric(),
exposed_events = numeric(),
total_person_days = numeric(),
total_events = numeric(),
day0_events = numeric(),
total_exposed = numeric(),
sample_size = numeric())
# Record number of events and person days for each active analysis -------------
print('Record number of events and person days for each active analysis')
for (i in 1:nrow(active_analyses)) {
## Load data -----------------------------------------------------------------
print(paste0("Load data for ",active_analyses$name[i]))
df <- read_rds(paste0("output/model_input-",active_analyses$name[i],".rds"))
df <- df[,c("patient_id","index_date","exp_date","out_date","end_date_exposure","end_date_outcome")]
# Remove exposures and outcomes outside follow-up ----------------------------
print("Remove exposures and outcomes outside follow-up")
df <- df %>%
dplyr::mutate(exposure = replace(exp_date, which(exp_date>end_date_exposure | exp_date<index_date), NA),
outcome = replace(out_date, which(out_date>end_date_outcome | out_date<index_date), NA))
## Make exposed subset -------------------------------------------------------
print('Make exposed subset')
exposed <- df[!is.na(df$exp_date),c("patient_id","exp_date","out_date","end_date_outcome")]
exposed <- exposed %>% dplyr::mutate(fup_start = exp_date,
fup_end = min(end_date_outcome, out_date, na.rm = TRUE))
exposed <- exposed[exposed$fup_start<=exposed$fup_end,]
exposed <- exposed %>% dplyr::mutate(person_days = as.numeric((fup_end - fup_start))+1)
## Make unexposed subset -----------------------------------------------------
print('Make unexposed subset')
unexposed <- df[,c("patient_id","index_date","exp_date","out_date","end_date_outcome")]
unexposed <- unexposed %>% dplyr::mutate(fup_start = index_date,
fup_end = min(exp_date-1, end_date_outcome, out_date, na.rm = TRUE),
out_date = replace(out_date, which(out_date>fup_end), NA))
unexposed <- unexposed[unexposed$fup_start<=unexposed$fup_end,]
unexposed <- unexposed %>% dplyr::mutate(person_days = as.numeric((fup_end - fup_start))+1)
## Append to table 2 ---------------------------------------------------------
print('Append to table 2')
table2[nrow(table2)+1,] <- c(name = active_analyses$name[i],
cohort = active_analyses$cohort[i],
exposure = active_analyses$exposure[i],
outcome = active_analyses$outcome[i],
analysis = active_analyses$analysis[i],
unexposed_person_days = sum(unexposed$person_days),
unexposed_events = nrow(unexposed[!is.na(unexposed$out_date),]),
exposed_person_days = sum(exposed$person_days, na.rm = TRUE),
exposed_events = nrow(exposed[!is.na(exposed$out_date),]),
total_person_days = sum(unexposed$person_days) + sum(exposed$person_days),
total_events = nrow(unexposed[!is.na(unexposed$out_date),]) + nrow(exposed[!is.na(exposed$out_date),]),
day0_events = nrow(exposed[exposed$exp_date==exposed$out_date & !is.na(exposed$exp_date) & !is.na(exposed$out_date),]),
total_exposed = nrow(exposed),
sample_size = nrow(df))
}
# Save Table 2 -----------------------------------------------------------------
print('Save Table 2')
write.csv(table2, paste0("output/table2_",focus,"_",cohort,".csv"), row.names = FALSE)
# Perform redaction ------------------------------------------------------------
print('Perform redaction')
table2$sample_size_midpoint6 <- roundmid_any(as.numeric(table2$sample_size), threshold)
table2$day0_events_midpoint6 <- roundmid_any(as.numeric(table2$day0_events), threshold)
table2$total_exposed_midpoint6 <- roundmid_any(as.numeric(table2$total_exposed), threshold)
table2$unexposed_events_midpoint6 <- roundmid_any(as.numeric(table2$unexposed_events), threshold)
table2$exposed_events_midpoint6 <- roundmid_any(as.numeric(table2$exposed_events), threshold)
table2$total_events_midpoint6_derived <- table2$unexposed_events_midpoint6 + table2$exposed_events_midpoint6
table2 <- table2[,c("name",
"cohort",
"exposure",
"outcome",
"analysis",
"unexposed_person_days",
"unexposed_events_midpoint6",
"exposed_person_days",
"exposed_events_midpoint6",
"total_person_days",
"total_events_midpoint6_derived",
"day0_events_midpoint6",
"total_exposed_midpoint6",
"sample_size_midpoint6")]
# Save Table 2 -----------------------------------------------------------------
print('Save rounded Table 2')
write.csv(table2, paste0("output/table2_",focus,"_",cohort,"_midpoint6.csv"), row.names = FALSE)