Skip to contents

Create a label-aware comparison table using gtsummary::tbl_summary() with optional global hypothesis tests, covariate-adjusted tests, effect sizes, and pairwise comparisons.

Usage

MakeComparisonTable(
  data,
  group_var = NULL,
  variables,
  ...,
  covariates = NULL,
  value_digits = 2,
  p_digits = 3,
  AddEffectSize = FALSE,
  effect_size_digits = 2,
  AddPairwise = FALSE,
  PairwiseMethod = "bonferroni",
  Parametric = TRUE,
  ParametricDisplay = NULL,
  IncludeOverallN = FALSE,
  IncludeMissing = FALSE,
  suppress_warnings = FALSE,
  Referent = NULL,
  IncludeOverallStats = FALSE,
  ShowPositiveBinaryOnLabel = TRUE,
  CatMethod = c("auto", "chisq", "fisher"),
  MultiCatAdjusted = c("multinomial_LR", "none"),
  ShowNotes = c("auto", "always", "never"),
  NotesPosition = c("last", "after_test", "before_pairwise"),
  Relabel = TRUE,
  TreatOrdinalAs = "Categorical",
  DataFrame = lifecycle::deprecated(),
  CompVariable = lifecycle::deprecated(),
  Variables = lifecycle::deprecated(),
  Covariates = lifecycle::deprecated(),
  ValueDigits = lifecycle::deprecated(),
  pDigits = lifecycle::deprecated(),
  EffectSizeDigits = lifecycle::deprecated()
)

Arguments

data

A data frame.

group_var

Character scalar naming the grouping variable.

variables

Character vector of variables to summarize.

...

Optional additional variable names supplied individually.

covariates

Optional character vector of covariates for adjusted models.

value_digits

Number of digits for descriptive statistics.

p_digits

Number of digits for p-values.

AddEffectSize

Logical; add effect-size columns.

effect_size_digits

Number of digits for effect sizes.

AddPairwise

Logical; add pairwise comparison columns.

PairwiseMethod

P-value adjustment method. Use "none" for no adjustment.

Parametric

Logical; use parametric tests for continuous outcomes.

ParametricDisplay

Logical; display continuous summaries as mean (SD). If FALSE, display median IQR. Defaults to Parametric.

IncludeOverallN

Logical; add N column.

IncludeMissing

Logical; include missing rows in summaries.

suppress_warnings

Logical; suppress selected gtsummary warnings.

Referent

Optional reference group for pairwise comparisons.

IncludeOverallStats

Logical; add overall summary column.

ShowPositiveBinaryOnLabel

Logical; for binary variables, show only the positive level where identifiable.

CatMethod

Categorical test method. One of "auto", "chisq", "fisher".

MultiCatAdjusted

Adjusted multicategory method. Currently "multinomial_LR" or "none".

ShowNotes

Whether to show the Analysis notes column. One of "auto", "always", or "never".

NotesPosition

Analysis notes column position. One of "last", "after_test", or "before_pairwise".

Relabel

Logical; if TRUE (default), use attached variable labels.

TreatOrdinalAs

How ordinal variables are treated: "Categorical", "Continuous", "Both", or "Exclude".

DataFrame

Deprecated (since 19.15.0). Use data instead.

CompVariable

Deprecated (since 19.15.0). Use group_var instead.

Variables

Deprecated (since 19.15.0). Use variables instead.

Covariates

Deprecated (since 19.15.0). Use covariates instead.

ValueDigits

Deprecated (since 19.15.0). Use value_digits instead.

pDigits

Deprecated (since 19.15.0). Use p_digits instead.

EffectSizeDigits

Deprecated (since 19.15.0). Use effect_size_digits instead.

Value

A gtsummary object.

Details

Continuous variables are numeric variables with more than two unique non-missing values. Numeric variables with exactly two unique values are treated as dichotomous categorical variables.

When covariates are supplied, continuous outcomes are tested using ANCOVA with Type II tests. If Parametric = FALSE, robust HC3 covariance is used for the group-level Wald test and adjusted pairwise comparisons.

