{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "e1c25c37",
            "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": "6adc25a1",
            "cell_type": "markdown",
            "source": "Reference [2] compared the survival times of patients with two different\ntypes of recurrent malignant gliomas. The samples below record the time\n(number of weeks) for which each patient participated in the study. The\n`scipy.stats.CensoredData` class is used because the data is\nright-censored: the uncensored observations correspond with observed deaths\nwhereas the censored observations correspond with the patient leaving the\nstudy for another reason.\n",
            "metadata": {}
        },
        {
            "id": "5b970383",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import stats\nx = stats.CensoredData(\n    uncensored=[6, 13, 21, 30, 37, 38, 49, 50,\n                63, 79, 86, 98, 202, 219],\n    right=[31, 47, 80, 82, 82, 149]\n)\ny = stats.CensoredData(\n    uncensored=[10, 10, 12, 13, 14, 15, 16, 17, 18, 20, 24, 24,\n                25, 28,30, 33, 35, 37, 40, 40, 46, 48, 76, 81,\n                82, 91, 112, 181],\n    right=[34, 40, 70]\n)",
            "outputs": []
        },
        {
            "id": "63e1d86d",
            "cell_type": "markdown",
            "source": "We can calculate and visualize the empirical survival functions\nof both groups as follows.\n",
            "metadata": {}
        },
        {
            "id": "7ad0dcf5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nax = plt.subplot()\necdf_x = stats.ecdf(x)\necdf_x.sf.plot(ax, label='Astrocytoma')\necdf_y = stats.ecdf(y)\necdf_y.sf.plot(ax, label='Glioblastoma')\nax.set_xlabel('Time to death (weeks)')\nax.set_ylabel('Empirical SF')\nplt.legend()\nplt.show()",
            "outputs": []
        },
        {
            "id": "708558bb",
            "cell_type": "markdown",
            "source": "Visual inspection of the empirical survival functions suggests that the\nsurvival times tend to be different between the two groups. To formally\nassess whether the difference is significant at the 1% level, we use the\nlogrank test.\n",
            "metadata": {}
        },
        {
            "id": "b0627ea8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = stats.logrank(x=x, y=y)\nres.statistic",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "-2.73799"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ec2b8f39",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res.pvalue",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.00618"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ea22bc1c",
            "cell_type": "markdown",
            "source": "The p-value is less than 1%, so we can consider the data to be evidence\nagainst the null hypothesis in favor of the alternative that there is a\ndifference between the two survival functions.\n",
            "metadata": {}
        }
    ]
}