apa_htest: Format hypothesis tests in APA style

View source: R/htest.R

apa_htestR Documentation

Format hypothesis tests in APA style

Description

Runs t tests, Wilcoxon tests, correlation tests, or chi-square tests of independence on the variables in a data frame and makes a table with one row per outcome variable: descriptive statistics, the test statistic, its degrees of freedom, the p-value, and an effect size with its confidence interval. Tests come from the stats package (e.g., stats::t.test()), and effect sizes are computed from the data with the effectsize package (e.g., effectsize::cohens_d()).

Usage

apa_htest(
  data,
  formula,
  test = c("t", "wilcoxon", "cor", "chisq"),
  effect_size = NULL,
  ci = 0.95,
  var_equal = FALSE,
  mu = 0,
  method = c("pearson", "spearman", "kendall"),
  descriptives = TRUE,
  pair_labels = NULL,
  starred = NA,
  bolded = NA,
  column_formats = NULL,
  ...
)

Arguments

data

data frame, or test results already computed: an htest object from stats::t.test(), stats::wilcox.test(), stats::cor.test(), or stats::chisq.test(), or a list of such objects of the same kind (named lists label the rows). See Test results.

formula

two-sided formula with outcome variables on the left and a grouping or predictor variable on the right. Several outcomes are joined with + (e.g., mpg + hp ~ am), and each gets its own row. For one-sample t tests and Wilcoxon signed-rank tests, use 1 on the right (e.g., mpg ~ 1). For paired tests, put each pair of variables in Pair() with 1 on the right (e.g., Pair(pre, post) ~ 1, or Pair(anx1, anx2) + Pair(dep1, dep2) ~ 1 for several pairs).

test

type of test:

  • "t": t test comparing the means of two groups (Welch's test unless var_equal = TRUE), a paired t test, or a one-sample t test comparing a mean with mu

  • "wilcoxon": Wilcoxon rank-sum test comparing two groups, or a Wilcoxon signed-rank test of paired variables or of a variable compared with mu

  • "cor": correlation test of each outcome with the variable on the right

  • "chisq": chi-square test of independence of each outcome with the variable on the right

effect_size

effect size to display. If NULL (default), the effect size is Cohen's d ("cohens_d") for t tests, the rank-biserial correlation ("rank_biserial") for Wilcoxon tests, and Cramér's V ("cramers_v") for chi-square tests. t tests can also show Hedges' g ("hedges_g"), and chi-square tests can show \phi ("phi", for 2 \times 2 tables) or Cohen's w ("cohens_w"). Use "none" for no effect size. Correlation tests show the correlation itself.

For paired t tests, Cohen's d is d~av~ ("d_av"), which divides the mean difference by the average of the two variables' standard deviations, so it is comparable to d for two independent groups. Other choices are d~z~ ("d_z", divided by the standard deviation of the differences) and d~rm~ ("d_rm", d~z~ corrected for the correlation between the variables), and Hedges' g versions with a small-sample correction ("g_av", "g_z", "g_rm"; "hedges_g" is "g_av"). These come from effectsize::repeated_measures_d().

ci

confidence level of the effect size interval (for correlation tests, the interval of Pearson's r). Intervals are two-sided.

var_equal

if TRUE, two-group t tests assume equal variances (Student's t test), and Cohen's d uses the pooled standard deviation. If FALSE (default), Welch's t test is used, and Cohen's d uses the average of the two groups' variances.

mu

value that one-sample tests compare each variable with (for two-group tests, the hypothesized difference in location)

method

correlation coefficient: "pearson" (default), "spearman", or "kendall"

descriptives

if TRUE (default), the table shows each group's mean and standard deviation (t tests) or median (Wilcoxon tests), or each variable's sample size (correlation and chi-square tests).

pair_labels

for paired tests, labels of the first and second variable of each pair, used as the headings above their descriptive statistics (e.g., c("Pretest", "Posttest")). If NULL (default), the headings are the variable names when there is one pair, and "First" and "Second" otherwise.

starred

columns to star with significant p-values (e.g., "t")

bolded

columns to bold, if significant

column_formats

column_formats object to format columns. If NULL, the default column_formats is used.

...

further arguments passed to the test function (e.g., alternative = "greater", or correct = FALSE for stats::chisq.test())

Details

Each test uses the rows in which both the outcome and the grouping (or predictor) variable are observed. For two-group tests, the grouping variable must have exactly two observed values, and differences are the first group (in factor level order) minus the second. Paired tests use the rows in which both variables of the pair are observed, and differences are the first variable minus the second.

Effect sizes are computed from the data rather than from the test statistic, so Cohen's d for Welch's test is exact rather than approximated from t.

Use apa_note() to write a table note that says which tests were done, how the effect size was computed, and what the abbreviations stand for.

Value

apa7_tbl (tibble)

Test results

htest objects keep the test statistic, its degrees of freedom, and the p-value, but not the data, so formula and the arguments that control the tests are not used, and effect sizes are computed from what the objects keep:

  • One-sample and paired t tests: Cohen's d (d~z~ for paired tests) is exactly t/\sqrt{n}, with an exact interval from the noncentral t distribution.

  • Two-sample t tests: Cohen's d is approximated as 2t/\sqrt{df} (effectsize::t_to_d()), which assumes groups of equal size, and the note says so. For exact values, give apa_htest() the data instead.

  • Correlation tests: the correlation and, for Pearson's r, its interval and the sample size.

  • Chi-square tests of independence: Cramér's V, \phi, or Cohen's w, computed from the observed table that the object keeps.

  • Wilcoxon tests and chi-square goodness-of-fit tests: no effect size, because the objects do not keep the sample sizes.

The rows are labeled by the list's names or, if unnamed, by the objects' data names (e.g., "mpg by am"). The table has no descriptive statistics.

References

Lakens, D. (2013). Calculating and reporting effect sizes to facilitate cumulative science: A practical primer for t-tests and ANOVAs. Frontiers in Psychology, 4, Article 863. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3389/fpsyg.2013.00863")}

Examples

# Welch's t tests with Cohen's d
d <- mtcars
d$am <- factor(d$am, labels = c("Automatic", "Manual"))
apa_htest(d, mpg + hp + qsec ~ am)

# Student's t test with Hedges' g
apa_htest(d, mpg ~ am, var_equal = TRUE, effect_size = "hedges_g")

# One-sample t test
apa_htest(d, mpg ~ 1, mu = 20)

# Paired t test with d_av
d_sleep <- data.frame(drug1 = sleep$extra[1:10], drug2 = sleep$extra[11:20])
apa_htest(d_sleep, Pair(drug1, drug2) ~ 1)

# Wilcoxon rank-sum tests with rank-biserial correlations
apa_htest(d, mpg + hp ~ am, test = "wilcoxon", exact = FALSE)

# Correlation tests
apa_htest(d, mpg + hp + qsec ~ wt, test = "cor")

# Chi-square tests of independence with Cramer's V
d$cyl <- factor(d$cyl)
d$vs <- factor(d$vs, labels = c("V-shaped", "Straight"))
tbl <- apa_htest(d, cyl + vs ~ am, test = "chisq")
tbl
apa_note(tbl)

# Test results already computed
apa_htest(t.test(mpg ~ am, data = d))
apa_htest(list(
  Weight = cor.test(d$mpg, d$wt),
  Displacement = cor.test(d$mpg, d$disp)
))

apa7 documentation built on Dec. 1, 2026, 5:06 p.m.