For parametric continuous outcomes, effect sizes are reported as absolute Cohen's d for two-group comparisons and Cohen's f for omnibus comparisons involving more than two groups. With covariates, two-group Cohen's d uses the estimated marginal mean difference divided by the model residual standard deviation, and multi-group Cohen's f is calculated from the Type II ANCOVA group effect. These effect-size scales are not numerically equivalent.

Binary categorical outcomes with covariates are tested using logistic regression likelihood-ratio tests. Multicategory categorical outcomes with covariates are tested using multinomial likelihood-ratio tests.

Pairwise comparisons preserve non-standard group labels and variable names.

Choosing the options

The defaults produce one test per variable, chosen from that variable's type: a t-test for continuous variables, a chi-squared test for categorical ones. The remaining arguments each answer a specific question the default table cannot.

AddEffectSize adds the standardized magnitude beside the p-value. A p-value says whether a difference is detectable, not whether it is large, and effect sizes are what make two significant rows comparable to each other.

AddPairwise matters as soon as there are more than two groups. The omnibus test only says "these groups are not all the same"; it never says which pair differs. Pairwise contrasts answer that, corrected across the contrasts so that hunting through them does not inflate the error rate. PairwiseMethod selects the correction - Bonferroni by default, "fdr" when there are many contrasts, "none" for exploratory work. Referent compares every group against one reference level instead of against each other, which is usually what a control group is for.

covariates replaces the simple test with a model-based one that holds the named variables constant. An unadjusted group difference in a biomarker may only reflect that the groups differ in age or sex; adjusting reports the group difference that remains.

Parametric = FALSE switches continuous comparisons to rank-based tests (Wilcoxon, or Kruskal-Wallis for more than two groups), which is the right choice for skewed measures or small, uneven groups.

IncludeOverallN and IncludeMissing add an overall column and explicit missing-value counts, for a table that has to stand on its own in a manuscript.

References

This function wraps gtsummary. Please cite:

Sjoberg, D. D., Whiting, K., Curry, M., Lavery, J. A., & Larmarange, J. (2021). Reproducible summary tables with the gtsummary package. The R Journal, 13(1), 570-580. doi:10.32614/RJ-2021-053

Examples

# \donttest{
data(SampleData)
data(SampleVariableTypes)

Labelled <- RevalueData(SampleData, SampleVariableTypes)$RevaluedData

vars_Compare <- c("age", "sex", "AXL", "Adiponectin", "tau", "p_tau")

# Two groups, default tests
MakeComparisonTable(
  data = Labelled,
  group_var = "Diagnosis",
  variables = vars_Compare
)
Comparison by Diagnosis (values: mean (SD)).
Characteristic Control
N = 242
1
Impaired
N = 91
1
p-value2 Test2
Age 72.75 (13.26) 71.78 (13.12) 0.553 Welch t-test
Sex

0.0272 Pearson chi-squared
    Female 157 (65%) 47 (52%)

    Male 85 (35%) 44 (48%)

AXL receptor tyrosine kinase 0.28 (0.46) 0.34 (0.41) 0.238 Welch t-test
Adiponectin -5.26 (0.65) -5.10 (0.69) 0.0539 Welch t-test
Tau protein 5.61 (0.49) 6.14 (0.55) <0.001 Welch t-test
Phosphorylated tau protein 3.94 (0.42) 4.30 (0.48) <0.001 Welch t-test
1 Mean (SD); n (%)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# With effect sizes MakeComparisonTable( data = Labelled, group_var = "Diagnosis", variables = vars_Compare, AddEffectSize = TRUE )
Comparison by Diagnosis (values: mean (SD)).
Characteristic Control
N = 242
1
Impaired
N = 91
1
p-value2 Test2 Effect size ES method
Age 72.75 (13.26) 71.78 (13.12) 0.553 Welch t-test 0.07 |d|
Sex

0.0272 Pearson chi-squared 0.12 Cramer's V
    Female 157 (65%) 47 (52%)



    Male 85 (35%) 44 (48%)



