--- a +++ b/demo/notebooks/CoxReg_Single_Run.ipynb @@ -0,0 +1,97 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Survival Regression Model code" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook describes code for the Cox Proportional Hazards regression used for the conventional parameter model in our study. We define a simple function that accepts as arguments the input data (predictor variables and survival outcomes) and a training parameter.\n", + "Below we describe each section of the code:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Import required libraries:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np, pandas as pd\n", + "import os, sys, time, pickle, copy, h5py\n", + "import lifelines.utils\n", + "from lifelines import CoxPHFitter" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Define function:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def coxreg_single_run(xtr, ytr, penalty):\n", + " df_tr = pd.DataFrame(np.concatenate((ytr, xtr),axis=1))\n", + " df_tr.columns = ['status','time'] + ['X'+str(i+1) for i in range(xtr.shape[1])]\n", + " cph = CoxPHFitter(penalizer=penalty)\n", + " cph.fit(df_tr, duration_col='time', event_col='status')\n", + " return cph" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Function arguments:\n", + "- `xtr` : $n \\times p$ matrix of predictor variables, where $n$ is sample size and $p$ is number of predictor variables. \n", + "- `ytr` : $n \\times 2$ matrix of survival outcomes, where first column is censoring status and second is survival/censoring time\n", + "- `penalty` : To control for high correlations among the variables, L2-norm regularization is used to restrict the size of the coefficient estimates, which improves their stability. The `penalty` coefficient controls the strength of the L2-norm regularization\n", + "\n", + "Function output:\n", + "- A `CoxPHFitter()` object which stores the model structure, regression coefficient estimates, etc. This object can be used to make predictions on new (test/validation) data" + ] + } + ], + "metadata": { + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}