Skip to contents

This function performs frequentist and Bayesian t-tests, with options for effect size estimation, confidence intervals, Bayesian credible intervals, probability of direction (pd), and Bayes factors.

Usage

t_test_all(
  x,
  y = NULL,
  var.label = c("x", "y"),
  paired = F,
  var.equal = FALSE,
  mu = 0,
  ci = c("freq", "bayes_central", "bayes_hdi"),
  alternative = c("two.sided", "less", "greater"),
  conf.level = 0.95,
  pd = FALSE,
  bf = FALSE,
  cor = TRUE,
  mean_x_EAP = FALSE,
  mean_x_MAP = FALSE,
  mean_x_MED = FALSE,
  diff_EAP = FALSE,
  diff_MAP = FALSE,
  diff_MED = FALSE,
  cohens_d = NULL,
  cohens_d_EAP = FALSE,
  cohens_d_MAP = FALSE,
  cohens_d_MED = FALSE,
  cohens_dz = TRUE,
  cohens_dz_EAP = FALSE,
  cohens_dz_MAP = FALSE,
  cohens_dz_MED = FALSE,
  rscale_est = Inf,
  rscale_bf = "medium",
  iterations = 10000,
  map_density_n = 512,
  verbose = TRUE
)

Arguments

x

A numeric vector representing the first sample.

y

An optional numeric vector representing the second sample. If NULL, a one-sample t-test is performed (default: NULL).

var.label

A character vector of length two specifying labels for x and y.

paired

Logical. If TRUE, a paired t-test is performed (default: FALSE).

var.equal

Logical. If TRUE, the two-sample t-test assumes equal variances (default: FALSE).

mu

A numeric value specifying the null hypothesis mean difference.

ci

Character. Specifies the type of confidence or credible interval: "freq" (frequentist confidence interval), "bayes_central" (Bayesian central credible interval), or "bayes_hdi" (highest density interval based on the posterior distribution) (default: "freq").

alternative

Character. Specifies the alternative hypothesis for the frequentist test: "two.sided", "less", or "greater" (default: "two.sided").

conf.level

Numeric. The confidence level for frequentist intervals or credibility level for Bayesian intervals (default: 0.95).

pd

Logical. If TRUE, computes the probability of direction (pd) based on posterior distributions (default: FALSE).

bf

Logical. If TRUE, computes Bayes factors for the presence of a difference versus the null hypothesis (default: FALSE).

cor

Logical. If TRUE, computes Pearson correlation for paired samples.

mean_x_EAP

Logical. If TRUE, computes the expected a posteriori (EAP) estimate of the mean of x (default: FALSE).

mean_x_MAP

Logical. If TRUE, computes the maximum a posteriori (MAP) estimate of the mean of x (default: FALSE).

mean_x_MED

Logical. If TRUE, computes the median of the posterior distribution (MED) for the mean of x (default: FALSE).

diff_EAP

Logical. If TRUE, computes the expected a posteriori (EAP) estimate of the mean difference (default: FALSE).

diff_MAP

Logical. If TRUE, computes the maximum a posteriori (MAP) estimate of the mean difference (default: FALSE).

diff_MED

Logical. If TRUE, computes the median of the posterior distribution (MED) for the mean difference (default: FALSE).

cohens_d

Logical. If TRUE, computes Cohen's d for independent samples.

cohens_d_EAP

Logical. If TRUE, computes the expected a posteriori (EAP) estimate of Cohen's d (default: FALSE).

cohens_d_MAP

Logical. If TRUE, computes the maximum a posteriori (MAP) estimate of Cohen's d (default: FALSE).

cohens_d_MED

Logical. If TRUE, computes the median of the posterior distribution (MED) for Cohen's d (default: FALSE).

cohens_dz

Logical. If TRUE, computes Cohen's dz for paired samples.

cohens_dz_EAP

Logical. If TRUE, computes the expected a posteriori (EAP) estimate of Cohen's dz (default: FALSE).

cohens_dz_MAP

Logical. If TRUE, computes the maximum a posteriori (MAP) estimate of Cohen's dz (default: FALSE).

cohens_dz_MED

Logical. If TRUE, computes the median of the posterior distribution (MED) for Cohen's dz (default: FALSE).

rscale_est

Numeric or character. Specifies the Cauchy prior scale for Bayesian estimation of the posterior distribution. Options: "ultrawide", "wide", "medium", or a positive real number (default: Inf). Passed to BayesFactor::ttestBF().

rscale_bf

Numeric or character. Specifies the Cauchy prior scale for Bayes factor calculation. Options: "ultrawide", "wide", "medium", or a positive real number (default: "medium"). Passed to BayesFactor::ttestBF().

iterations

Integer. Number of MCMC iterations for Bayesian estimation.

map_density_n

Integer. Number of bins for MAP density estimation.

verbose

Logical. If TRUE, prints additional messages.

Value

A data frame containing test statistics, effect sizes, confidence intervals, and Bayesian estimates.

