Learn R Programming

lsr (version 1.0.0)

independentSamplesTTest: Independent samples t-test

Description

Runs an independent-samples t-test and prints the results in a readable format.

Usage

independentSamplesTTest(
  formula,
  data = NULL,
  var.equal = FALSE,
  one.sided = FALSE,
  conf.level = 0.95
)

Value

Prints a summary showing the outcome and grouping variable names, group means and standard deviations, null and alternative hypotheses, test results (t-statistic, degrees of freedom, p-value), a confidence interval, and Cohen's d as a measure of effect size. The underlying results are also returned as a list, so the output can be assigned to a variable and inspected if needed.

Arguments

formula

A formula of the form outcome ~ group, where outcome is the numeric variable being measured and group is a factor with exactly two levels.

data

An optional data frame containing the variables named in formula. Tibbles are accepted and converted automatically. If data is omitted the variables are looked up in the workspace.

var.equal

Set to TRUE to run Student's t-test, which assumes equal group variances. The default (FALSE) runs Welch's t-test, which is safer when variances may differ between groups.

one.sided

Set to FALSE (default) for a two-sided test. Set to the name of the group expected to have the larger mean for a one-sided test (e.g., one.sided = "group2").

conf.level

The confidence level for the confidence interval. The default is 0.95 for a 95% interval.

Details

Runs an independent-samples t-test comparing the means of two groups, and prints the results in a beginner-friendly format. The calculations are done by t.test and cohensD. When var.equal = TRUE, Cohen's d uses a pooled standard deviation; when var.equal = FALSE (Welch's test), it uses the "unequal" method. Cases with missing values are removed with a warning.

See Also

t.test, oneSampleTTest, pairedSamplesTTest, cohensD

Examples

Run this code
df <- data.frame(
  rt = c(451, 562, 704, 324, 505, 600, 829),
  cond = factor(x = c(1, 1, 1, 2, 2, 2, 2), labels = c("group1", "group2"))
)

# Welch's t-test (the default, does not assume equal variances)
independentSamplesTTest(rt ~ cond, df)

# Student's t-test (assumes equal variances)
independentSamplesTTest(rt ~ cond, df, var.equal = TRUE)

# one-sided test: is group1 larger?
independentSamplesTTest(rt ~ cond, df, one.sided = "group1")

# missing values are removed with a warning
df$rt[1] <- NA
df$cond[7] <- NA
independentSamplesTTest(rt ~ cond, df)

Run the code above in your browser using DataLab