forked from insightsengineering/tern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
odds_ratio.R
326 lines (302 loc) · 11 KB
/
odds_ratio.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#' Odds Ratio Estimation
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Compares bivariate responses between two groups in terms of odds ratios
#' along with a confidence interval.
#'
#' @inheritParams split_cols_by_groups
#' @inheritParams argument_convention
#' @param .stats (`character`)\cr statistics to select for the table. Run `get_stats("estimate_odds_ratio")`
#' to see available statistics for this function.
#'
#' @details This function uses either logistic regression for unstratified
#' analyses, or conditional logistic regression for stratified analyses.
#' The Wald confidence interval with the specified confidence level is
#' calculated.
#'
#' @note For stratified analyses, there is currently no implementation for conditional
#' likelihood confidence intervals, therefore the likelihood confidence interval is not
#' yet available as an option. Besides, when `rsp` contains only responders or non-responders,
#' then the result values will be `NA`, because no odds ratio estimation is possible.
#'
#' @seealso Relevant helper function [h_odds_ratio()].
#'
#' @name odds_ratio
#' @order 1
NULL
#' @describeIn odds_ratio Statistics function which estimates the odds ratio
#' between a treatment and a control. A `variables` list with `arm` and `strata`
#' variable names must be passed if a stratified analysis is required.
#'
#' @return
#' * `s_odds_ratio()` returns a named list with the statistics `or_ci`
#' (containing `est`, `lcl`, and `ucl`) and `n_tot`.
#'
#' @examples
#' # Unstratified analysis.
#' s_odds_ratio(
#' df = subset(dta, grp == "A"),
#' .var = "rsp",
#' .ref_group = subset(dta, grp == "B"),
#' .in_ref_col = FALSE,
#' .df_row = dta
#' )
#'
#' # Stratified analysis.
#' s_odds_ratio(
#' df = subset(dta, grp == "A"),
#' .var = "rsp",
#' .ref_group = subset(dta, grp == "B"),
#' .in_ref_col = FALSE,
#' .df_row = dta,
#' variables = list(arm = "grp", strata = "strata")
#' )
#'
#' @export
s_odds_ratio <- function(df,
.var,
.ref_group,
.in_ref_col,
.df_row,
variables = list(arm = NULL, strata = NULL),
conf_level = 0.95,
groups_list = NULL) {
y <- list(or_ci = "", n_tot = "")
if (!.in_ref_col) {
assert_proportion_value(conf_level)
assert_df_with_variables(df, list(rsp = .var))
assert_df_with_variables(.ref_group, list(rsp = .var))
if (is.null(variables$strata)) {
data <- data.frame(
rsp = c(.ref_group[[.var]], df[[.var]]),
grp = factor(
rep(c("ref", "Not-ref"), c(nrow(.ref_group), nrow(df))),
levels = c("ref", "Not-ref")
)
)
y <- or_glm(data, conf_level = conf_level)
} else {
assert_df_with_variables(.df_row, c(list(rsp = .var), variables))
# The group variable prepared for clogit must be synchronised with combination groups definition.
if (is.null(groups_list)) {
ref_grp <- as.character(unique(.ref_group[[variables$arm]]))
trt_grp <- as.character(unique(df[[variables$arm]]))
grp <- stats::relevel(factor(.df_row[[variables$arm]]), ref = ref_grp)
} else {
# If more than one level in reference col.
reference <- as.character(unique(.ref_group[[variables$arm]]))
grp_ref_flag <- vapply(
X = groups_list,
FUN.VALUE = TRUE,
FUN = function(x) all(reference %in% x)
)
ref_grp <- names(groups_list)[grp_ref_flag]
# If more than one level in treatment col.
treatment <- as.character(unique(df[[variables$arm]]))
grp_trt_flag <- vapply(
X = groups_list,
FUN.VALUE = TRUE,
FUN = function(x) all(treatment %in% x)
)
trt_grp <- names(groups_list)[grp_trt_flag]
grp <- combine_levels(.df_row[[variables$arm]], levels = reference, new_level = ref_grp)
grp <- combine_levels(grp, levels = treatment, new_level = trt_grp)
}
# The reference level in `grp` must be the same as in the `rtables` column split.
data <- data.frame(
rsp = .df_row[[.var]],
grp = grp,
strata = interaction(.df_row[variables$strata])
)
y_all <- or_clogit(data, conf_level = conf_level)
checkmate::assert_string(trt_grp)
checkmate::assert_subset(trt_grp, names(y_all$or_ci))
y$or_ci <- y_all$or_ci[[trt_grp]]
y$n_tot <- y_all$n_tot
}
}
y$or_ci <- formatters::with_label(
x = y$or_ci,
label = paste0("Odds Ratio (", 100 * conf_level, "% CI)")
)
y$n_tot <- formatters::with_label(
x = y$n_tot,
label = "Total n"
)
y
}
#' @describeIn odds_ratio Formatted analysis function which is used as `afun` in `estimate_odds_ratio()`.
#'
#' @return
#' * `a_odds_ratio()` returns the corresponding list with formatted [rtables::CellValue()].
#'
#' @examples
#' a_odds_ratio(
#' df = subset(dta, grp == "A"),
#' .var = "rsp",
#' .ref_group = subset(dta, grp == "B"),
#' .in_ref_col = FALSE,
#' .df_row = dta
#' )
#'
#' @export
a_odds_ratio <- make_afun(
s_odds_ratio,
.formats = c(or_ci = "xx.xx (xx.xx - xx.xx)"),
.indent_mods = c(or_ci = 1L)
)
#' @describeIn odds_ratio Layout-creating function which can take statistics function arguments
#' and additional format arguments. This function is a wrapper for [rtables::analyze()].
#'
#' @param ... arguments passed to `s_odds_ratio()`.
#'
#' @return
#' * `estimate_odds_ratio()` returns a layout object suitable for passing to further layouting functions,
#' or to [rtables::build_table()]. Adding this function to an `rtable` layout will add formatted rows containing
#' the statistics from `s_odds_ratio()` to the table layout.
#'
#' @examples
#' set.seed(12)
#' dta <- data.frame(
#' rsp = sample(c(TRUE, FALSE), 100, TRUE),
#' grp = factor(rep(c("A", "B"), each = 50), levels = c("A", "B")),
#' strata = factor(sample(c("C", "D"), 100, TRUE))
#' )
#'
#' l <- basic_table() %>%
#' split_cols_by(var = "grp", ref_group = "B") %>%
#' estimate_odds_ratio(vars = "rsp")
#'
#' build_table(l, df = dta)
#'
#' @export
#' @order 2
estimate_odds_ratio <- function(lyt,
vars,
variables = list(arm = NULL, strata = NULL),
conf_level = 0.95,
groups_list = NULL,
na_str = default_na_str(),
nested = TRUE,
...,
show_labels = "hidden",
table_names = vars,
.stats = "or_ci",
.formats = NULL,
.labels = NULL,
.indent_mods = NULL) {
extra_args <- list(variables = variables, conf_level = conf_level, groups_list = groups_list, ...)
afun <- make_afun(
a_odds_ratio,
.stats = .stats,
.formats = .formats,
.labels = .labels,
.indent_mods = .indent_mods
)
analyze(
lyt,
vars,
afun = afun,
na_str = na_str,
nested = nested,
extra_args = extra_args,
show_labels = show_labels,
table_names = table_names
)
}
#' Helper Functions for Odds Ratio Estimation
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Functions to calculate odds ratios in [estimate_odds_ratio()].
#'
#' @inheritParams argument_convention
#' @param data (`data.frame`)\cr data frame containing at least the variables `rsp` and `grp`, and optionally
#' `strata` for [or_clogit()].
#'
#' @return A named `list` of elements `or_ci` and `n_tot`.
#'
#' @seealso [odds_ratio]
#'
#' @name h_odds_ratio
NULL
#' @describeIn h_odds_ratio Estimates the odds ratio based on [stats::glm()]. Note that there must be
#' exactly 2 groups in `data` as specified by the `grp` variable.
#'
#' @examples
#' # Data with 2 groups.
#' data <- data.frame(
#' rsp = as.logical(c(1, 1, 0, 1, 0, 0, 1, 1)),
#' grp = letters[c(1, 1, 1, 2, 2, 2, 1, 2)],
#' strata = letters[c(1, 2, 1, 2, 2, 2, 1, 2)],
#' stringsAsFactors = TRUE
#' )
#'
#' # Odds ratio based on glm.
#' or_glm(data, conf_level = 0.95)
#'
#' @export
or_glm <- function(data, conf_level) {
checkmate::assert_logical(data$rsp)
assert_proportion_value(conf_level)
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp"))
checkmate::assert_multi_class(data$grp, classes = c("factor", "character"))
data$grp <- as_factor_keep_attributes(data$grp)
assert_df_with_factors(data, list(val = "grp"), min.levels = 2, max.levels = 2)
formula <- stats::as.formula("rsp ~ grp")
model_fit <- stats::glm(
formula = formula, data = data,
family = stats::binomial(link = "logit")
)
# Note that here we need to discard the intercept.
or <- exp(stats::coef(model_fit)[-1])
or_ci <- exp(
stats::confint.default(model_fit, level = conf_level)[-1, , drop = FALSE]
)
values <- stats::setNames(c(or, or_ci), c("est", "lcl", "ucl"))
n_tot <- stats::setNames(nrow(model_fit$model), "n_tot")
list(or_ci = values, n_tot = n_tot)
}
#' @describeIn h_odds_ratio estimates the odds ratio based on [survival::clogit()]. This is done for
#' the whole data set including all groups, since the results are not the same as when doing
#' pairwise comparisons between the groups.
#'
#' @examples
#' # Data with 3 groups.
#' data <- data.frame(
#' rsp = as.logical(c(1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0)),
#' grp = letters[c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3)],
#' strata = LETTERS[c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)],
#' stringsAsFactors = TRUE
#' )
#'
#' # Odds ratio based on stratified estimation by conditional logistic regression.
#' or_clogit(data, conf_level = 0.95)
#'
#' @export
or_clogit <- function(data, conf_level) {
checkmate::assert_logical(data$rsp)
assert_proportion_value(conf_level)
assert_df_with_variables(data, list(rsp = "rsp", grp = "grp", strata = "strata"))
checkmate::assert_multi_class(data$grp, classes = c("factor", "character"))
checkmate::assert_multi_class(data$strata, classes = c("factor", "character"))
data$grp <- as_factor_keep_attributes(data$grp)
data$strata <- as_factor_keep_attributes(data$strata)
# Deviation from convention: `survival::strata` must be simply `strata`.
formula <- stats::as.formula("rsp ~ grp + strata(strata)")
model_fit <- clogit_with_tryCatch(formula = formula, data = data)
# Create a list with one set of OR estimates and CI per coefficient, i.e.
# comparison of one group vs. the reference group.
coef_est <- stats::coef(model_fit)
ci_est <- stats::confint(model_fit, level = conf_level)
or_ci <- list()
for (coef_name in names(coef_est)) {
grp_name <- gsub("^grp", "", x = coef_name)
or_ci[[grp_name]] <- stats::setNames(
object = exp(c(coef_est[coef_name], ci_est[coef_name, , drop = TRUE])),
nm = c("est", "lcl", "ucl")
)
}
list(or_ci = or_ci, n_tot = c(n_tot = model_fit$n))
}