Examples

set.seed(123)
x <- rnorm(30,0, 1)
y <- x + rnorm(30,0.5, 1)

# Welch's t-test with effect size estimation
t_test_all(x, y)
#> 
#> -----------------------------------------------
#> Two-sample t-test
#> (Welch's method applied in the frequentist analysis)
#> -----------------------------------------------
#> H0: mu_x - mu_y = 0, H1: mu_x - mu_y != 0
#> 
#> n_x = 30, n_y = 30
#> 
#> t = -2.416334, df = 56.0632, p = 0.01895871
#> -----------------------------------------------
#> Mean of x = -0.04710376
#> Mean of y = 0.6312346
#> -----------------------------------------------
#> Diff (x-y) = -0.6783383
#> CI = [-1.240695, -0.1159818]
#> -----------------------------------------------
#> Cohen's d = -0.62389485
#> CI = [-1.139925, -0.1027481]
#> -----------------------------------------------
#> Note:
#> The CI represents the frequentist confidence interval.
#> (Confidence level = 0.95)

# Welch's t-test with Bayesian independent t-test (Bayes factor and pd)
# and 95% highest density intervals
t_test_all(x, y, diff_MAP = TRUE, cohens_d_MAP = TRUE,
           bf = TRUE, pd = TRUE, ci = "bayes_hdi", rscale_bf = "medium")
#> 
#> -----------------------------------------------
#> Two-sample t-test
#> (Welch's method applied in the frequentist analysis)
#> -----------------------------------------------
#> H0: mu_x - mu_y = 0, H1: mu_x - mu_y != 0
#> 
#> n_x = 30, n_y = 30
#> 
#> t = -2.416334, df = 56.0632, p = 0.01895871
#> pd = 0.9902
#> BF_10 = 2.869844 (rscale = 0.7071068)
#> Note: BF is sensitive to the rscale parameter of the prior.
#> -----------------------------------------------
#> Mean of x = -0.04710376
#> Mean of y = 0.6312346
#> -----------------------------------------------
#> Diff (x-y) = -0.6783383
#> MAP of diff = -0.6916485
#> CI = [-1.22348, -0.1196791]
#> -----------------------------------------------
#> Cohen's d = -0.62389485
#> MAP of delta = -0.6500264
#> CI = [-1.1427925, -0.1035713]
#> -----------------------------------------------
#> Note:
#> The CI represents the Beyesian posterior highest density interval.
#> (Probability = 0.95)
#> Note:
#> The posterior was approximated using 10000 samples,
#> given prior rscale = Inf.

# Paired t-test with effect size estimation
t_test_all(x, y, paired = TRUE)
#> 
#> -----------------------------------------------
#> Paired t-test
#> -----------------------------------------------
#> H0: mu_x - mu_y = 0, H1: mu_x - mu_y != 0
#> 
#> N = 30 (pairs)
#> 
#> t = -4.448914, df = 29, p = 0.0001169206
#> r_(x,y) = 0.7175137
#> -----------------------------------------------
#> Mean of x = -0.04710376
#> Mean of y = 0.6312346
#> -----------------------------------------------
#> Diff (x-y) = -0.6783383
#> CI = [-0.9901802, -0.3664964]
#> -----------------------------------------------
#> Cohen's d_z = -0.8122568
#> CI = [-1.224918, -0.3995955]
#> -----------------------------------------------
#> Note:
#> The CI represents the frequentist confidence interval.
#> (Confidence level = 0.95)

# Paired t-test with Bayesian paired t-test (Bayes factor and pd)
# and 95% central credible intervals
t_test_all(x, y, paired = TRUE, diff_MAP = TRUE, cohens_dz_MAP = TRUE,
           bf = TRUE, pd = TRUE, ci = "bayes_central", rscale_bf = "medium")
#> 
#> -----------------------------------------------
#> Paired t-test
#> -----------------------------------------------
#> H0: mu_x - mu_y = 0, H1: mu_x - mu_y != 0
#> 
#> N = 30 (pairs)
#> 
#> t = -4.448914, df = 29, p = 0.0001169206
#> r_(x,y) = 0.7175137
#> pd = 0.9999
#> BF_10 = 225.371 (rscale = 0.7071068)
#> Note: BF is sensitive to the rscale parameter of the prior.
#> -----------------------------------------------
#> Mean of x = -0.04710376
#> Mean of y = 0.6312346
#> -----------------------------------------------
#> Diff (x-y) = -0.6783383
#> MAP of diff = -0.6957117
#> CI = [-0.9809708, -0.3694457]
#> -----------------------------------------------
#> Cohen's d_z = -0.8122568
#> MAP of delta_z = -0.8325525
#> CI = [-1.231449, -0.4140352]
#> -----------------------------------------------
#> Note:
#> The CI represents the Beyesian central posterior interval.
#> (Probability = 0.95)
#> Note:
#> The posterior was approximated using 10000 samples,
#> given prior rscale = Inf.