Skip to Main Content

MASH : Maths and Stats Help

Independent Samples t-Test

Introduction

An independent-samples t-test compares the means between two unrelated groups, such as comparing the difference between class 1 and class 2, another very common example would be used to compare a binary gender difference.

If you have more than three groups consider using a One-Way Between-Subjects ANOVA

It is considered a parametric test and is only suitable for parametric data. To check if your data is parametric, please read our dedicated guide: Parametric or Not Guide (PDF)

If your data is non-parametric you should consider using a Mann-Whitney U-Test.

 

Types of independent-samples t-tests

Based on whether the two independent groups/samples have similar or different variances, you can run two types of t-tests in R. In order to learn how to check the equality of variances of two groups, please see our brief guide on Levene's test of equality of variances.

 

Independent-samples t-test with unequal variances

Formula method

This is used for when the data is structured using a grouping variable. A grouping variable is a categorical variable indicating which scores belong to different groups.

Data: 

The first argument entered in the t.test() function is a formula that takes the following structure: dependent variable ~ independent variable. In the case of t-tests, the independent variable should be a grouping variable with two categories (levels).

The second argument is the data frame.

By default, the t-test function is assuming that variances are unequal. It has an option called var.equal that is equal to FALSE by default. For that reason, no additional arguments are required here.

t.test(English_Score ~ Class, englishdata1)
## 
##  Welch Two Sample t-test
## 
## data:  English_Score by Class
## t = 0.89443, df = 12.303, p-value = 0.3883
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
##  -1.429355  3.429355
## sample estimates:
## mean in group 1 mean in group 2 
##               6               5    

 

Variables method

This method is used when your data is structured into two separate variables. Both of your variables should be numeric.

Data:

Put both of your variables in the t.test() function in any order. If you have your variables stored in a data frame, use the following structure to indicate the variable: data_frame$variable_name

t.test(englishdata2$group1, englishdata2$group2)
## 
##  Welch Two Sample t-test
## 
## data:  englishdata2$group1 and englishdata2$group2
## t = 0.89443, df = 12.303, p-value = 0.3883
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.429355  3.429355
## sample estimates:
## mean of x mean of y 
##         6         5

 

 

Independent-samples t-test with equal variances

Formula method

Same as with the previously run t-test, the first argument entered in the t.test() function is a formula that takes the following structure: dependent variable ~ independent variable. In the case of t-tests, the independent variable should be a grouping variable with two categories (levels).

The second argument is the data frame.

The third argument is the option to run the t-test with equal variances assumed (var.equal = TRUE). The argument is FALSE by default, so we need to change it to TRUE.

t.test(English_Score ~ Class, englishdata1, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  English_Score by Class
## t = 0.89443, df = 14, p-value = 0.3862
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
##  -1.397944  3.397944
## sample estimates:
## mean in group 1 mean in group 2 
##               6               5

 

Variables method

Similar to the previous example, this method is used when your data is structured into two separate variables. Both of your variables should be numeric.

For the first two arguments, put both of your variables in the t.test() function in any order. If you have your variables stored in a data frame, use the following structure to indicate the variable: data_frame$variable_name

The third argument is the option to run the t-test with equal variances assumed (var.equal = TRUE).

t.test(englishdata2$group1, englishdata2$group2, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  englishdata2$group1 and englishdata2$group2
## t = 0.89443, df = 14, p-value = 0.3862
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.397944  3.397944
## sample estimates:
## mean of x mean of y 
##         6         5

 

 

Results

R will generate largely the same output for both formula and variables methods.

 

Descriptive Statistics by group

The results show the means for the two groups, but no standard deviations. As standard deviations are commonly reported, please check our guide on descriptive statistics.

Independent Samples Test

This table shows the specific test results including the t-statistic (t), the degrees of freedom (df) the two-tailed significance or p-value (Two-Sided p), and the 95% Confidence Interval (95% Confidence Interval of the Difference).

Differences between equal variances and unequal variances (Welch) t-test

The only difference between the two tests is that for the Welch test, the degrees of freedom (df) are adjusted by a formula, making them smaller. This affects the p-value, making it less likel to be significant. They are reported in the exact same way.

 

Reporting the Results in APA Formatting

English scores of class 1 and class 2 students were compared. On average, class 1 students (= 6.00, SD = 1.77) scored higher than class 2 students (M = 5.00, SD = 2.62). An independent-samples t-test indicated that this difference, 1.00, 95%CI [-1.40, 3.40], was not statistically significant, (14) = 0.89, =.386