{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "(survival_analysis)=\n", "# Bayesian Survival Analysis\n", "\n", "[Survival analysis](https://en.wikipedia.org/wiki/Survival_analysis) studies the distribution of the time to an event. Its applications span many fields across medicine, biology, engineering, and social science. This tutorial shows how to fit and analyze a Bayesian survival model in Python using PyMC.\n", "\n", "We illustrate these concepts by analyzing a [mastectomy data set](https://vincentarelbundock.github.io/Rdatasets/doc/HSAUR/mastectomy.html) from `R`'s [HSAUR](https://cran.r-project.org/web/packages/HSAUR/index.html) package.\n", "\n", ":::{post} Jan 17, 2023\n", ":tags: censored, survival analysis \n", ":category: intermediate, how-to\n", ":author: Austin Rochford, Chris Fonnesbeck\n", ":::" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on PyMC v5.0.1+42.g99dd7158\n" ] } ], "source": [ "import arviz as az\n", "import numpy as np\n", "import pandas as pd\n", "import pymc as pm\n", "import pytensor\n", "\n", "from matplotlib import pyplot as plt\n", "from pymc.distributions.timeseries import GaussianRandomWalk\n", "from pytensor import tensor as T\n", "\n", "print(f\"Running on PyMC v{pm.__version__}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "RANDOM_SEED = 8927\n", "rng = np.random.default_rng(RANDOM_SEED)\n", "az.style.use(\"arviz-darkgrid\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "try:\n", " df = pd.read_csv(\"../data/mastectomy.csv\")\n", "except FileNotFoundError:\n", " df = pd.read_csv(pm.get_data(\"mastectomy.csv\"))\n", "\n", "df.event = df.event.astype(np.int64)\n", "df.metastasized = (df.metastasized == \"yes\").astype(np.int64)\n", "n_patients = df.shape[0]\n", "patients = np.arange(n_patients)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | time | \n", "event | \n", "metastasized | \n", "
---|---|---|---|
0 | \n", "23 | \n", "1 | \n", "0 | \n", "
1 | \n", "47 | \n", "1 | \n", "0 | \n", "
2 | \n", "69 | \n", "1 | \n", "0 | \n", "
3 | \n", "70 | \n", "0 | \n", "0 | \n", "
4 | \n", "100 | \n", "0 | \n", "0 | \n", "