每当我尝试在Python中运行CoxPH回归时,我都会收到错误消息。我不是蟒蛇的专家,我还在学习。
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from lifelines import KaplanMeierFitter
from lifelines.statistics import multivariate_logrank_test
from lifelines.statistics import logrank_test
from lifelines import CoxPHFitter
import pyreadstat
在加载数据之后
data["faculty2"] = data["faculty2"].astype(int)
data["sex"] = data["sex"].astype(int)
data["mos"] = data["mos"].astype(int)
data["state2"] = data["state2"].astype(int)
data["ss"] = data["ss"].astype(int)
data["supervisor"] = data["supervisor"].astype(int)
data["time"] = data["time"].astype(int)
data["event"] = data["event"].astype(int)
Eventvar = data['event']
Timevar = data['time']
""" assigning labels to values"""
data['sex'] = data['sex'].apply({1:'Male', 0:'female'}.get)
data['faculty2'] = data['faculty2'].apply({1:'Arts',2:'Sciences',3:'Medicals',\
4:'Agriculture', 5:'Social Sciences',6:'Education',\
7:'Tech',8:'Law',9:'Institues'}.get)
data['state2'] = data['state2'].apply({1:'SW',2:'SS',3:'SE',4:'NC', 5:'NE',6:'NW'}.get)
data['ss'] = data['ss'].apply({1:'Yes', 0:'No'}.get)
data['mos'] = data['mos'].apply({1:'Full Time', 0:'Part Time'}.get)
cf = CoxPHFitter()
cf.fit(data, 'time', event_col='event',show_progress=True)
cf.print_summary()
当我运行这些代码时,我会得到这个错误消息。
ValueError: could not convert string to float: 'Arts'
我需要帮助--我不知道该怎么做--如果我添加了假人,我有不同的错误信息
ohe_features = ['faculty2', 'sex', 'mos','state2','ss']
data = pd.get_dummies(data,drop_first=True,columns=ohe_features)
这是我得到的错误信息
ConvergenceError: Convergence halted due to matrix inversion problems. Suspicion is high collinearity. Please see the following tips in the lifelines documentation: https://lifelines.readthedocs.io/en/latest/Examples.html#problems-with-convergence-in-the-cox-proportional-hazard-modelMatrix is singular
如果不使用来运行代码,将值赋值给标签,而不添加假人,则会运行这些代码,但是不同的级别没有显示出来。它的运行就好像是连续变量一样。
发布于 2021-01-26 18:30:45
在生命线文件中,他们建议
发布于 2022-06-28 21:34:09
我有个完全相同的问题。我改变了
cph = CoxPHFitter()
至
cph = CoxPHFitter(penalizer=0.0001)
这解决了这个问题。
https://stackoverflow.com/questions/65278367
复制相似问题