{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "916423e0",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "848b2c05",
            "cell_type": "markdown",
            "source": "Here are some data comparing the time to relief of three brands of\nheadache medicine, reported in minutes. Data adapted from [3].\n",
            "metadata": {}
        },
        {
            "id": "aaf9d4b6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "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]",
            "outputs": []
        },
        {
            "id": "2b91872f",
            "cell_type": "markdown",
            "source": "We would like to see if the means between any of the groups are\nsignificantly different. First, visually examine a box and whisker plot.\n",
            "metadata": {}
        },
        {
            "id": "478694eb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig, ax = plt.subplots(1, 1)\nax.boxplot([group0, group1, group2])\nax.set_xticklabels([\"group0\", \"group1\", \"group2\"]) # doctest: +SKIP\nax.set_ylabel(\"mean\") # doctest: +SKIP\nplt.show()",
            "outputs": []
        },
        {
            "id": "68338077",
            "cell_type": "markdown",
            "source": "From 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",
            "metadata": {}
        },
        {
            "id": "094701d2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = tukey_hsd(group0, group1, group2)\nprint(res)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "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"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5e3d67a5",
            "cell_type": "markdown",
            "source": "The 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",
            "metadata": {}
        },
        {
            "id": "f5f8b353",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "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}\")",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(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"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}