Skip to content

Commit b331fbe

Browse files
committed
Updating for multinomial models
1 parent c1edb26 commit b331fbe

7 files changed

+72
-43
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Imports:
5959
Suggests:
6060
aod (>= 1.3.3),
6161
broom (>= 1.0.5),
62-
broom.helpers (>= 1.17.0),
62+
broom.helpers (>= 1.20.0),
6363
broom.mixed (>= 0.2.9),
6464
car (>= 3.0-11),
6565
cardx (>= 0.2.4),

NAMESPACE

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ S3method(tbl_regression,lmerMod)
7373
S3method(tbl_regression,mipo)
7474
S3method(tbl_regression,mira)
7575
S3method(tbl_regression,model_fit)
76-
S3method(tbl_regression,multinom)
7776
S3method(tbl_regression,stanreg)
7877
S3method(tbl_regression,survreg)
7978
S3method(tbl_regression,workflow)

R/tbl_regression.R

+45
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ tbl_regression.default <- function(x,
223223
get_theme_element("tbl_regression-fn:addnl-fn-to-run", default = identity) |>
224224
do.call(list(res))
225225

226+
# running function for multimodel models (and similar) for grouped results ---
227+
res <- .regression_grouped_results(res)
228+
226229
# return results -------------------------------------------------------------
227230
res <- res |>
228231
modify_table_styling(
@@ -233,3 +236,45 @@ tbl_regression.default <- function(x,
233236
res$call_list <- list(tbl_regression = match.call())
234237
res
235238
}
239+
240+
.regression_grouped_results <- function(x) {
241+
# if grouped model, then adjusting output accordingly
242+
if ("group_by" %in% names(x$table_body)) {
243+
# rename the grouping column
244+
x$table_body <-
245+
x$table_body |>
246+
dplyr::rename(groupname_col = "group_by") |>
247+
dplyr::relocate("groupname_col", .before = "label")
248+
249+
# assign header label
250+
x <- x |>
251+
modify_table_styling(
252+
columns = "groupname_col",
253+
hide = FALSE,
254+
label = .grouped_data_header(x$inputs$x),
255+
align = "left"
256+
)
257+
258+
# warning about multinomial models
259+
cli::cli_inform(
260+
c("i" = "Multinomial models and other grouped models have a different
261+
underlying structure than the models gtsummary was designed for.",
262+
"*" = "Functions designed to work with {.fun tbl_regression} objects may yield unexpected results.")
263+
)
264+
}
265+
266+
# return regression tbl
267+
x
268+
}
269+
270+
.grouped_data_header <- function(x) {
271+
UseMethod(".grouped_data_header")
272+
}
273+
274+
.grouped_data_header.default <- function(x) {
275+
"**Group**"
276+
}
277+
278+
.grouped_data_header.default <- function(x) {
279+
"**Outcome**"
280+
}

R/tbl_regression_methods.R

-37
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ tbl_regression.survreg <- function(x, tidy_fun = function(x, ...) broom::tidy(x,
4949
tbl_regression.mira <- function(x, tidy_fun = pool_and_tidy_mice, ...) {
5050
tbl <- tbl_regression.default(x = x, tidy_fun = tidy_fun, ...)
5151

52-
# adding outcome levels to multinomial models
53-
if (inherits(x$analyses[[1]], "multinom")) {
54-
tbl <- .multinom_modifations(tbl)
55-
}
56-
5752
tbl
5853
}
5954

@@ -113,35 +108,3 @@ tbl_regression.crr <- function(x, ...) {
113108
tbl_regression.default(x = x, ...)
114109
}
115110

116-
#' @export
117-
#' @rdname tbl_regression_methods
118-
tbl_regression.multinom <- function(x, ...) {
119-
result <- tbl_regression.default(x = x, ...)
120-
121-
# grouping by outcome, and printing warning message
122-
.multinom_modifations(result)
123-
}
124-
125-
.multinom_modifations <- function(x) {
126-
# adding a grouped header for the outcome levels
127-
x$table_body <-
128-
x$table_body |>
129-
mutate(groupname_col = .data$y.level, .before = 1L)
130-
131-
x <- modify_table_styling(
132-
x = x,
133-
columns = all_of("groupname_col"),
134-
hide = FALSE,
135-
label = "**Outcome**",
136-
align = "left"
137-
)
138-
139-
# warning about multinomial models
140-
cli::cli_inform(
141-
c("i" = "Multinomial models have a different underlying structure than the
142-
models gtsummary was designed for.",
143-
"*" = "Functions designed to work with {.fun tbl_regression} objects may yield unexpected results.")
144-
)
145-
146-
x
147-
}

man/tbl_regression_methods.Rd

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/tbl_regression_methods.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@
105105
**Characteristic** **Beta** **95% CI**
106106
1 hp -0.03 -0.06, 0.00
107107

108+
---
109+
110+
Code
111+
as.data.frame(tbl_regression(lme4::lmer(mpg ~ hp + (1 | cyl), mtcars), tidy = broom.mixed::tidy,
112+
group_by = "effect", group_label = c(fixed = "Fixed Effects", ran_pars = "Random Effects")))
113+
Message
114+
i Multinomial models and other grouped models have a different underlying structure than the models gtsummary was designed for.
115+
* Functions designed to work with `tbl_regression()` objects may yield unexpected results.
116+
Output
117+
**Outcome** **Characteristic** **Beta** **95% CI**
118+
1 Fixed Effects hp -0.03 -0.06, 0.00
119+
2 Random Effects cyl.sd__(Intercept) 4.0 <NA>
120+
3 <NA> Residual.sd__Observation 3.1 <NA>
121+
108122
# tbl_regression.gam()
109123

110124
Code
@@ -151,7 +165,7 @@
151165
final value 29.311125
152166
converged
153167
Message
154-
i Multinomial models have a different underlying structure than the models gtsummary was designed for.
168+
i Multinomial models and other grouped models have a different underlying structure than the models gtsummary was designed for.
155169
* Functions designed to work with `tbl_regression()` objects may yield unexpected results.
156170
Output
157171
**Outcome** **Characteristic** **log(OR)** **95% CI** **p-value**

tests/testthat/test-tbl_regression_methods.R

+11
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ test_that("tbl_regression.lmerMod()", {
6666
tbl_regression() |>
6767
as.data.frame()
6868
)
69+
70+
expect_snapshot(
71+
lme4::lmer(mpg ~ hp + (1 | cyl), mtcars) |>
72+
tbl_regression(
73+
tidy = broom.mixed::tidy,
74+
group_by = "effect",
75+
group_label = c(fixed = "Fixed Effects",
76+
ran_pars = "Random Effects")
77+
) |>
78+
as.data.frame()
79+
)
6980
})
7081

7182
test_that("tbl_regression.gam()", {

0 commit comments

Comments
 (0)