Function Repository Resource:

WelchTest

Source Notebook

Compare the means of two observational samples using the Welch test

Contributed by: András Aszódi

ResourceFunction["WelchTest"][data1,data2]

tests whether the two lists of observations data1 and data2 have equal means.

Details

The Welch test is a modification of the Student t-test that does not require equality of variances.
The Welch test is used to compare the means of two groups of observations (samples) if the samples were drawn from (approximately) normal populations with unequal variances.
ResourceFunction["WelchTest"] returns an association containing the following keys:
"PValue"p-values
"TestStatistic"value of the t-statistic
“DegreesOfFreedom"approximate (non-integer) degrees of freedom
ResourceFunction["WelchTest"] takes the following option:
AlternativeHypothesis"Unequal"the inequality for the alternative hypothesis
With the setting AlternativeHypothesis"Unequal", ResourceFunction["WelchTest"] performs a two-sided Welch test.
Use AlternativeHypothesis"Greater" or AlternativeHypothesis"Less" to perform a one-sided Welch test.

Examples

Basic Examples (2) 

Two samples with unequal length:

In[1]:=
x1 = {2.970794, 3.639524, 2.475263, 3.627425, 2.916037, 3.064092, 3.487566, 2.857041, 3.478470, 2.301809, 2.610873};
x2 = {5.6249932, 2.2246327, 3.6890269, 4.7565668, 4.1545929, 1.9943043, 2.6960969, 3.3570305, 3.6784494, -0.1559634, 2.4236762, 3.9508845, 1.6016226, 0.3065061, 3.2132993};

Perform the two-sided Welch test on the two samples:

In[2]:=
ResourceFunction["WelchTest"][x1, x2]
Out[2]=

Options (3) 

AlternativeHypothesis (3) 

Two samples with unequal length:

In[3]:=
x1 = {2.970794, 3.639524, 2.475263, 3.627425, 2.916037, 3.064092, 3.487566, 2.857041, 3.478470, 2.301809, 2.610873};
x2 = {5.6249932, 2.2246327, 3.6890269, 4.7565668, 4.1545929, 1.9943043, 2.6960969, 3.3570305, 3.6784494, -0.1559634, 2.4236762, 3.9508845, 1.6016226, 0.3065061, 3.2132993};

Test H0:μ1-μ20 versus Ha:μ1-μ2<0:

In[4]:=
ResourceFunction["WelchTest"][x1, x2, AlternativeHypothesis -> "Less"]
Out[4]=

Test H0:μ1-μ2<0 versus Ha:μ1-μ20:

In[5]:=
ResourceFunction["WelchTest"][x1, x2, AlternativeHypothesis -> "Greater"]
Out[5]=

Publisher

Andras Aszodi

Version History

  • 1.0.0 – 08 April 2022

Source Metadata

Author Notes

This very simple implementation does not check the t-test prerequisite that the samples must come from (approximately) Normal distributions. In particular, the VerifyTestAssumptions option of the TTest function is not implemented.

License Information