{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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": 2, "metadata": {}, "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" ] }, { "name": "stderr", "output_type": "stream", "text": [ "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": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | sex | \n", "age | \n", "rate | \n", "true_t | \n", "t | \n", "event | \n", "index | \n", "age_centered | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "male | \n", "54 | \n", "0.082085 | \n", "1.013855 | \n", "1.013855 | \n", "True | \n", "0 | \n", "-1.12 | \n", "
1 | \n", "male | \n", "39 | \n", "0.082085 | \n", "4.890597 | \n", "4.890597 | \n", "True | \n", "1 | \n", "-16.12 | \n", "
2 | \n", "female | \n", "45 | \n", "0.049787 | \n", "4.093404 | \n", "4.093404 | \n", "True | \n", "2 | \n", "-10.12 | \n", "
3 | \n", "female | \n", "43 | \n", "0.049787 | \n", "7.036226 | \n", "7.036226 | \n", "True | \n", "3 | \n", "-12.12 | \n", "
4 | \n", "female | \n", "57 | \n", "0.049787 | \n", "5.712299 | \n", "5.712299 | \n", "True | \n", "4 | \n", "1.88 | \n", "