bundles / scipy 1.18.0 / scipy / stats / _hypotests / tukey_hsd
function
scipy.stats._hypotests:tukey_hsd
source: /scipy/stats/_hypotests.py:1990
Signature
def tukey_hsd(*samples, equal_var = True)Summary
Perform Tukey's HSD test for equality of means over multiple treatments.
Extended Summary
Tukey's honestly significant difference (HSD) test performs pairwise comparison of means for a set of samples. Whereas ANOVA (e.g. f_oneway) assesses whether the true means underlying each sample are identical, Tukey's HSD is a post hoc test used to compare the mean of each sample to the mean of each other sample.
The null hypothesis is that the distributions underlying the samples all have the same mean. The test statistic, which is computed for every possible pairing of samples, is simply the difference between the sample means. For each pair, the p-value is the probability under the null hypothesis (and other assumptions; see notes) of observing such an extreme value of the statistic, considering that many pairwise comparisons are being performed. Confidence intervals for the difference between each pair of means are also available.
Parameters
*samples: array_likeThe sample measurements for each group. There must be at least two arguments.
equal_var: bool, optionalIf True (default) and equal sample size, perform Tukey-HSD test [6]. If True and unequal sample size, perform Tukey-Kramer test [4]. If False, perform Games-Howell test [7], which does not assume equal variances.
Returns
result: `~scipy.stats._result_classes.TukeyHSDResult` instanceThe return value is an object with the following attributes:
statistic
The computed statistic of the test for each comparison. The element at index
(i, j)is the statistic for the comparison between groupsiandj.pvalue
The computed p-value of the test for each comparison. The element at index
(i, j)is the p-value for the comparison between groupsiandj.
The object has the following methods:
confidence_interval(confidence_level=0.95):
Compute the confidence interval for the specified confidence level.
Notes
The use of this test relies on several assumptions.
The observations are independent within and among groups.
The observations within each group are normally distributed.
The distributions from which the samples are drawn have the same finite variance.
The original formulation of the test was for samples of equal size drawn from populations assumed to have equal variances [6]. In case of unequal sample sizes, the test uses the Tukey-Kramer method [4]. When equal variances are not assumed (equal_var=False), the test uses the Games-Howell method [7].
Examples
Here are some data comparing the time to relief of three brands of headache medicine, reported in minutes. Data adapted from [3]_.import numpy as np from scipy.stats import tukey_hsd group0 = [24.5, 23.5, 26.4, 27.1, 29.9] group1 = [28.4, 34.2, 29.5, 32.2, 30.1] group2 = [26.1, 28.3, 24.3, 26.2, 27.8]✓
import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1)✓
ax.boxplot([group0, group1, group2])
✗{'whiskers': [<matplotlib.lines.Line2D object at 0x7fa8e7e04980>, <matplotlib.lines.Line2D object at 0x7fa8e7e046e0>, <matplotlib.lines.Line2D object at 0x7fa8e7e06a50>, <matplotlib.lines.Line2D object at 0x7fa8e7e06270>, <matplotlib.lines.Line2D object at 0x7fa8e7e07770>, <matplotlib.lines.Line2D object at 0x7fa8e7e07e00>], 'caps': [<matplotlib.lines.Line2D object at 0x7fa8e7e04440>, <matplotlib.lines.Line2D object at 0x7fa8e7e070e0>, <matplotlib.lines.Line2D object at 0x7fa8e7e042f0>, <matplotlib.lines.Line2D object at 0x7fa8e7e07380>, <matplotlib.lines.Line2D object at 0x7fa8e7e063c0>, <matplotlib.lines.Line2D object at 0x7fa8e7e041a0>], 'boxes': [<matplotlib.lines.Line2D object at 0x7fa8e7e06510>, <matplotlib.lines.Line2D object at 0x7fa8e7e07b60>, <matplotlib.lines.Line2D object at 0x7fa8e7e052b0>], 'medians': [<matplotlib.lines.Line2D object at 0x7fa8e7e05940>, <matplotlib.lines.Line2D object at 0x7fa8e7e06660>, <matplotlib.lines.Line2D object at 0x7fa8e7e05160>], 'fliers': [<matplotlib.lines.Line2D object at 0x7fa8e7e056a0>, <matplotlib.lines.Line2D object at 0x7fa8e7e04ec0>, <matplotlib.lines.Line2D object at 0x7fa8e7e057f0>], 'means': []}
plt.show()
✓
res = tukey_hsd(group0, group1, group2)
✓print(res)
✗Pairwise Group Comparisons (95.0% Confidence Interval) Comparison Statistic p-value Lower CI Upper CI (0 - 1) -4.600 0.014 -8.249 -0.951 (0 - 2) -0.260 0.980 -3.909 3.389 (1 - 0) 4.600 0.014 0.951 8.249 (1 - 2) 4.340 0.020 0.691 7.989 (2 - 0) 0.260 0.980 -3.389 3.909 (2 - 1) -4.340 0.020 -7.989 -0.691The null hypothesis is that each group has the same mean. The p-value for comparisons between ``group0`` and ``group1`` as well as ``group1`` and ``group2`` do not exceed .05, so we reject the null hypothesis that they have the same means. The p-value of the comparison between ``group0`` and ``group2`` exceeds .05, so we accept the null hypothesis that there is not a significant difference between their means. We can also compute the confidence interval associated with our chosen confidence level.
group0 = [24.5, 23.5, 26.4, 27.1, 29.9] group1 = [28.4, 34.2, 29.5, 32.2, 30.1] group2 = [26.1, 28.3, 24.3, 26.2, 27.8] result = tukey_hsd(group0, group1, group2) conf = res.confidence_interval(confidence_level=.99) for ((i, j), l) in np.ndenumerate(conf.low): # filter out self comparisons if i != j: h = conf.high[i,j] print(f"({i} - {j}) {l:>6.3f} {h:>6.3f}")✓
(0 - 1) -9.480 0.280 (0 - 2) -5.140 4.620 (1 - 0) -0.280 9.480 (1 - 2) -0.540 9.220 (2 - 0) -4.620 5.140 (2 - 1) -9.220 0.540
See also
- dunnett
performs comparison of means against a control group.
Aliases
scipy.stats.tukey_hsd