This function wraps stats::t.test() to provide a consistent interface for independent-samples, paired-samples, and one-sample t-tests. It always calculates Cohen's d as a measure of effect size and returns results in a tidy format.

t_test(
  x,
  y = NULL,
  data = NULL,
  paired = FALSE,
  mu = 0,
  var.equal = FALSE,
  conf.level = 0.95,
  alternative = c("two.sided", "less", "greater"),
  ...
)

Arguments

x

A numeric vector of data values, or a formula of the form outcome ~ group for independent samples tests.

y

An optional numeric vector of data values. For independent samples, this should be NULL if x is a formula. For paired samples, this should be a vector of the same length as x.

data

An optional data frame containing the variables in the formula (for independent samples tests only).

paired

Logical indicating whether to perform a paired samples t-test. Default is FALSE.

mu

A number indicating the true value of the mean (for one-sample tests) or difference in means (for paired and independent samples tests). Default is 0.

var.equal

Logical indicating whether to treat the two variances as equal for independent samples tests. Default is FALSE (Welch's t-test).

conf.level

Confidence level for the confidence interval. Default is 0.95.

alternative

A character string specifying the alternative hypothesis: "two.sided" (default), "less", or "greater".

...

Additional arguments passed to stats::t.test().

Value

An object of class "timesaveR_t_test" containing:

test_type

Type of t-test performed

statistic

The t-statistic

parameter

Degrees of freedom

p.value

The p-value

estimate

The estimated mean or mean difference

conf.low

Lower bound of confidence interval

conf.high

Upper bound of confidence interval

cohens_d

Cohen's d effect size

alternative

Alternative hypothesis

method

Description of the test method

data.name

Description of the data

htest

The original htest object from t.test()

Details

Cohen's d is calculated as follows:

  • For independent samples: (mean1 - mean2) / pooled_sd (or using separate SDs if var.equal = FALSE)

  • For paired samples: mean_diff / sd_diff

  • For one sample: (mean - mu) / sd

Examples

# One-sample t-test
t_test(mtcars$mpg, mu = 20)
#> 
#> One Sample t-test 
#> 
#> data:  mtcars$mpg 
#> t = 0.085, df = 31, p-value = .933
#> alternative hypothesis: true mean is not equal to 20 
#> 95% confidence interval:
#>  17.918 22.264
#> sample estimate:
#>  mean of x
#>  20.091
#> 
#> Cohen's d: 0.015
#> 

# Independent samples t-test (formula interface)
t_test(mpg ~ am, data = mtcars)
#> 
#> Welch Two Sample t-test 
#> 
#> data:  mpg by am 
#> t = -3.767, df = 18.33, p-value = .001
#> alternative hypothesis: true mean is not equal to 0 
#> 95% confidence interval:
#>  -11.280 -3.210
#> sample estimates:
#>  mean of x    mean of y
#>  17.147    24.392
#> 
#> Cohen's d: -1.411
#> 

# Independent samples t-test (vector interface)
t_test(mtcars$mpg[mtcars$am == 0], mtcars$mpg[mtcars$am == 1])
#> 
#> Welch Two Sample t-test 
#> 
#> data:  mtcars$mpg[mtcars$am == 0] and mtcars$mpg[mtcars$am == 1] 
#> t = -3.767, df = 18.33, p-value = .001
#> alternative hypothesis: true mean is not equal to 0 
#> 95% confidence interval:
#>  -11.280 -3.210
#> sample estimates:
#>  mean of x    mean of y
#>  17.147    24.392
#> 
#> Cohen's d: -1.411
#> 

# Paired samples t-test
t_test(iris$Sepal.Width, iris$Petal.Length, paired = TRUE)
#> 
#> Paired t-test 
#> 
#> data:  iris$Sepal.Width and iris$Petal.Length 
#> t = -4.309, df = 149, p-value < .001
#> alternative hypothesis: true mean is not equal to 0 
#> 95% confidence interval:
#>  -1.022 -0.379
#> sample estimate:
#>  mean difference
#>  -0.701
#> 
#> Cohen's d: -0.352
#>