In a gtsummary table, I want to show the p-value for the difference in several variables between group A and group B, corrected for age and sex. I found here that I can use the ANCOVA test in gtsummary::add_p. However, I can't figure out how to specify my covariates "age" and "sex" in test_args.
In the following example, I want to calculate the p-value of the ancova for var1 and var2 (corrected for age and sex). Var3 is a variable with levels that I want to perform the chisq.test on.
library(dplyr)
library(gtsummary)
df <- data.frame(
group = c(2, 1, 1, 2, 1, 2),
var1 = (rnorm(6,mean=10, sd=3)),
var2 = (rnorm(6,mean=6, sd=1)),
var3 = c(0, 4, 1, 3, 1, 1),
age = c(50, 32, 26, 46, 38, 62),
sex = c(1, 0, 1, 1, 1, 0))
df %>%
tbl_summary(
by=group,
type=list(c("var1":"age") ~ 'continuous', "sex" ~ 'categorical'),
statistic = list(c("var1", "var2", "age") ~ "{mean} ± {sd}", "var3" ~ "{median} ({p25}, {p75})")
) %>%
add_p(test=list(c("var1", "var2") ~ "ancova", "var3" ~ "chisq.test"), test_args = list(all_tests("ancova") ~ list(by = group, adj.vars = c("age", "sex"))))
I get the following error
'...' must be empty
Problematic argument: test_args = (...)
I found a similar question here, but it was not answered unfortunately. Any help is greatly appreciated!
To get adjusted results, you will need to use
add_p()with a custom function for the p-value calculation. See below!Created on 2024-03-13 with reprex v2.1.0