使用 gtsummary 包的宽格式多项逻辑回归结果 table
multinomial logistic regression results table in wide format using the gtsummary package
我有一个具有四个结果的因变量。我已经使用 mlogit 包进行多项逻辑回归。
当我尝试使用 gtsummary 包显示结果时,我的多项式逻辑回归结果堆叠在一起(参见下面的代码和 table)。
是否可以仅使用一组级别标签将结果并排排成一行,而不是像下面的 table 那样堆叠在一起?
# load packages
library(gtsummary)
library(nnet)
# dummy data
crime <-data.frame(city = sample(c("SF", "AR", "NYC","MN"),13000,replace = TRUE),
year = sample(as.factor(c(1990, 2000, 1999, 1989)),13000,replace = TRUE)
)
# multinom model tabulated with gtsummary
multinom(city ~ year, data = crime) %>%
tbl_regression(exponentiate = T)
默认情况下,多项式模型将以长格式打印。
我编写了一个小函数来将结果转换为宽屏并将其保存为 GitHub Gist。 https://gist.github.com/ddsjoberg/a55afa74ac58e1f895862fcabab62406
set.seed(20210511)
library(gtsummary)
library(magrittr)
multinom_pivot_wider <- function(x) {
# check inputs match expectatations
if (!inherits(x, "tbl_regression") || !inherits(x$model_obj, "multinom")) {
stop("`x=` must be class 'tbl_regression' summary of a `nnet::multinom()` model.")
}
# create tibble of results
df <- tibble::tibble(outcome_level = unique(x$table_body$groupname_col))
df$tbl <-
purrr::map(
df$outcome_level,
function(lvl) {
gtsummary::modify_table_body(
x,
~dplyr::filter(.x, .data$groupname_col %in% lvl) %>%
dplyr::ungroup() %>%
dplyr::select(-.data$groupname_col)
)
}
)
tbl_merge(df$tbl, tab_spanner = paste0("**", df$outcome_level, "**"))
}
# dummy data
crime <-
data.frame(
city = sample(c("SF", "AR", "NYC", "MN"), 13000, replace = TRUE),
year = sample(as.factor(c(1990, 2000, 1999, 1989)), 13000, replace = TRUE)
)
# multinom model tabulated with gtsummary
tbl <-
nnet::multinom(city ~ year, data = crime) %>%
tbl_regression(exponentiate = TRUE) %>%
multinom_pivot_wider()
我有一个具有四个结果的因变量。我已经使用 mlogit 包进行多项逻辑回归。
当我尝试使用 gtsummary 包显示结果时,我的多项式逻辑回归结果堆叠在一起(参见下面的代码和 table)。
是否可以仅使用一组级别标签将结果并排排成一行,而不是像下面的 table 那样堆叠在一起?
# load packages
library(gtsummary)
library(nnet)
# dummy data
crime <-data.frame(city = sample(c("SF", "AR", "NYC","MN"),13000,replace = TRUE),
year = sample(as.factor(c(1990, 2000, 1999, 1989)),13000,replace = TRUE)
)
# multinom model tabulated with gtsummary
multinom(city ~ year, data = crime) %>%
tbl_regression(exponentiate = T)
默认情况下,多项式模型将以长格式打印。
我编写了一个小函数来将结果转换为宽屏并将其保存为 GitHub Gist。 https://gist.github.com/ddsjoberg/a55afa74ac58e1f895862fcabab62406
set.seed(20210511)
library(gtsummary)
library(magrittr)
multinom_pivot_wider <- function(x) {
# check inputs match expectatations
if (!inherits(x, "tbl_regression") || !inherits(x$model_obj, "multinom")) {
stop("`x=` must be class 'tbl_regression' summary of a `nnet::multinom()` model.")
}
# create tibble of results
df <- tibble::tibble(outcome_level = unique(x$table_body$groupname_col))
df$tbl <-
purrr::map(
df$outcome_level,
function(lvl) {
gtsummary::modify_table_body(
x,
~dplyr::filter(.x, .data$groupname_col %in% lvl) %>%
dplyr::ungroup() %>%
dplyr::select(-.data$groupname_col)
)
}
)
tbl_merge(df$tbl, tab_spanner = paste0("**", df$outcome_level, "**"))
}
# dummy data
crime <-
data.frame(
city = sample(c("SF", "AR", "NYC", "MN"), 13000, replace = TRUE),
year = sample(as.factor(c(1990, 2000, 1999, 1989)), 13000, replace = TRUE)
)
# multinom model tabulated with gtsummary
tbl <-
nnet::multinom(city ~ year, data = crime) %>%
tbl_regression(exponentiate = TRUE) %>%
multinom_pivot_wider()