AXL receptor tyrosine kinase 0.28 (0.46) 0.34 (0.41) 0.238 Welch t-test 0.14 |d|
Adiponectin -5.26 (0.65) -5.10 (0.69) 0.0539 Welch t-test 0.25 |d|
Tau protein 5.61 (0.49) 6.14 (0.55) <0.001 Welch t-test 1.03 |d|
Phosphorylated tau protein 3.94 (0.42) 4.30 (0.48) <0.001 Welch t-test 0.83 |d|
1 Mean (SD); n (%)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# Collapse the six APOE genotypes into three risk groups Labelled$ApoeGroup <- factor( dplyr::case_when( Labelled$Genotype %in% c("E2E2", "E2E3") ~ "E2 carrier", Labelled$Genotype == "E3E3" ~ "E3/E3", TRUE ~ "E4 carrier" ), levels = c("E2 carrier", "E3/E3", "E4 carrier") ) # Three groups, with pairwise contrasts MakeComparisonTable( data = Labelled, group_var = "ApoeGroup", variables = vars_Compare, AddEffectSize = TRUE, AddPairwise = TRUE )
Comparison by ApoeGroup (values: mean (SD)). Pairwise p-values are bonferroni-adjusted within outcome.
Characteristic E2 carrier
N = 39
1
E3/E3
N = 167
1
E4 carrier
N = 127
1
p-value2 Test2 Effect size ES method E2 carrier - E3/E3 E2 carrier - E4 carrier E3/E3 - E4 carrier
Age 71.36 (13.33) 72.04 (13.87) 73.38 (12.33) 0.599 ANOVA 0.06 Cohen's f 1 1 1
Sex


0.575 Pearson chi-squared 0.06 Cramer's V 1 1 1
    Female 22 (56%) 100 (60%) 82 (65%)






    Male 17 (44%) 67 (40%) 45 (35%)






AXL receptor tyrosine kinase 0.30 (0.43) 0.31 (0.47) 0.28 (0.42) 0.882 ANOVA 0.03 Cohen's f 1 1 1
Adiponectin -5.12 (0.68) -5.27 (0.70) -5.18 (0.62) 0.354 ANOVA 0.08 Cohen's f 0.713 1 0.821
Tau protein 5.65 (0.44) 5.67 (0.55) 5.89 (0.58) 0.0121 ANOVA 0.20 Cohen's f 1 0.0643 0.0218
Phosphorylated tau protein 3.93 (0.37) 3.99 (0.47) 4.15 (0.46) 0.00352 ANOVA 0.19 Cohen's f 1 0.0104 0.0111
1 Mean (SD); n (%)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes. Pairwise p-values are bonferroni-adjusted within outcome.
# FDR instead of Bonferroni across the contrasts MakeComparisonTable( data = Labelled, group_var = "ApoeGroup", variables = c("age", "tau", "p_tau"), AddPairwise = TRUE, PairwiseMethod = "fdr" )
Comparison by ApoeGroup (values: mean (SD)). Pairwise p-values are fdr-adjusted within outcome.
Characteristic E2 carrier
N = 39
1
E3/E3
N = 167
1
E4 carrier
N = 127
1
p-value2 Test2 E2 carrier - E3/E3 E2 carrier - E4 carrier E3/E3 - E4 carrier
Age 71.36 (13.33) 72.04 (13.87) 73.38 (12.33) 0.599 ANOVA 0.776 0.607 0.607
Tau protein 5.65 (0.44) 5.67 (0.55) 5.89 (0.58) 0.0121 ANOVA 0.799 0.0321 0.0218
Phosphorylated tau protein 3.93 (0.37) 3.99 (0.47) 4.15 (0.46) 0.00352 ANOVA 0.422 0.00557 0.00557
1 Mean (SD)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes. Pairwise p-values are fdr-adjusted within outcome.
# Every group against one reference level MakeComparisonTable( data = Labelled, group_var = "ApoeGroup", variables = c("age", "tau", "p_tau"), AddPairwise = TRUE, Referent = "E3/E3" )
Comparison by ApoeGroup (values: mean (SD)). Pairwise p-values are bonferroni-adjusted within outcome.
Characteristic E2 carrier
N = 39
1
E3/E3
N = 167
1
E4 carrier
N = 127
1
p-value2 Test2 E3/E3 - E2 carrier E3/E3 - E4 carrier
Age 71.36 (13.33) 72.04 (13.87) 73.38 (12.33) 0.599 ANOVA 1 0.788
Tau protein 5.65 (0.44) 5.67 (0.55) 5.89 (0.58) 0.0121 ANOVA 1 0.0145
Phosphorylated tau protein 3.93 (0.37) 3.99 (0.47) 4.15 (0.46) 0.00352 ANOVA 0.843 0.00743
1 Mean (SD)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes. Pairwise p-values are bonferroni-adjusted within outcome.
# Adjusted for age and sex MakeComparisonTable( data = Labelled, group_var = "Diagnosis", variables = c("AXL", "Adiponectin", "tau", "p_tau"), covariates = c("age", "sex") )
Comparison by Diagnosis (values: mean (SD)). p-values adjusted for Age, Sex.
Characteristic Control
N = 242
1
Impaired
N = 91
1
p-value2 Test2
AXL receptor tyrosine kinase 0.28 (0.46) 0.34 (0.41) 0.296 ANCOVA (Type II)
Adiponectin -5.26 (0.65) -5.10 (0.69) 0.0804 ANCOVA (Type II)
Tau protein 5.61 (0.49) 6.14 (0.55) <0.001 ANCOVA (Type II)
Phosphorylated tau protein 3.94 (0.42) 4.30 (0.48) <0.001 ANCOVA (Type II)
1 Mean (SD)
2 p-values use Type II ANCOVA for continuous outcomes and logistic likelihood-ratio test for binary outcomes or multinomial likelihood-ratio test for multicategory outcomes for categorical outcomes.
# Rank-based tests for the continuous variables MakeComparisonTable( data = Labelled, group_var = "Diagnosis", variables = vars_Compare, Parametric = FALSE )
Comparison by Diagnosis (values: median [IQR]).
Characteristic Control
N = 242
1
Impaired
N = 91
1
p-value2 Test2
Age 74.00 [64.00, 83.00] 73.00 [63.00, 82.00] 0.707 Wilcoxon rank-sum
Sex

