generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine-weights.R
180 lines (146 loc) · 4.5 KB
/
combine-weights.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
# # # # # # # # # # # # # # # # # # # # #
# Purpose: combine weights from all adjustment strategies
# imports weighting data and creates:
# - dataset containing all weights for each strategy
# - dataset containing effective sample size for each strategy
# # # # # # # # # # # # # # # # # # # # #
## Import libraries ----
library('tidyverse')
library('here')
library('glue')
library("arrow")
## Import custom user functions from lib
source(here("analysis", "0-lib", "utility.R"))
## Import design elements
source(here("analysis", "0-lib", "design.R"))
# import command-line arguments ----
args <- commandArgs(trailingOnly=TRUE)
if(length(args)==0){
# use for interactive testing
removeobjects <- FALSE
cohort <- "age75plus"
} else {
removeobjects <- TRUE
cohort <- args[[1]]
}
# create output directories ----
output_dir <- here_glue("output", "3-adjust", cohort, "combine")
fs::dir_create(output_dir)
## import unadjusted cohort data ----
data_cohort <- read_feather(here("output", "2-prepare", cohort, "data_cohort.arrow"))
## create dataset of metaparameters to import
cohort0 <- cohort
metaparams_cohort_method_spec <-
metaparams |>
select(cohort, method, spec) |>
unique() |>
filter(
cohort == cohort0,
)
## weights ----
## create dataset that contains only patient IDs and the weights from all different adjustment strategies
data_weights <-
metaparams_cohort_method_spec |>
mutate(
data = pmap(
list(cohort, method, spec),
function(cohort, method, spec) {
dat <-
here("output", "3-adjust", cohort, glue("{method}-{spec}"), "data_adjusted.arrow") |>
read_feather() |>
select(patient_id, treatment, weight)
dat
}
)
) |>
unnest(data)
data_weights_wider <-
data_weights |>
pivot_wider(
id_cols = c(patient_id, treatment),
names_from = c(cohort, method, spec),
names_prefix = "wt_",
names_sep = "_",
values_from = weight
) |>
mutate(
wt_unadjusted = 1,
.after = "treatment"
)
data_all <-
left_join(
data_cohort,
data_weights_wider,
by = c("patient_id", "treatment")
)
write_feather(data_all, fs::path(output_dir, "data_weights.arrow"))
## ESS ----
## create dataset of effective sample sizes for each adjustment strategy
table_ess <-
data_weights |>
group_by(treatment, cohort, method, spec) |>
summarise(
ess = (sum(weight)^2) / (sum(weight^2))
) |>
pivot_wider(
id_cols = c(cohort, method, spec),
names_from = treatment,
names_prefix = "ess_",
values_from = ess
)
write_feather(table_ess, fs::path(output_dir, "table_ess.arrow"))
## event counts ----
## create dataset that reports event counts for each outcome of interest
data_event_counts <-
bind_rows(
metaparams |>
distinct(cohort, subgroup, outcome, .keep_all=TRUE) |>
mutate(
method="unadjusted",
spec=""
),
metaparams
) |>
group_by(cohort, method, spec, subgroup, outcome) |>
mutate(
data = pmap(
list(cohort, method, spec, subgroup, outcome),
function(cohort, method, spec, subgroup, outcome) {
data_all %>%
mutate(
subgroup_level = .[[subgroup]],
wt = ifelse(
method!="unadjusted",
.[[paste0("wt_", cohort, "_", method, "_", spec)]],
1
),
treatment_date = vax_date-1L,
event_date = as.Date(.[[paste0(outcome, "_date")]]),
# person-time is up to and including censor date
censor_date = pmin(
dereg_date,
death_date,
study_dates$followupend_date,
treatment_date + maxfup,
na.rm=TRUE
),
noncompetingcensor_date = pmin(
dereg_date,
study_dates$followupend_date,
treatment_date + maxfup,
na.rm=TRUE
),
event_time = tte(treatment_date, event_date, censor_date, na.censor=FALSE),
event_indicator = censor_indicator(event_date, censor_date),
) |>
group_by(subgroup_level, treatment) |>
summarise(
n = roundmid_any(sum(wt), sdc.limit),
persontime = sum(wt*as.numeric(censor_date - (vax_date - 1))),
count = sum(wt*event_indicator)
)
}
)
) |>
unnest(data)
write_feather(data_event_counts, fs::path(output_dir, "table_event_counts.arrow"))