2  Group comparisons and p-values

2.1 Summarizing by group: by

The columns are split by treatment group, and each header shows the group size N =.

2.2 Adding overall and N columns

gtsummary is designed so that you build a table with tbl_summary() and add information by chaining add_*() functions with |>.

2.3 Adding p-values: add_p()

Tests used by default (for two groups):

Variable type Default test
Continuous Wilcoxon rank-sum test (Mann-Whitney U test)
Categorical Pearson’s chi-squared test (Fisher’s exact test if any cell has an expected count below 5)

With three or more groups, continuous variables use the Kruskal-Wallis test. The tests used are stated in the footnote.

2.4 Specifying tests: test

If you summarized with mean (SD), it is natural to use the t-test as well.

Commonly used test names:

Specification Test
"t.test" t-test (Welch by default)
"wilcox.test" Wilcoxon rank-sum test
"aov" One-way analysis of variance (3+ groups)
"kruskal.test" Kruskal-Wallis test
"chisq.test" Chi-squared test (with continuity correction)
"chisq.test.no.correct" Chi-squared test (no correction)
"fisher.test" Fisher’s exact test
"paired.t.test" etc. Paired tests (require the group argument)

The full list is at ?gtsummary::tests.

2.5 Number of digits for p-values

label_style_pvalue(digits = 3) creates a formatting function that shows “3 decimal places, and <0.001 below 0.001”.

2.6 Multiple-comparison adjustment: add_q()

When testing many variables, you may add q-values adjusted for the false discovery rate (FDR) or similar.

2.7 Differences and confidence intervals: add_difference()

More and more journals ask you to show not only p-values but between-group differences and their 95% confidence intervals.

  • Continuous variables: difference in means (t-test)
  • 0/1 variables: difference in proportions

When multi-level categorical variables are included, use the standardized mean difference (SMD). It is often used to show group balance in observational studies.

2.8 Confidence intervals for each group: add_ci()

2.9 Showing statistic labels on the rows: add_stat_label()

Use this when you want to write “Median (Q1, Q3)” etc. on each row instead of in the footnote.

ImportantChapter summary
  • by = for per-group columns; add_overall() / add_n() for overall and N columns
  • add_p() for p-values. Change tests with test =, and digits with pvalue_fun =
  • add_q() (multiple-comparison adjustment), add_difference() (differences and CIs, SMD), add_ci() (per-group CIs)