简体   繁体   中英

Lognormal survival analysis in python

I need to make a survival analysis with lognormal parametric model using python.

I have data I need to apply it to.

I followed this work here https://github.com/MustafaOguz/Survival_Analysis/blob/master/4_Parametric_Models.ipynb

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from lifelines import KaplanMeierFitter
from sklearn.model_selection import train_test_split
from statsmodels.base.model import GenericLikelihoodModel

df = pd.read_csv('survival.csv')

train, test = train_test_split(df[['T', 'E']], test_size=0.2)

kmf = KaplanMeierFitter()

kmf.fit(df['T'], event_observed=df['E'], label="All groups")


eventvar = df['E']
start_params_var = [0,0]

def _ll_lognormal(y,X,scale,gamma):
    ll = eventvar * (
    np.log(
    norm.pdf(((np.log(y) - scale) * gamma))/
    (y * (1/gamma) * (1 - norm.cdf((np.log(y) - scale) * gamma))))
    ) + np.log(1 - norm.cdf((np.log(y) - scale) * gamma))
    return ll

class Lognormal(GenericLikelihoodModel):
    def _init_(self,endog,exog,**kwds):
        super(Lognormal, self).__init__(endog,exog,**kwds)
    def nloglikeobs(self,params):
        scale = params[0]
        gamma = params[1]
        ll = _ll_lognormal(self.endog,self.exog,scale,gamma)
        return -ll
    def fit(self, start_params = None, maxiter = 10000,maxfun = 5000,**kwds):
        if len(self.exog_names) == len(self.exog[1]):
            self.exog_names.append('gamma')
        if start_params == None:
            start_params = start_params_var
        return super(Lognormal, self).fit(start_params = start_params, maxiter = maxiter, maxfun = maxfun,**kwds)


lognormal_data = np.repeat(1,len(df['T']))
mod_lognormal = Lognormal(df['T'],lognormal_data)
res_lognorm = mod_lognormal.fit()
print(res_lognorm.summary())

#Plot the lognormal prediction against the empirical survival curve
plt.figure()
ax = plt.subplot(1,1,1)
t = np.linspace(0,150,151)
plt.plot(t,mod_lognormal.predict_survival_lognormal(res_lognorm.params, t))
plt.plot(t,mod_lognormal.predict_survival_lognormal_cis(res_lognorm.params, res_lognorm.cov_params(), t)[[1]],'r--',linewidth = 1.0)
plt.plot(t,mod_lognormal.predict_survival_lognormal_cis(res_lognorm.params, res_lognorm.cov_params(), t)[[2]],'r--',linewidth = 1.0)
kmf.plot(ax = ax)
plt.title('Lognormal')
plt.xlabel('Years since start of group')
plt.ylabel('Probability of ending')
plt.savefig('Lognormal.png',dpi = 300)

but then got lost where predict_survival_lognormal , predict_survival_lognormal_cis , predict_survival_lognormal_cis methods came from.

Thanks in anticipation

More generally, lifelines has a lognormal parametric model implemented:

from lifelines import LogNormalFitter

df = pd.read_csv('survival.csv')

lnf = LogNormalFitter()
lnf.fit(df['T'], event_observed=df['E'], label="All groups")

lnf.plot()
lnf.print_summary()

# predicted survival function
lnf.survival_function_at_times([1,2,3])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM