英文:
merging rows from tbl_summary and tbl_regression using tbl_merge from the gtsummary package
问题
以下是翻译好的部分:
Long time browser, first time poster.
我长时间浏览者,第一次发帖。
I would like to merge multiple frequency tables created using tbl_summary()
, and then stacked with tbl_stack()
, with multiple tables of univariate logistic regressions created using tbl_regression()
, also stacked using tbl_stack()
.
我想要合并使用 tbl_summary()
创建的多个频率表,然后使用 tbl_stack()
进行堆叠,还有使用 tbl_regression()
创建的多个一元逻辑回归表,也使用 tbl_stack()
进行堆叠。
I would like the columns to be (1) the (binary) dependent variable names, (2) the frequency % of the dependent variable by strata of a binary independent variable, (3) the OR from the regression, (4) and the 95% confidence interval from the regression. I would like the rows of the merged table to be labeled as the dependent variables from the regressions.
我希望列包括(1)(二进制)因变量的名称,(2)依赖变量在二进制独立变量分层中的频率%,(3)来自回归的OR,(4)以及来自回归的95%置信区间。我希望合并表格的行标记为回归中的因变量。
I have tried stacking then merging, in which case the rows are grouped from the tbl_summary
stack and then the tbl_regression
stack, but with staggered empty columns. I have also tried merging then stacking, in which case the rows are sequentially staggered, with one from tbl_summary
followed by one from tbl_regression
and so on.
我尝试过先堆叠再合并,在这种情况下,行是从 tbl_summary
堆叠开始的,然后是 tbl_regression
堆叠,但有错位的空列。我还尝试了先合并再堆叠,在这种情况下,行是依次错位的,从 tbl_summary
开始,然后是从 tbl_regression
等等。
The problem I think I'm running up against is that the variable names in the tbl_summary
tables are preserved from the data set (the desired dependent variables), whereas the variable names in the tbl_regression
tables are the independent variable.
我认为我遇到的问题是,tbl_summary
表中的变量名从数据集中保留下来(所需的因变量),而tbl_regression
表中的变量名是独立变量。
I am not fluent in the gt
package, which I think could help me here...
我对gt
包不够熟练,但我认为它可以帮助我在这里...
Any advice and/or sample code is appreciated!
感谢任何建议和/或示例代码!
Reprex below.
以下是示例代码。
library(gtsummary)
library(dplyr)
packageVersion("gtsummary")
#> '1.6.2'
ex <- trial %>%
mutate(gradeI = case_when(grade == "I" ~ 1,
TRUE ~ 0),
gradeII = case_when(grade == "II" ~ 1,
TRUE ~ 0),
gradeIII = case_when(grade == "III" ~ 1,
TRUE ~ 0),
agegp = case_when(age<47 ~ 0,
TRUE ~ 1))
t1 <- ex %>%
select(gradeI, agegp) %>%
tbl_summary(by = agegp)
t2 <- ex %>%
select(gradeII, agegp) %>%
tbl_summary(by = agegp)
t3 <- ex %>%
select(gradeIII, agegp) %>%
tbl_summary(by = agegp)
#glm1 <- glm(grade1 ~ agegp, family = binomial(), data = ex)
#glm2 <- glm(grade2 ~ agegp, family = binomial(), data = ex)
#glm2 <- glm(grade2 ~ agegp, family = binomial(), data = ex)
glm1 <- ex %>%
select(gradeI, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeI,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade I by age group"))
glm2 <- ex %>%
select(gradeII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade II by age group"))
glm3 <- ex %>%
select(gradeIII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeIII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade II by age group"))
merge1 <- tbl_merge(tbls = list(t1, glm1),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**"))
merge2 <- tbl_merge(tbls = list(t2, glm2),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**"))
merge3 <- tbl_merge(tbls = list(t3, glm3),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**"))
tbl_stack(tbls = list(merge1, merge2, merge3))
英文:
Long time browser, first time poster.
I would like to merge multiple frequency tables created using tbl_summary()
, and then stacked with tbl_stack()
, with multiple tables of univariate logistic regressions created using tbl_regression()
, also stacked using tbl_stack()
.
I would like the columns to be (1) the (binary) dependent variable names, (2) the frequency % of the dependent variable by strata of a binary independent variable, (3) the OR from the regression, (4) and the 95% confidence interval from the regression. I would like the rows of the merged table to be labeled as the dependent variables from the regressions.
I have tried stacking then merging, in which case the rows are grouped from the tbl_summary
stack and then the tbl_regression
stack, but with staggered empty columns. I have also tried merging then stacking, in which case the rows are sequentially staggered, with one from tbl_summary
followed by one from tbl_regression
and so on.
The problem I think I'm running up against is that the variable names in the tbl_summary
tables are preserved from the data set (the desired dependent variables), whereas the variable names in the tbl_regression
tables are the independent variable.
I am not fluent in the gt
package, which I think could help me here...
Any advice and/or sample code is appreciated!
Reprex below.
library(gtsummary)
library(dplyr)
packageVersion("gtsummary")
#> '1.6.2'
ex <- trial %>%
mutate(gradeI = case_when(grade == "I" ~ 1,
TRUE ~ 0),
gradeII = case_when(grade == "II" ~ 1,
TRUE ~ 0),
gradeIII = case_when(grade == "III" ~ 1,
TRUE ~ 0),
agegp = case_when(age<47 ~ 0,
TRUE ~ 1))
t1 <- ex %>% select(gradeI, agegp) %>% tbl_summary(by = agegp)
t2 <- ex %>% select(gradeII, agegp) %>% tbl_summary(by = agegp)
t3 <- ex %>% select(gradeIII, agegp) %>% tbl_summary(by = agegp)
#glm1 <- glm(grade1 ~ agegp, family = binomial(), data = ex)
#glm2 <- glm(grade2 ~ agegp, family = binomial(), data = ex)
#glm2 <- glm(grade2 ~ agegp, family = binomial(), data = ex)
glm1 <- ex %>%
select(gradeI, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeI,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade I by age group"))
glm2 <- ex %>%
select(gradeII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade II by age group"))
glm3 <- ex %>%
select(gradeIII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeIII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade II by age group"))
merge1 <- tbl_merge(tbls = list(t1, glm1),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**"))
merge2 <- tbl_merge(tbls = list(t2, glm2),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**"))
merge3 <- tbl_merge(tbls = list(t3, glm3),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**"))
tbl_stack(tbls = list(merge1, merge2, merge3))
答案1
得分: 0
你需要匹配两个表格中的变量和标签。这些信息存储在表格主体中。我认为你应该只调用一次 tab_spanner
,所以我已经交换了 tbl_stack
和 tbl_merge
的顺序。
我已经为您的结果使用了通用标签 "Grade X"。我猜您可以在调用 tbl_summary
时设置标签,但我直接在 table_body
中设置了标签。
library(gtsummary)
library(dplyr)
ex <- trial %>%
mutate(gradeI = case_when(grade == "I" ~ 1,
TRUE ~ 0),
gradeII = case_when(grade == "II" ~ 1,
TRUE ~ 0),
gradeIII = case_when(grade == "III" ~ 1,
TRUE ~ 0),
agegp = case_when(age < 47 ~ 0,
TRUE ~ 1))
t1 <- ex %>% select(gradeI, agegp) %>% tbl_summary(by = agegp)
t2 <- ex %>% select(gradeII, agegp) %>% tbl_summary(by = agegp)
t3 <- ex %>% select(gradeIII, agegp) %>% tbl_summary(by = agegp)
# 设置标签
t1$table_body$label <- "Grade I"
t2$table_body$label <- "Grade II"
t3$table_body$label <- "Grade III"
glm1 <- ex %>%
select(gradeI, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeI,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade I"))
glm2 <- ex %>%
select(gradeII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade II"))
glm3 <- ex %>%
select(gradeIII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeIII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade III"))
# 匹配变量
glm1$table_body$variable <- "gradeI"
glm2$table_body$variable <- "gradeII"
glm3$table_body$variable <- "gradeIII"
tbl_merge(list(
tbl_stack(list(t1,t2,t3)),
tbl_stack(list(glm1,glm2,glm3)),
tab_spanner = c("**肿瘤等级频率<br>按年龄组**", "**<br>单变量回归**")
))
英文:
You need to match both the variable and the labels in both tables. These are stored in the table body. I think that you should only call the tab_spanner
once so I have flipped the order of tbl_stack
and tbl_merge
.
I have used the common label "Grade X" for your outcomes. I guess you can set the labels in the call for tbl_summary
but I did it directly in the table_body
.
library(gtsummary)
library(dplyr)
ex <- trial %>%
mutate(gradeI = case_when(grade == "I" ~ 1,
TRUE ~ 0),
gradeII = case_when(grade == "II" ~ 1,
TRUE ~ 0),
gradeIII = case_when(grade == "III" ~ 1,
TRUE ~ 0),
agegp = case_when(age<47 ~ 0,
TRUE ~ 1))
t1 <- ex %>% select(gradeI, agegp) %>% tbl_summary(by = agegp)
t2 <- ex %>% select(gradeII, agegp) %>% tbl_summary(by = agegp)
t3 <- ex %>% select(gradeIII, agegp) %>% tbl_summary(by = agegp)
# Set labels
t1$table_body$label <- "Grade I"
t2$table_body$label <- "Grade II"
t3$table_body$label <- "Grade III"
glm1 <- ex %>%
select(gradeI, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeI,
method.args = list(family = binomial),
exponentiate = TRUE,
# label = list(agegp ~ "Grade I by age group"))
label = list(agegp ~ "Grade I"))
glm2 <- ex %>%
select(gradeII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade II"))
glm3 <- ex %>%
select(gradeIII, agegp) %>%
tbl_uvregression(
method = glm,
y = gradeIII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ "Grade III"))
# Match variables
glm1$table_body$variable <- "gradeI"
glm2$table_body$variable <- "gradeII"
glm3$table_body$variable <- "gradeIII"
tbl_merge(list(
tbl_stack(list(t1,t2,t3)),
tbl_stack(list(glm1,glm2,glm3))),
tab_spanner = c("**Tumor grade frequencies<br>by age group**", "**<br>Univariate regression**")
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论