使用gtsummary包中的tbl_merge合并tbl_summary和tbl_regression中的行。

huangapple go评论162阅读模式
英文:

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_stacktbl_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("**肿瘤等级频率&lt;br&gt;按年龄组**", "**&lt;br&gt;单变量回归**")
))
英文:

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 &lt;- trial %&gt;%
mutate(gradeI = case_when(grade == &quot;I&quot; ~ 1,
TRUE ~ 0),
gradeII = case_when(grade == &quot;II&quot; ~ 1,
TRUE ~ 0),
gradeIII = case_when(grade == &quot;III&quot; ~ 1,
TRUE ~ 0),
agegp = case_when(age&lt;47 ~ 0,
TRUE ~ 1))
t1 &lt;- ex %&gt;% select(gradeI, agegp) %&gt;% tbl_summary(by = agegp)
t2 &lt;- ex %&gt;% select(gradeII, agegp) %&gt;% tbl_summary(by = agegp)
t3 &lt;- ex %&gt;% select(gradeIII, agegp) %&gt;% tbl_summary(by = agegp)
# Set labels
t1$table_body$label &lt;- &quot;Grade I&quot;
t2$table_body$label &lt;- &quot;Grade II&quot;
t3$table_body$label &lt;- &quot;Grade III&quot;
glm1 &lt;- ex %&gt;%
select(gradeI, agegp) %&gt;%
tbl_uvregression(
method = glm,
y = gradeI,
method.args = list(family = binomial),
exponentiate = TRUE,
# label = list(agegp ~ &quot;Grade I by age group&quot;))
label = list(agegp ~ &quot;Grade I&quot;))
glm2 &lt;- ex %&gt;%
select(gradeII, agegp) %&gt;%
tbl_uvregression(
method = glm,
y = gradeII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ &quot;Grade II&quot;))
glm3 &lt;- ex %&gt;%
select(gradeIII, agegp) %&gt;%
tbl_uvregression(
method = glm,
y = gradeIII,
method.args = list(family = binomial),
exponentiate = TRUE,
label = list(agegp ~ &quot;Grade III&quot;))
# Match variables
glm1$table_body$variable &lt;- &quot;gradeI&quot;
glm2$table_body$variable &lt;- &quot;gradeII&quot;
glm3$table_body$variable &lt;- &quot;gradeIII&quot;
tbl_merge(list(
tbl_stack(list(t1,t2,t3)), 
tbl_stack(list(glm1,glm2,glm3))), 
tab_spanner = c(&quot;**Tumor grade frequencies&lt;br&gt;by age group**&quot;, &quot;**&lt;br&gt;Univariate regression**&quot;)
)

huangapple
  • 本文由 发表于 2023年3月7日 01:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定