Skip to content

Commit

Permalink
more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Jan 7, 2019
1 parent e379a16 commit 12f8c99
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 21 deletions.
21 changes: 19 additions & 2 deletions docs/jupyter_notebooks/Proportional hazard assumption.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
"source": [
"## Testing the Proportional Hazard Assumptions\n",
"\n",
"This Jupyter notebook is a small tutorial on how to test and fix proportional hazard problems. Recall that the proportional hazard assumption is that the ratio of the hazard of the $i$th individual and the baseline hazard is constant (that is, not a function of time). "
"This Jupyter notebook is a small tutorial on how to test and fix proportional hazard problems. \n",
"\n",
"The proportional hazard assumption is that _all_ individuals have the same hazard function, but a unique scaling factor infront. So the _shape_ of the hazard function is the same for all individuals, and only a scalar infront changes. \n",
"\n",
"$$h_i(t) = a_i h(t)$$\n",
"\n",
"At the core of the assumption is that $a_i$ is not time varying, that is, $a_i(t) = a_i$.. In this tutorial we will test this non-time varying assumption, and look at ways to handle violations. \n"
]
},
{
Expand Down Expand Up @@ -99,7 +105,17 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In the advice above, we can see that `wexp` has small cardinality, so we can easily fix that by specifying it in the `strata`. What does the `strata` do? For each unique value in the stratifying variable(s), a new baseline hazard is created. Hence, for each strata, a unique time-varying baseline can be created to fit the unique time-dependent effects of the variables. "
"In the advice above, we can see that `wexp` has small cardinality, so we can easily fix that by specifying it in the `strata`. What does the `strata` do? Let's go back to the proportional hazard assumption.\n",
"\n",
"In the introduction, we said that the proportional hazard assumption was that \n",
"\n",
"$$ h_i(t) = a_i h(t)$$\n",
"\n",
"In a simple case, it may be that there are two subgroups that have _very_ different baseline hazards. That is, we can split the dataset into subsamples based on some variable (we call this the stratifying variable), run the Cox model on all subsamples, and compare their baseline hazards. If these baseline hazards are _very_ different, then clearly the formula above is wrong - the $h(t)$ is some weighted average of the subgroups' baseline hazards. This ill fitting average baseline can cause $a_i$ to have time-dependent influence. A better model might be:\n",
"\n",
"$$ h_{i |i\\in G}(t) = a_i h_G(t)$$\n",
"\n",
"where now we have a unique baseline hazard _per_ subgroup $G$. Because of the way the Cox model is designed, inference of the coefficients is identical (expect now there are more baseline hazards, and no variation of the stratifying variable within a subgroup $G$). \n"
]
},
{
Expand Down Expand Up @@ -129,6 +145,7 @@
"\n",
"#### Option 1: bin variable and stratify on it\n",
"\n",
"\n",
"The first option proposed is to bin the variable into equal-sized bins, and stratify like we did with `wexp`. There is a trade off here between estimation and information-loss. If we have large bins, we will lose information (since different values are now binned together), but we need to estimate less new baseline hazards. On the other hand, with tiny bins, we allow the `age` data to have the most \"wiggle room\", but must compute many baseline hazards each of which has a smaller sample size. Like most things, the optimial value is somewhere inbetween."
]
},
Expand Down
12 changes: 12 additions & 0 deletions docs/lifelines.datasets.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
lifelines.datasets package
==========================

Submodules
----------

lifelines.datasets.dfcv\_dataset module
---------------------------------------

.. automodule:: lifelines.datasets.dfcv_dataset
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------

Expand Down
44 changes: 30 additions & 14 deletions docs/lifelines.fitters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,72 @@ lifelines.fitters package
Submodules
----------

lifelines.fitters.aalen_additive_fitter module
----------------------------------------------
lifelines.fitters.aalen\_additive\_fitter module
------------------------------------------------

.. automodule:: lifelines.fitters.aalen_additive_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.breslow_fleming_harrington_fitter module
----------------------------------------------------------
lifelines.fitters.aalen\_johansen\_fitter module
------------------------------------------------

.. automodule:: lifelines.fitters.aalen_johansen_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.breslow\_fleming\_harrington\_fitter module
-------------------------------------------------------------

.. automodule:: lifelines.fitters.breslow_fleming_harrington_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.coxph_fitter module
-------------------------------------
lifelines.fitters.cox\_time\_varying\_fitter module
---------------------------------------------------

.. automodule:: lifelines.fitters.cox_time_varying_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.coxph\_fitter module
--------------------------------------

.. automodule:: lifelines.fitters.coxph_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.exponential_fitter module
-------------------------------------------
lifelines.fitters.exponential\_fitter module
--------------------------------------------

.. automodule:: lifelines.fitters.exponential_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.kaplan_meier_fitter module
--------------------------------------------
lifelines.fitters.kaplan\_meier\_fitter module
----------------------------------------------

.. automodule:: lifelines.fitters.kaplan_meier_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.nelson_aalen_fitter module
--------------------------------------------
lifelines.fitters.nelson\_aalen\_fitter module
----------------------------------------------

.. automodule:: lifelines.fitters.nelson_aalen_fitter
:members:
:undoc-members:
:show-inheritance:

lifelines.fitters.weibull_fitter module
---------------------------------------
lifelines.fitters.weibull\_fitter module
----------------------------------------

.. automodule:: lifelines.fitters.weibull_fitter
:members:
Expand Down
12 changes: 10 additions & 2 deletions docs/lifelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ Subpackages
Submodules
----------

lifelines.generate_datasets module
----------------------------------
lifelines.compat module
-----------------------

.. automodule:: lifelines.compat
:members:
:undoc-members:
:show-inheritance:

lifelines.generate\_datasets module
-----------------------------------

.. automodule:: lifelines.generate_datasets
:members:
Expand Down
28 changes: 26 additions & 2 deletions docs/lifelines.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@ lifelines.utils package
Submodules
----------

lifelines.utils.progress_bar module
-----------------------------------
lifelines.utils.btree module
----------------------------

.. automodule:: lifelines.utils.btree
:members:
:undoc-members:
:show-inheritance:

lifelines.utils.concordance module
----------------------------------

.. automodule:: lifelines.utils.concordance
:members:
:undoc-members:
:show-inheritance:

lifelines.utils.lowess module
-----------------------------

.. automodule:: lifelines.utils.lowess
:members:
:undoc-members:
:show-inheritance:

lifelines.utils.progress\_bar module
------------------------------------

.. automodule:: lifelines.utils.progress_bar
:members:
Expand Down
2 changes: 1 addition & 1 deletion lifelines/fitters/coxph_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ def _compute_likelihood_ratio_test(self):
trivial_dataset = pd.DataFrame({"E": self.event_observed, "T": self.durations, "W": self.weights})

cp_null = CoxPHFitter()
cp_null.fit(trivial_dataset, "T", "E", weights_col="W", show_progress=False)
cp_null.fit(trivial_dataset, "T", "E", weights_col="W", show_progress=True)

ll_null = cp_null._log_likelihood
ll_alt = self._log_likelihood
Expand Down

0 comments on commit 12f8c99

Please sign in to comment.