Reputation: 192
I have a dataset with age as continuous and as a factor, sex as a factor and 4 groups.
structure(list(Age = c(9, 12, 16, 57), Age_1 = structure(c(2L,
3L, 3L, 7L), .Label = c("8", "1", "2", "3", "4", "5", "6", "7"
), class = "factor"), Sex = structure(c(2L, 1L, 2L, 1L), .Label = c("M",
"F", "U"), class = "factor"), N = structure(c(2L, 2L, 2L,
2L), .Label = c("0", "1"), class = "factor"), G = structure(c(1L,
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), L_1 =
structure(c(1L,
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), C_1 =
structure(c(1L,
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), G_1 =
structure(c(1L,
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), m = structure(c(1L,
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), A = c(1,
1, 1, 1)), row.names = c(NA, 4L), class = "data.frame")
I want to do logistic regression for each variable (Age, Age_1 and sex) for each of the groups (N, G, L_1, C_1, G_1, m). eg.
mylogit <- glm(N ~ Sex, data = logistic_s, family = "binomial")
mylogit <- glm(N ~ Age, data = logistic_s, family = "binomial")
I am using gtsummary for combining the variables in a table.
library(gtsummary)
tbl_n <-
tbl_uvregression(
logistic_s[c("N", "Age", "sex", "Age_1")],
method = glm,
y = N,
method.args = list(family = binomial),
exponentiate = TRUE
)
tbl_n
This produces the output for one group (eg. N) with the variables Age, Age_1, Sex.
I want to repeat this with each of the groups (eg N, G, L_1 etc) and then combine the tables to make one combined table.
I am open to using different packages if there are other options that suit this better. I want to make a table that can be exported in word.
Upvotes: 2
Views: 2353
Reputation: 11680
I agree that some reproducible code would be helpful. I am not 100% certain what kind of output you're looking to get. You want to build some univariate logistic regression models, separately for 2 or more groups?
If that is correct, here is one way to go about it:
I will use the trial
data set in the gtsummary
package as an example. I'll make the grouping variable treatment (trt
).
library(gtsummary)
library(tidyverse)
trial_subset <-
trial %>%
select(trt, response, age, marker, grade)
We'll begin by constructing univariate regression tables stratified by trt
using the tbl_uvregression()
function from gtsummary
package. They will be stored in a new column in the data frame called tbl_uv
.
df_uv <-
trial_subset %>%
# group by trt, and nest data within group
group_by(trt) %>%
nest() %>%
# build univariate logistic regression models separately within grouping variable
mutate(
tbl_uv = map(
data,
~tbl_uvregression(
data = .x,
y = response,
method = glm,
method.args = list(family = binomial),
exponentiate = TRUE
)
)
)
#> # A tibble: 2 x 3
#> # Groups: trt [2]
#> trt data tbl_uv
#> <chr> <list> <list>
#> 1 Drug A <tibble [98 x 4]> <tbl_vrgr>
#> 2 Drug B <tibble [102 x 4]> <tbl_vrgr>
We can now use the tables saved in df_uv
to merge them into a single table using the tbl_merge()
function.
tbl_merge(
tbls = df_uv$tbl_uv, # list of the univariate logistic regression tables
tab_spanner = paste0("**", df_uv$trt, "**") # adding stars to bold the header
)
This produces the table below. I hope this is helpful!
Upvotes: 2