gpt4 book ai didi

r - 使用 gtsummary 包的宽格式多项逻辑回归结果表

转载 作者:行者123 更新时间:2023-12-05 01:10:09 33 4
gpt4 key购买 nike

我有一个有四个结果的因变量。我使用 mlogit 包进行多项逻辑回归。

当我尝试使用 gtsummary 包呈现结果时,我的多项逻辑回归结果堆叠在一起(参见下面的代码和表格)。

有没有办法让结果并排在一行中,只使用一组标签作为级别,而不是像下表那样堆叠在一起?

# 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)

enter image description here

最佳答案

默认情况下,多项模型将以长格式打印。

我编写了一个小函数来将结果转换为宽,并将其保存为 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()

enter image description here

关于r - 使用 gtsummary 包的宽格式多项逻辑回归结果表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64463878/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com