{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The use of this test relies on several assumptions."
            }
          ]
        },
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": true,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The observations are independent within and among groups."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The observations within each group are normally distributed."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The distributions from which the samples are drawn have the same finite    variance."
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The original formulation of the test was for samples of equal size drawn from populations assumed to have equal variances "
            },
            {
              "__type": "FootnoteReference",
              "__tag": 4066,
              "label": "6"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". In case of unequal sample sizes, the test uses the Tukey-Kramer method "
            },
            {
              "__type": "FootnoteReference",
              "__tag": 4066,
              "label": "4"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". When equal variances are not assumed ("
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "equal_var=False"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "), the test uses the Games-Howell method "
            },
            {
              "__type": "FootnoteReference",
              "__tag": 4066,
              "label": "7"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "result",
              "annotation": "`~scipy.stats._result_classes.TukeyHSDResult` instance",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The return value is an object with the following attributes:"
                    }
                  ]
                },
                {
                  "__type": "DefList",
                  "__tag": 4033,
                  "children": [
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "statistic"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "The computed statistic of the test for each comparison. The element     at index "
                            },
                            {
                              "__type": "InlineCode",
                              "__tag": 4051,
                              "value": "(i, j)"
                            },
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": " is the statistic for the comparison between     groups "
                            },
                            {
                              "__type": "InlineCode",
                              "__tag": 4051,
                              "value": "i"
                            },
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": " and "
                            },
                            {
                              "__type": "InlineCode",
                              "__tag": 4051,
                              "value": "j"
                            },
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "."
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "pvalue"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "The computed p-value of the test for each comparison. The element     at index "
                            },
                            {
                              "__type": "InlineCode",
                              "__tag": 4051,
                              "value": "(i, j)"
                            },
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": " is the p-value for the comparison between     groups "
                            },
                            {
                              "__type": "InlineCode",
                              "__tag": 4051,
                              "value": "i"
                            },
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": " and "
                            },
                            {
                              "__type": "InlineCode",
                              "__tag": 4051,
                              "value": "j"
                            },
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "."
                            }
                          ]
                        }
                      ]
                    }
                  ]
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The object has the following methods:"
                    }
                  ]
                },
                {
                  "__type": "DefList",
                  "__tag": 4033,
                  "children": [
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "confidence_interval(confidence_level=0.95):"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "Compute the confidence interval for the specified confidence level."
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Perform Tukey's HSD test for equality of means over multiple treatments."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "*samples",
              "annotation": "array_like",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The sample measurements for each group. There must be at least two arguments."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "equal_var",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If True (default) and equal sample size, perform Tukey-HSD test [6]. If True and unequal sample size, perform Tukey-Kramer test "
                    },
                    {
                      "__type": "FootnoteReference",
                      "__tag": 4066,
                      "label": "4"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". If False, perform Games-Howell test "
                    },
                    {
                      "__type": "FootnoteReference",
                      "__tag": 4066,
                      "label": "7"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", which does not assume equal variances."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Tukey's honestly significant difference (HSD) test performs pairwise comparison of means for a set of samples. Whereas ANOVA (e.g. "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "f_oneway",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ") 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."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "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."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/stats/_hypotests.py",
  "item_line": 1990,
  "item_type": "function",
  "aliases": [
    "scipy.stats.tukey_hsd"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "Here are some data comparing the time to relief of three brands of\nheadache medicine, reported in minutes. Data adapted from [3]_.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\nfrom scipy.stats import tukey_hsd\ngroup0 = [24.5, 23.5, 26.4, 27.1, 29.9]\ngroup1 = [28.4, 34.2, 29.5, 32.2, 30.1]\ngroup2 = [26.1, 28.3, 24.3, 26.2, 27.8]\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nWe would like to see if the means between any of the groups are\nsignificantly different. First, visually examine a box and whisker plot.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import matplotlib.pyplot as plt\nfig, ax = plt.subplots(1, 1)\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax.boxplot([group0, group1, group2])\n",
        "execution_status": "failure",
        "out": "{'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': []}\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "plt.show()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "1.18.0",
          "kind": "assets",
          "path": "fig-5edbceb7e2ff1018.png"
        }
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nFrom the box and whisker plot, we can see overlap in the interquartile\nranges group 1 to group 2 and group 3, but we can apply the ``tukey_hsd``\ntest to determine if the difference between means is significant. We\nset a significance level of .05 to reject the null hypothesis.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res = tukey_hsd(group0, group1, group2)\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "print(res)\n",
        "execution_status": "failure",
        "out": "Pairwise Group Comparisons (95.0% Confidence Interval)\nComparison  Statistic  p-value  Lower CI  Upper CI\n (0 - 1)     -4.600     0.014    -8.249    -0.951\n (0 - 2)     -0.260     0.980    -3.909     3.389\n (1 - 0)      4.600     0.014     0.951     8.249\n (1 - 2)      4.340     0.020     0.691     7.989\n (2 - 0)      0.260     0.980    -3.389     3.909\n (2 - 1)     -4.340     0.020    -7.989    -0.691\n\n"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe null hypothesis is that each group has the same mean. The p-value for\ncomparisons between ``group0`` and ``group1`` as well as ``group1`` and\n``group2`` do not exceed .05, so we reject the null hypothesis that they\nhave the same means. The p-value of the comparison between ``group0``\nand ``group2`` exceeds .05, so we accept the null hypothesis that there\nis not a significant difference between their means.\n\nWe can also compute the confidence interval associated with our chosen\nconfidence level.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "group0 = [24.5, 23.5, 26.4, 27.1, 29.9]\ngroup1 = [28.4, 34.2, 29.5, 32.2, 30.1]\ngroup2 = [26.1, 28.3, 24.3, 26.2, 27.8]\nresult = tukey_hsd(group0, group1, group2)\nconf = res.confidence_interval(confidence_level=.99)\nfor ((i, j), l) in np.ndenumerate(conf.low):\n    # filter out self comparisons\n    if i != j:\n        h = conf.high[i,j]\n        print(f\"({i} - {j}) {l:>6.3f} {h:>6.3f}\")\n",
        "execution_status": "success",
        "out": "(0 - 1) -9.480  0.280\n(0 - 2) -5.140  4.620\n(1 - 0) -0.280  9.480\n(1 - 2) -0.540  9.220\n(2 - 0) -4.620  5.140\n(2 - 1) -9.220  0.540\n"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "dunnett",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.stats._multicomp:dunnett"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "performs comparison of means against a control group."
            }
          ]
        }
      ],
      "type": "func"
    }
  ],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "samples",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "VAR_POSITIONAL",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "equal_var",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "KEYWORD_ONLY",
        "default": "True"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "tukey_hsd"
  },
  "references": [
    ".. [1] NIST/SEMATECH e-Handbook of Statistical Methods, \"7.4.7.1. Tukey's",
    "       Method.\"",
    "       https://www.itl.nist.gov/div898/handbook/prc/section4/prc471.htm,",
    "       28 November 2020.",
    ".. [2] Abdi, Herve & Williams, Lynne. (2021). \"Tukey's Honestly Significant",
    "       Difference (HSD) Test.\"",
    "       https://personal.utdallas.edu/~herve/abdi-HSD2010-pretty.pdf",
    ".. [3] \"One-Way ANOVA Using SAS PROC ANOVA & PROC GLM.\" SAS",
    "       Tutorials, 2007.",
    "       https://www.stattutorials.com/SAS/TUTORIAL-PROC-GLM.htm",
    ".. [4] Kramer, Clyde Young. \"Extension of Multiple Range Tests to Group",
    "       Means with Unequal Numbers of Replications.\" Biometrics, vol. 12,",
    "       no. 3, 1956, pp. 307-310. https://www.jstor.org/stable/3001469",
    ".. [5] NIST/SEMATECH e-Handbook of Statistical Methods, \"7.4.3.3.",
    "       The ANOVA table and tests of hypotheses about means\"",
    "       https://www.itl.nist.gov/div898/handbook/prc/section4/prc433.htm,",
    "       2 June 2021.",
    ".. [6] Tukey, John W. \"Comparing Individual Means in the Analysis of",
    "       Variance.\" Biometrics, vol. 5, no. 2, 1949, pp. 99-114.",
    "       https://www.jstor.org/stable/3001913",
    ".. [7] P. A. Games and J. F. Howell, \"Pairwise Multiple Comparison Procedures",
    "       with Unequal N's and/or Variances: A Monte Carlo Study,\" Journal of",
    "       Educational Statistics, vol. 1, no. 2, pp. 113-125, Jun. 1976.",
    "       :doi:`10.3102/10769986001002113`."
  ],
  "qa": "scipy.stats._hypotests:tukey_hsd",
  "arbitrary": [],
  "local_refs": [
    "*samples",
    "equal_var",
    "result"
  ]
}