{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline\n", "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly\n", "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "plotly.offline.init_notebook_mode(connected=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## load pp_survival data from a random survival model" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ppsurv = pd.read_csv('plotly_example_data.csv')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0itermodel_cohortsexlevel_3event_timesurvival
000test modelfemale00.0000001.000000
110test modelfemale12.6159611.000000
220test modelfemale23.5846940.977162
330test modelfemale34.3793380.952078
440test modelfemale46.2535460.945339
\n", "
" ], "text/plain": [ " Unnamed: 0 iter model_cohort sex level_3 event_time survival\n", "0 0 0 test model female 0 0.000000 1.000000\n", "1 1 0 test model female 1 2.615961 1.000000\n", "2 2 0 test model female 2 3.584694 0.977162\n", "3 3 0 test model female 3 4.379338 0.952078\n", "4 4 0 test model female 4 6.253546 0.945339" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ppsurv.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## plot posterior predicted survival time by sex" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ppsummary = ppsurv.groupby(['sex','event_time'])['survival'].agg({\n", " '95_lower': lambda x: np.percentile(x, 2.5),\n", " '95_upper': lambda x: np.percentile(x, 97.5),\n", " '50_lower': lambda x: np.percentile(x, 25),\n", " '50_upper': lambda x: np.percentile(x, 75),\n", " 'median': lambda x: np.percentile(x, 50),\n", " }).reset_index()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sexevent_timemedian50_lower95_lower95_upper50_upper
72female18.5438420.3380760.2837890.1824410.5196740.405841
73female18.6568980.3315240.2752710.1760580.5074880.398727
74female18.9323250.3204790.2691880.1763200.4929800.384687
75female19.8118320.3062520.2514040.1578440.4716770.367278
76female20.0000000.2847250.2290340.1343970.4676710.344815
\n", "
" ], "text/plain": [ " sex event_time median 50_lower 95_lower 95_upper 50_upper\n", "72 female 18.543842 0.338076 0.283789 0.182441 0.519674 0.405841\n", "73 female 18.656898 0.331524 0.275271 0.176058 0.507488 0.398727\n", "74 female 18.932325 0.320479 0.269188 0.176320 0.492980 0.384687\n", "75 female 19.811832 0.306252 0.251404 0.157844 0.471677 0.367278\n", "76 female 20.000000 0.284725 0.229034 0.134397 0.467671 0.344815" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ppsummary[ppsummary['sex']=='female'].tail()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shade_colors = dict(male='rgba(0, 128, 128, {})', female='rgba(214, 12, 140, {})')\n", "line_colors = dict(male='rgb(0, 128, 128)', female='rgb(214, 12, 140)')\n", "ppsummary.sort_values(['sex', 'event_time'], inplace=True)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data5 = list()\n", "for grp, grp_df in ppsummary.groupby('sex'):\n", " x = list(grp_df['event_time'].values)\n", " x_rev = x[::-1]\n", " y_upper = list(grp_df['50_upper'].values)\n", " y_lower = list(grp_df['50_lower'].values)\n", " y_lower = y_lower[::-1]\n", " y2_upper = list(grp_df['95_upper'].values)\n", " y2_lower = list(grp_df['95_lower'].values)\n", " y2_lower = y2_lower[::-1]\n", " y = list(grp_df['median'].values)\n", " my_shading50 = go.Scatter(\n", " x = x + x_rev,\n", " y = y_upper + y_lower,\n", " fill = 'tozerox',\n", " fillcolor = shade_colors[grp].format(0.3),\n", " line = go.Line(color = 'transparent'),\n", " showlegend = True,\n", " name = '{} - 50% CI'.format(grp),\n", " )\n", " my_shading95 = go.Scatter(\n", " x = x + x_rev,\n", " y = y2_upper + y2_lower,\n", " fill = 'tozerox',\n", " fillcolor = shade_colors[grp].format(0.1),\n", " line = go.Line(color = 'transparent'),\n", " showlegend = True,\n", " name = '{} - 95% CI'.format(grp),\n", " )\n", " my_line = go.Scatter(\n", " x = x,\n", " y = y,\n", " line = go.Line(color=line_colors[grp]),\n", " mode = 'lines',\n", " name = grp,\n", " )\n", " data5.append(my_line) \n", " data5.append(my_shading50)\n", " data5.append(my_shading95)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layout5 = go.Layout(\n", " yaxis=dict(\n", " title='Survival (%)',\n", " #zeroline=False,\n", " tickformat='.0%',\n", " ),\n", " xaxis=dict(title='Days since enrollment')\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.iplot(go.Figure(data=data5, layout=layout5), filename='survivalstan/posterior-predicted-values')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }