{ "cells": [ { "cell_type": "code", "execution_count": 3, "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" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/jacquelineburos/miniconda3/envs/python3/lib/python3.5/site-packages/Cython/Distutils/old_build_ext.py:30: UserWarning: Cython.Distutils.old_build_ext does not properly handle dependencies and is deprecated.\n", " \"Cython.Distutils.old_build_ext does not properly handle dependencies \"\n", "/home/jacquelineburos/.local/lib/python3.5/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.\n", " \"`IPython.html.widgets` has moved to `ipywidgets`.\", ShimWarning)\n", "INFO:stancache.seed:Setting seed to 1245502385\n" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline\n", "import random\n", "random.seed(1100038344)\n", "import survivalstan\n", "import numpy as np\n", "import pandas as pd\n", "from stancache import stancache\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulate survival data " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to demonstrate the use of this model, we will first simulate some survival data using `survivalstan.sim.sim_data_exp_correlated`. As the name implies, this function simulates data assuming a constant hazard throughout the follow-up time period, which is consistent with the Exponential survival function.\n", "\n", "This function includes two simulated covariates by default (`age` and `sex`). We also simulate a situation where hazard is a function of the simulated value for `sex`. \n", "\n", "We also center the `age` variable since this will make it easier to interpret estimates of the baseline hazard.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:stancache.stancache:sim_data_exp_correlated: cache_filename set to sim_data_exp_correlated.cached.N_100.censor_time_20.rate_coefs_54462717316.rate_form_1 + sex.pkl\n", "INFO:stancache.stancache:sim_data_exp_correlated: Loading result from cache\n" ] } ], "source": [ "d = stancache.cached(\n", " survivalstan.sim.sim_data_exp_correlated,\n", " N=100,\n", " censor_time=20,\n", " rate_form='1 + sex',\n", " rate_coefs=[-3, 0.5],\n", ")\n", "d['age_centered'] = d['age'] - d['age'].mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Aside: In order to make this a more reproducible example, this code is using a file-caching function `stancache.cached` to wrap a function call to `survivalstan.sim.sim_data_exp_correlated`. *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explore simulated data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is what these data look like - this is `per-subject` or `time-to-event` form:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | age | \n", "sex | \n", "rate | \n", "true_t | \n", "t | \n", "event | \n", "index | \n", "age_centered | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "59 | \n", "male | \n", "0.082085 | \n", "20.948771 | \n", "20.000000 | \n", "False | \n", "0 | \n", "4.18 | \n", "
1 | \n", "58 | \n", "male | \n", "0.082085 | \n", "12.827519 | \n", "12.827519 | \n", "True | \n", "1 | \n", "3.18 | \n", "
2 | \n", "61 | \n", "female | \n", "0.049787 | \n", "27.018886 | \n", "20.000000 | \n", "False | \n", "2 | \n", "6.18 | \n", "
3 | \n", "57 | \n", "female | \n", "0.049787 | \n", "62.220296 | \n", "20.000000 | \n", "False | \n", "3 | \n", "2.18 | \n", "
4 | \n", "55 | \n", "male | \n", "0.082085 | \n", "10.462045 | \n", "10.462045 | \n", "True | \n", "4 | \n", "0.18 | \n", "