每当我尝试在Python中运行CoxPH回归时,我都会收到错误消息。我不是一个仍在学习python的专业人士。
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
如果我运行代码时没有为标签分配值,也没有添加假人它会运行,但不同的级别不会显示。它就像是连续变量一样运行
这是的数据
在生命线文档中,他们建议
- 添加惩罚参数
- 使用方差膨胀因子或
- 检查数据集中的相关矩阵
https://lifelines.readthedocs.io/en/latest/Examples.html#problems-具有收敛性的cox比例风险模型矩阵
我遇到了完全相同的问题。我更改了
cph=CoxPFitter((
至
cph=CoxPFitter(惩罚=0.0001(
这解决了问题。