1

每当我尝试在 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

如果我运行代码而不为标签分配值并且不添加假人,它会运行但不同的级别没有显示。它就像连续变量一样运行

这是数据

4

1 回答 1

1

在他们建议的生命线文档中

  1. 添加惩罚参数
  2. 使用方差膨胀因子或
  3. 检查数据集中的相关矩阵

https://lifelines.readthedocs.io/en/latest/Examples.html#problems-with-convergence-in-the-cox-proportional-hazard-modelMatrix

于 2021-01-26T18:30:45.727 回答