{
"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",
" Unnamed: 0 | \n",
" iter | \n",
" model_cohort | \n",
" sex | \n",
" level_3 | \n",
" event_time | \n",
" survival | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" test model | \n",
" female | \n",
" 0 | \n",
" 0.000000 | \n",
" 1.000000 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 0 | \n",
" test model | \n",
" female | \n",
" 1 | \n",
" 2.615961 | \n",
" 1.000000 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 0 | \n",
" test model | \n",
" female | \n",
" 2 | \n",
" 3.584694 | \n",
" 0.977162 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 0 | \n",
" test model | \n",
" female | \n",
" 3 | \n",
" 4.379338 | \n",
" 0.952078 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 0 | \n",
" test model | \n",
" female | \n",
" 4 | \n",
" 6.253546 | \n",
" 0.945339 | \n",
"
\n",
" \n",
"
\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",
" sex | \n",
" event_time | \n",
" median | \n",
" 50_lower | \n",
" 95_lower | \n",
" 95_upper | \n",
" 50_upper | \n",
"
\n",
" \n",
" \n",
" \n",
" 72 | \n",
" female | \n",
" 18.543842 | \n",
" 0.338076 | \n",
" 0.283789 | \n",
" 0.182441 | \n",
" 0.519674 | \n",
" 0.405841 | \n",
"
\n",
" \n",
" 73 | \n",
" female | \n",
" 18.656898 | \n",
" 0.331524 | \n",
" 0.275271 | \n",
" 0.176058 | \n",
" 0.507488 | \n",
" 0.398727 | \n",
"
\n",
" \n",
" 74 | \n",
" female | \n",
" 18.932325 | \n",
" 0.320479 | \n",
" 0.269188 | \n",
" 0.176320 | \n",
" 0.492980 | \n",
" 0.384687 | \n",
"
\n",
" \n",
" 75 | \n",
" female | \n",
" 19.811832 | \n",
" 0.306252 | \n",
" 0.251404 | \n",
" 0.157844 | \n",
" 0.471677 | \n",
" 0.367278 | \n",
"
\n",
" \n",
" 76 | \n",
" female | \n",
" 20.000000 | \n",
" 0.284725 | \n",
" 0.229034 | \n",
" 0.134397 | \n",
" 0.467671 | \n",
" 0.344815 | \n",
"
\n",
" \n",
"
\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
}