Two sample independent tests are the mostly used methods in studies. This comherensive guide includes the tests for two independent samples when the outcome is continuous. Find out how to use these tests in R.

In this guide, we will work on three tests for two independent samples in R.

  • Student’s t-test: Normality and variance homogeneity assumptions need to be satisfied.
  • Welch’s t-test: Normality needs to be satisfied, but variance homogeneity assumption is not.
  • Mann-Whitney U test: Non-parametric test. It is used when normality and variance homogeneity assumptions are not satisfied.

We will use CO2 dataset available in R. We will compare origin of the plant with respect to carbon dioxide uptake rates.

Box-and-Whisker and Density Plots

Let’s obtain descriptive statistics for each group.

library(onewaytests)
describe(uptake ~ Type, data = CO2)
##              n     Mean  Std.Dev Median Min  Max   25th  75th   Skewness Kurtosis NA
## Quebec      42 33.54286 9.673830  37.15 9.3 45.5 30.325 40.15 -1.0853883 3.051340  0
## Mississippi 42 20.88333 7.815773  19.30 7.7 35.5 13.875 28.05  0.1783301 1.767652  0

Check Out: Variance Homogeneity Tests in R

1. Student’s t-Test

If normality in each group is satisfied and the variance of the two groups are equivalent (homoscedasticity), Student’s t-test is conducted. It compares the means of two independent groups. We use st.test() function available onewaytests package (Dag et al., 2018).

library(onewaytests)
st.test(uptake ~ Type, data = CO2)
## 
##   Student's t-Test (alpha = 0.05) 
## ------------------------------------------------------------- 
##   Groups : Quebec vs. Mississippi 
## 
##   statistic  : 6.596901 
##   parameter  : 82 
##   p.value    : 3.834686e-09 
## 
##   Result     : Difference is statistically significant. 
## -------------------------------------------------------------

According to Student’s t-test, there is enough evidence to reject null hypothesis (Ho: Means are equal) since p-value (3.834686e-09) is lower than alpha (0.05). Therefore, the means of groups are not equal.

Also Check: How to Assess Normality in R

2. Welch’s t-Test

If normality in each group is satisfied and the variance of the two groups are not equivalent (heteroscedasticity), Welch’s t-test is conducted. It tests the hypothesis that two populations have equal means. We use wt.test() function available onewaytests package (Dag et al., 2018).

library(onewaytests)
wt.test(uptake ~ Type, data = CO2)
## 
##   Welch's t-Test (alpha = 0.05) 
## ------------------------------------------------------------- 
##   Groups : Quebec vs. Mississippi 
## 
##   statistic  : 6.596901 
##   parameter  : 78.53324 
##   p.value    : 4.450881e-09 
## 
##   Result     : Difference is statistically significant. 
## -------------------------------------------------------------

According to Welch’s t-test, there is enough evidence to reject null hypothesis (Ho: Means are equal) since p-value (4.450881e-09) is lower than alpha (0.05). Therefore, the means of groups are not equal.

Also Check: Shapiro-Wilk Test for Univariate and Multivariate Normality in R

3. Mann-Whitney U Test

Mann-Whitney U test is non-parametric test to compare two independent groups. Normality and variance homogeneity assumptions are not necessary to be satisfied. It compares whether the distribution of the dependent variable is same for the two groups. We use mw.test() function available onewaytests package (Dag et al., 2018).

library(onewaytests)
mw.test(uptake ~ Type, data = CO2)
## 
##   Mann-Whitney U Test (alpha = 0.05) 
## ------------------------------------------------------------- 
##   Groups : Quebec vs. Mississippi 
## 
##   statistic  : 5.430589 
##   p.value    : 5.616832e-08 
## 
##   Result     : Difference is statistically significant. 
## -------------------------------------------------------------

According to Mann-Whitney U test, there is enough evidence to reject null hypothesis (Ho: The distribution of the dependent variable is same for the two groups) since p-value (5.616832e-08) is lower than alpha (0.05). Therefore, the distribution of the dependent variable is different for the two groups.

The application of the codes is available in our youtube channel below.

Two Sample Independent Tests in R
Subscribe to YouTube Channel

Don’t forget to check: How to Categorize Numeric Variables in R

References

Dag, O., Dolgun, A., Konar, N.M. (2018). onewaytests: An R Package for One-Way Tests in Independent Groups Designs. R Journal, 10(1), 175-199.


Dr. Osman Dag