- class surpyval.univariate.nonparametric.kaplan_meier.KaplanMeier_
Bases:
NonParametricFitterKaplan-Meier estimator class. Calculates the Non-Parametric estimate of the survival function using:
\[R(x) = \prod_{i:x_{i} \leq x}^{} \left ( 1 - \frac{d_{i} }{r_{i}} \right )\]The variance of the cumulative hazard used for confidence bounds is estimated with Greenwood’s formula:
\[\widehat{Var}(H(x)) = \sum_{i:x_{i} \leq x} \frac{d_{i}}{r_{i} \left ( r_{i} - d_{i} \right )}\]Examples
>>> import numpy as np >>> from surpyval import KaplanMeier >>> x = np.array([1, 2, 3, 4, 5]) >>> model = KaplanMeier.fit(x) >>> model.R array([0.8, 0.6, 0.4, 0.2, 0. ])
- fit(x: ArrayLike | None = None, c: ArrayLike | None = None, n: ArrayLike | None = None, t: ArrayLike | None = None, xl: ArrayLike | None = None, xr: ArrayLike | None = None, tl: ArrayLike | numbers.Number | None = None, tr: ArrayLike | numbers.Number | None = None, turnbull_estimator: str = 'Fleming-Harrington', set_lower_limit: float | None = None) NonParametric
The central feature to SurPyval’s capability. This function aimed to have an API to mimic the simplicity of the scipy API. That is, to use a simple
fit()call, with as many or as few parameters as are needed.- Parameters
x (array like, optional) – Array of observations of the random variables. If x is
None, xl and xr must be provided.c (array like, optional) – Array of censoring flag. -1 is left censored, 0 is observed, 1 is right censored, and 2 is intervally censored. If not provided will assume all values are observed.
n (array like, optional) – Array of counts for each x. If data is proivded as counts, then this can be provided. If
Nonewill assume each observation is 1.t (2D-array like, optional) – 2D array like of the left and right values at which the respective observation was truncated. If not provided it assumes that no truncation occurs.
tl (array like or scalar, optional) – Values of left truncation for observations. If it is a scalar value assumes each observation is left truncated at the value. If an array, it is the respective ‘late entry’ of the observation.
tr (array like or scalar, optional) – Values of right truncation for observations. If it is a scalar value assumes each observation is right truncated at the value. If an array, it is the respective right truncation value for each observation.
xl (array like, optional) – Array like of the left array for 2-dimensional input of x. This is useful for data that is all intervally censored. Must be used with the
xrinput.xr (array like, optional) – Array like of the right array for 2-dimensional input of x. This is useful for data that is all intervally censored. Must be used with the
xlinput.turnbull_estimator (('Nelson-Aalen', 'Kaplan-Meier', or) –
'Fleming-Harrington') – If using the Turnbull heuristic, you can elect to use either the KM, NA, or FH estimator with the Turnbull estimates of r, and d. Defaults to FH.
str – If using the Turnbull heuristic, you can elect to use either the KM, NA, or FH estimator with the Turnbull estimates of r, and d. Defaults to FH.
optional – If using the Turnbull heuristic, you can elect to use either the KM, NA, or FH estimator with the Turnbull estimates of r, and d. Defaults to FH.
- Returns
model – A parametric model with the fitted parameters and methods for all functions of the distribution using the fitted parameters.
- Return type
Examples
>>> from surpyval import NelsonAalen, Weibull, Turnbull >>> import numpy as np >>> x = Weibull.random(100, 10, 4) >>> model = NelsonAalen.fit(x) >>> print(model) Non-Parametric SurPyval Model ============================= Model : Nelson-Aalen >>> Turnbull.fit(x, turnbull_estimator='Kaplan-Meier') Non-Parametric SurPyval Model ============================= Model : Turnbull Estimator : Kaplan-Meier
- from_xrd(x: ArrayLike, r: ArrayLike, d: ArrayLike) NonParametric
The central feature to SurPyval’s capability. This function aimed to have an API to mimic the simplicity of the scipy API. That is, to use a simple
fit()call, with as many or as few parameters as are needed.- Parameters
x (array like, optional) – Array of observations of the random variables. If x is
None, xl and xr must be provided.r (array like, optional) – Array of at risk items. For each value of x the r array is the number of at risk items immediately prior to the failures at x.
d (array like, optional) – Array of counts of deaths/failures at each x. For each value of x the d array is the number of deaths at x (can be zero).
- Returns
model – A non-parametric model with the survival curved estimated using the selected method.
- Return type
Examples
>>> from surpyval import NelsonAalen, Weibull, Turnbull >>> import numpy as np >>> x = [1, 2, 3, 4, 5, 6] >>> r = [10, 8, 6, 4, 3, 2] >>> d = [2, 1, 1, 1, 1, 1] >>> model = NelsonAalen.from_xrd(x, r, d) >>> print(model) Non-Parametric SurPyval Model ============================= Model : Nelson-Aalen