{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Bayesian Survival Analysis\n", "\n", "Author: Austin Rochford\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 [PyMC3](https://pymc-devs.github.io/pymc3).\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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Couldn't import dot_parser, loading of dot files will not be possible.\n" ] } ], "source": [ "from matplotlib import pyplot as plt\n", "import numpy as np\n", "import pymc3 as pm\n", "from pymc3.distributions.timeseries import GaussianRandomWalk\n", "import seaborn as sns\n", "from statsmodels import datasets\n", "from theano import tensor as T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fortunately, [`statsmodels.datasets`](http://statsmodels.sourceforge.net/0.6.0/datasets/index.html) makes it quite easy to load a number of data sets from `R`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = datasets.get_rdataset('mastectomy', 'HSAUR', cache=True).data\n", "df.event = df.event.astype(np.int64)\n", "df.metastized = (df.metastized == 'yes').astype(np.int64)\n", "n_patients = df.shape[0]\n", "patients = np.arange(n_patients)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | time | \n", "event | \n", "metastized | \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", "