-
Notifications
You must be signed in to change notification settings - Fork 7
/
model_get_n.R
247 lines (221 loc) · 5.89 KB
/
model_get_n.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
#' Get the number of observations
#'
#' For binomial and multinomial logistic models, will also return
#' the number of events.
#'
#' For Poisson models, will return the number of events and exposure time
#' (defined with [stats::offset()]).
#'
#' For Cox models ([survival::coxph()]), will return the number of events and
#' exposure time.
#'
#' For competing risk regression models ([tidycmprsk::crr()]), `n_event` takes
#' into account only the event of interest defined by `failcode.`
#'
#' See [tidy_add_n()] for more details.
#'
#' The total number of observations (`N_obs`), of events (`N_event`) and of
#' exposure time (`Exposure`) are stored as attributes of the returned tibble.
#'
#' This function does not cover `lavaan` models (`NULL` is returned).
#'
#' @param model a model object
#' @export
#' @family model_helpers
#' @examples
#' lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) %>%
#' model_get_n()
#'
#' mod <- glm(
#' response ~ stage * grade + trt,
#' gtsummary::trial,
#' family = binomial,
#' contrasts = list(stage = contr.sum, grade = contr.treatment(3, 2), trt = "contr.SAS")
#' )
#' mod %>% model_get_n()
#'
#' \dontrun{
#' mod <- glm(
#' Survived ~ Class * Age + Sex,
#' data = Titanic %>% as.data.frame(),
#' weights = Freq, family = binomial
#' )
#' mod %>% model_get_n()
#'
#' d <- dplyr::as_tibble(Titanic) %>%
#' dplyr::group_by(Class, Sex, Age) %>%
#' dplyr::summarise(
#' n_survived = sum(n * (Survived == "Yes")),
#' n_dead = sum(n * (Survived == "No"))
#' )
#' mod <- glm(cbind(n_survived, n_dead) ~ Class * Age + Sex, data = d, family = binomial)
#' mod %>% model_get_n()
#'
#' mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = poisson)
#' mod %>% model_get_n()
#'
#' mod <- glm(
#' response ~ trt * grade + offset(ttdeath),
#' gtsummary::trial,
#' family = poisson
#' )
#' mod %>% model_get_n()
#'
#' dont
#' df <- survival::lung %>% dplyr::mutate(sex = factor(sex))
#' mod <- survival::coxph(survival::Surv(time, status) ~ ph.ecog + age + sex, data = df)
#' mod %>% model_get_n()
#'
#' mod <- lme4::lmer(Reaction ~ Days + (Days | Subject), lme4::sleepstudy)
#' mod %>% model_get_n()
#'
#' mod <- lme4::glmer(response ~ trt * grade + (1 | stage),
#' family = binomial, data = gtsummary::trial
#' )
#' mod %>% model_get_n()
#'
#' mod <- lme4::glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
#' family = binomial, data = lme4::cbpp
#' )
#' mod %>% model_get_n()
#' }
model_get_n <- function(model) {
UseMethod("model_get_n")
}
#' @export
#' @rdname model_get_n
model_get_n.default <- function(model) {
tcm <- model %>% model_compute_terms_contributions()
if (is.null(tcm)) {
return(NULL)
}
w <- model %>% model_get_weights()
n <- dplyr::tibble(
term = colnames(tcm),
n_obs = colSums(tcm * w)
)
attr(n, "N_obs") <- sum(w)
n
}
#' @export
#' @rdname model_get_n
model_get_n.glm <- function(model) {
tcm <- model %>% model_compute_terms_contributions()
if (is.null(tcm)) {
return(NULL)
} # nocov
w <- model %>% model_get_weights()
n <- dplyr::tibble(
term = colnames(tcm),
n_obs = colSums(tcm * w)
)
attr(n, "N_obs") <- sum(w)
ct <- model %>% model_get_coefficients_type()
if (ct %in% c("logistic", "poisson")) {
y <- model %>% model_get_response()
if (is.factor(y)) {
# the first level denotes failure and all others success
y <- as.integer(y != levels(y)[1])
}
n$n_event <- colSums(tcm * y * w)
attr(n, "N_event") <- sum(y * w)
}
if (ct == "poisson") {
off <- model %>% model_get_offset()
if (is.null(off)) off <- 0L
n$exposure <- colSums(tcm * exp(off) * w)
attr(n, "Exposure") <- sum(exp(off) * w)
}
n
}
#' @export
#' @rdname model_get_n
model_get_n.glmerMod <- model_get_n.glm
#' @export
#' @rdname model_get_n
model_get_n.multinom <- function(model) {
tcm <- model %>% model_compute_terms_contributions()
if (is.null(tcm)) {
return(NULL)
} # nocov
w <- model %>% model_get_weights()
y <- model %>% model_get_response()
if (!is.factor(y)) y <- factor(y)
n <- purrr::map_df(
levels(y)[-1],
~ dplyr::tibble(
y.level = .x,
term = colnames(tcm),
n_obs = colSums(tcm * w),
n_event = colSums((y == .x) * tcm * w)
)
)
attr(n, "N_obs") <- sum(w)
attr(n, "N_event") <- sum((y != levels(y)[1]) * w)
n
}
#' @export
#' @rdname model_get_n
model_get_n.LORgee <- function(model) {
if (stringr::str_detect(model$title, "NOMINAL")) {
model_get_n.multinom(model)
} else {
model_get_n.default(model)
}
}
#' @export
#' @rdname model_get_n
model_get_n.coxph <- function(model) {
tcm <- model %>% model_compute_terms_contributions()
if (is.null(tcm)) {
return(NULL)
} # nocov
w <- model %>% model_get_weights()
n <- dplyr::tibble(
term = colnames(tcm),
n_obs = colSums(tcm * w)
)
attr(n, "N_obs") <- sum(w)
y <- model %>% model_get_response()
status <- y[, ncol(y)]
if (ncol(y) == 3) {
time <- y[, 2] - y[, 1]
} else {
time <- y[, 1]
}
n$n_event <- colSums(tcm * status * w)
attr(n, "N_event") <- sum(status * w)
n$exposure <- colSums(tcm * time * w)
attr(n, "Exposure") <- sum(time * w)
n
}
#' @export
#' @rdname model_get_n
model_get_n.survreg <- model_get_n.coxph
#' @export
#' @rdname model_get_n
model_get_n.model_fit <- function(model) {
model_get_n(model$fit)
}
#' @export
#' @rdname model_get_n
model_get_n.tidycrr <- function(model) {
tcm <- model %>% model_compute_terms_contributions()
if (is.null(tcm)) {
return(NULL)
} # nocov
w <- model %>% model_get_weights()
n <- dplyr::tibble(
term = colnames(tcm),
n_obs = colSums(tcm * w)
)
attr(n, "N_obs") <- sum(w)
y <- model %>% model_get_response()
time <- y[, 1]
status <- as.integer(y[, 2] == model$failcode)
n$n_event <- colSums(tcm * status * w)
attr(n, "N_event") <- sum(status * w)
n$exposure <- colSums(tcm * time * w)
attr(n, "Exposure") <- sum(time * w)
n
}