[英]Lognormal survival analysis in python
我需要使用python使用對數正態參數模型進行生存分析。
我有需要應用的數據。
我在這里關注了這項工作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)
但是隨后迷失了predict_survival_lognormal
, predict_survival_lognormal_cis
, predict_survival_lognormal_cis
方法的來源。
謝謝你的期待
更一般而言, 生命線已實現了對數正態參數模型:
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])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.