Skip to contents

A simple interface to lower-level statistics functions, including stats::wilcox.test(), stats::kruskal.test(), emmeans::emmeans(), and emmeans::emtrends().

Usage

stats_table(
  df,
  regr = NULL,
  resp = attr(df, "response"),
  stat.by = NULL,
  split.by = NULL,
  test = "emmeans",
  fit = "lm",
  at = NULL,
  level = 0.95,
  alt = "!=",
  mu = 0,
  p.adj = "fdr"
)

Arguments

df

The dataset (data.frame or tibble object). "Dataset fields" mentioned below should match column names in df. Required.

regr

Dataset field with the x-axis (independent; predictive) values. Must be numeric. Default: NULL

resp

Dataset field with the y-axis (dependent; response) values, such as taxa abundance or alpha diversity. Default: attr(df, 'response')

stat.by

Dataset field with the statistical groups. Must be categorical. Default: NULL

split.by

Dataset field(s) that the data should be split by prior to any calculations. Must be categorical. Default: NULL

test

Method for computing p-values: 'wilcox', 'kruskal', 'emmeans', or 'emtrends'. Default: 'emmeans'

fit

How to fit the trendline. 'lm', 'log', or 'gam'. Default: 'lm'

at

Position(s) along the x-axis where the means or slopes should be evaluated. Default: NULL, which samples 100 evenly spaced positions and selects the position where the p-value is most significant.

level

The confidence level for calculating a confidence interval. Default: 0.95

alt

Alternative hypothesis direction. Options are '!=' (two-sided; not equal to mu), '<' (less than mu), or '>' (greater than mu). Default: '!='

mu

Reference value to test against. Default: 0

p.adj

Method to use for multiple comparisons adjustment of p-values. Run p.adjust.methods for a list of available options. Default: "fdr"

Value

A tibble data.frame with fields from the table below. This tibble object provides the $code operator to print the R code used to generate the statistics.

FieldDescription
.meanEstimated marginal mean. See emmeans::emmeans().
.mean.diffDifference in means.
.slopeTrendline slope. See emmeans::emtrends().
.slope.diffDifference in slopes.
.h1Alternate hypothesis.
.p.valProbability that null hypothesis is correct.
.adj.p.p.val after adjusting for multiple comparisons.
.effect.sizeEffect size. See emmeans::eff_size().
.lowerConfidence interval lower bound.
.upperConfidence interval upper bound.
.seStandard error.
.nNumber of samples.
.dfDegrees of freedom.
.statWilcoxon or Kruskal-Wallis rank sum statistic.
.t.ratio.mean / .se
.r.sqrPercent of variation explained by the model.
.adj.r.r.sqr, taking degrees of freedom into account.
.aicAkaike Information Criterion (predictive models).
.bicBayesian Information Criterion (descriptive models).
.loglikLog-likelihood goodness-of-fit score.
.fit.pP-value for observing this fit by chance.

See also

Other stats_tables: adiv_stats(), bdiv_stats(), distmat_stats(), taxa_stats()

Examples

    library(rbiom)
    
    biom <- rarefy(hmp50)
    
    df <- taxa_table(biom, rank = "Family")
    stats_table(df, stat.by = "Body Site")[,1:6]
#> # Model:    lm(.abundance ~ `Body Site`)
#> # A tibble: 10 × 6
#>    `Body Site`                    .mean.diff .h1   .p.val .adj.p .effect.size
#>    <chr>                               <dbl> <fct>  <dbl>  <dbl>        <dbl>
#>  1 Mid vagina - Saliva                 83.4  != 0   0.140  0.489       0.271 
#>  2 Anterior nares - Mid vagina        -85.6  != 0   0.162  0.489      -0.257 
#>  3 Buccal mucosa - Saliva              59.0  != 0   0.170  0.489       0.252 
#>  4 Anterior nares - Buccal mucosa     -61.2  != 0   0.212  0.489      -0.229 
#>  5 Mid vagina - Stool                  75.3  != 0   0.245  0.489       0.219 
#>  6 Buccal mucosa - Stool               50.9  != 0   0.327  0.545       0.185 
#>  7 Buccal mucosa - Mid vagina         -24.4  != 0   0.717  0.905      -0.0664
#>  8 Anterior nares - Stool             -10.3  != 0   0.809  0.905      -0.0455
#>  9 Saliva - Stool                      -8.09 != 0   0.814  0.905      -0.0442
#> 10 Anterior nares - Saliva             -2.18 != 0   0.946  0.946      -0.0124
    
    df <- adiv_table(biom)
    stats_table(df, stat.by = "Sex", split.by = "Body Site")[,1:7]
#> # Model:    lm(.diversity ~ Sex)
#> # A tibble: 5 × 7
#>   `Body Site`    Sex           .mean.diff .h1   .p.val .adj.p .effect.size
#>   <fct>          <chr>              <dbl> <fct>  <dbl>  <dbl>        <dbl>
#> 1 Saliva         Male - Female     0.236  != 0   0.226  0.333        0.830
#> 2 Stool          Male - Female     0.243  != 0   0.228  0.333        0.886
#> 3 Buccal mucosa  Male - Female     0.541  != 0   0.250  0.333        0.784
#> 4 Anterior nares Male - Female     0.0440 != 0   0.831  0.831        0.140
#> 5 Mid vagina     NA               NA      NA    NA     NA           NA