0.0272 Pearson chi-squared
    Female 157 (65%) 47 (52%)

    Male 85 (35%) 44 (48%)

AXL receptor tyrosine kinase 0.28 [-0.04, 0.61] 0.28 [0.10, 0.61] 0.481 Wilcoxon rank-sum
Adiponectin -5.26 [-5.74, -4.89] -5.08 [-5.57, -4.61] 0.0303 Wilcoxon rank-sum
Tau protein 5.57 [5.25, 6.00] 6.16 [5.83, 6.51] <0.001 Wilcoxon rank-sum
Phosphorylated tau protein 3.87 [3.65, 4.24] 4.26 [4.04, 4.63] <0.001 Wilcoxon rank-sum
1 Median [Q1, Q3]; n (%)
2 p-values use Wilcoxon rank-sum test (two groups) or Kruskal-Wallis test (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# Overall column and explicit missing counts MakeComparisonTable( data = Labelled, group_var = "Diagnosis", variables = vars_Compare, IncludeOverallN = TRUE, IncludeMissing = TRUE )
Comparison by Diagnosis (values: mean (SD)).
Characteristic N Control
N = 242
1
Impaired
N = 91
1
p-value2 Test2
Age 322 72.75 (13.26) 71.78 (13.12) 0.553 Welch t-test
    Unknown
10 1

Sex 333

0.0272 Pearson chi-squared
    Female
157 (65%) 47 (52%)

    Male
85 (35%) 44 (48%)

AXL receptor tyrosine kinase 333 0.28 (0.46) 0.34 (0.41) 0.238 Welch t-test
Adiponectin 333 -5.26 (0.65) -5.10 (0.69) 0.0539 Welch t-test
Tau protein 233 5.61 (0.49) 6.14 (0.55) <0.001 Welch t-test
    Unknown
68 32

Phosphorylated tau protein 333 3.94 (0.42) 4.30 (0.48) <0.001 Welch t-test
1 Mean (SD); n (%)
2 p-values use Welch t-test (two groups) or ANOVA (more than two groups) for continuous outcomes and chi-square or Fisher’s exact test, selected from expected counts for categorical outcomes.
# }