2

I'm trying to generate manuscript ready summary tables using gtsummary. I have a factor variable with 3 levels (1,2 and 3), and would like to estimate group-wise effects comparing groups 2 and 3 to the reference group (group 1).

Here's a working example.

N = 1000
x1 = rnorm(N,mean=5, sd=2)
x2 = rnorm(N,mean=3, sd=3)
e = rnorm(N)
group = rep_along(along=1:N,x=c(1,2,3))
y1 = 4*group + 2*x1 - 3*x2 + e
y2 = 6*group + 1.5*x1 - 5*x2 + e
group = factor(group,levels=c("1","2","3"))

df = data.frame(y1,y2,group,x1,x2)

Getting overall test statistics is easy enough for y1 and y2 with

df %>% select(group,y1,y2,x1,x2) %>% 
  tbl_summary(by=group,missing="no") %>% add_n() %>%
  add_p(test=list(all_continuous() ~ "kruskal.test", all_categorical() ~ "fisher.test"))

but what i need is instead of 1 column with p-values for the overall group differences, i want 2 columns with p-values, the first with p-values comparing groups 2 vs 1 and the second with p-values comparing groups 3 vs 1. In effect i want the output of

summary(lm(y1~ group + x1 + x2))

incorporated into the summary table. The ancova option in gtsummary seems like it will do what i need (https://www.danieldsjoberg.com/gtsummary/reference/tests.html), but it's not clear how supply the parameters to the function. Here's where i'm stuck

df %>% select(group,y1,x1,x2) %>% 
  tbl_summary(by=group,missing="no") %>% add_n() %>%
  add_p(y1 ~ "ancova", test.args = list(by="group",adj.vars = c("x1","x2")))

Help with an explanation of the solution would be helpful, teach a person to fish and all.

0 Answers0