lifelines icon indicating copy to clipboard operation
lifelines copied to clipboard

CoxPHFitter Error

Open slipss opened this issue 10 years ago • 16 comments

Not sure if this is the right place for this, but I am having an issue getting the CoxPH methods to work. I am new to survival analysis, so I assume this is something wrong with my data set up. I am getting a delta contains nan's, convergence halted during the coxph.fit()? Wondering if anyone can shed some light on why this is happening?

Thanks,

ValueError Traceback (most recent call last) 3 cphf1=CoxPHFitter() ----> 4 cphf1.fit(X, 'T','E') 5 cphf1.print_summary()

/lifelines/fitters/coxph_fitter.pyc in fit(self, df, duration_col, event_col, show_progress, initial_beta, include_likelihood, strata) 313 hazards_ = self.newton_rhaphson(df, T, E, initial_beta=initial_beta, 314 show_progress=show_progress, --> 315 include_likelihood=include_likelihood) 316 317 self.hazards = pd.DataFrame(hazards_.T, columns=df.columns,

lifelines/fitters/coxph_fitter.pyc in _newton_rhaphson(self, X, T, E, initial_beta, step_size, precision, show_progress, include_likelihood) 223 delta = solve(-h, step_size * g.T) 224 if np.any(np.isnan(delta)): --> 225 raise ValueError("delta contains nan value(s). Convergence halted.") 226 227 # Save these as pending result

ValueError: delta contains nan value(s). Convergence halted.

slipss avatar Jul 28 '16 19:07 slipss

Hey there @slipss, are you able to post the dataset you are using by chance? Or a reduced dataset?

CamDavidsonPilon avatar Jul 28 '16 19:07 CamDavidsonPilon

Not sure if this will work, but something like this, in a pandas DF

image

slipss avatar Jul 28 '16 19:07 slipss

Can you share that in a csv format?

CamDavidsonPilon avatar Jul 28 '16 19:07 CamDavidsonPilon

Here it is.

Dataset_example.csv.zip

slipss avatar Jul 28 '16 19:07 slipss

So, if I saved my dataframe to a csv, and then read it back into a pandas dataframe pd.read_csv() it no longer has that error.

I guess it has something to do with removing rows and columns from the dataframe, does lifelines just see those as empty perhaps?

slipss avatar Jul 28 '16 20:07 slipss

it might be a type error. Are you sure everything is a numeric or boolean?

CamDavidsonPilon avatar Jul 28 '16 20:07 CamDavidsonPilon

I believe everything is.

The Dataset I provided is really only half the dataset, as one of the binary variables, was spilt into two separate dataframes using:

df1=df[df['X']==1] df0=df[df['X']==0]

Would that be the cause of this issue?

slipss avatar Jul 28 '16 20:07 slipss

ah possibly, but 'X' is not being used in the model, is it? That is, "X" is being dropped before using fit, correct?

CamDavidsonPilon avatar Jul 28 '16 21:07 CamDavidsonPilon

Correct

slipss avatar Jul 28 '16 21:07 slipss

Sorry to bump an old thread, but I'm having this same issue and roundtripping through a csv did not solve the problem for me.

I'm just trying to call cphf.fit(X, 'duration', event_col='observed') where X is read from this csv. example.csv.zip

And I get this:

ValueError                                Traceback (most recent call last)
<ipython-input-54-dc888f60324d> in <module>()
----> 1 cphf.fit(X, 'duration', event_col='observed')

/Users/trevormurphy/.pyenv/versions/3.5.1/envs/analytics-3.5.1/lib/python3.5/site-packages/lifelines/fitters/coxph_fitter.py in fit(self, df, duration_col, event_col, show_progress, initial_beta, include_likelihood, strata)
    313         hazards_ = self._newton_rhaphson(df, T, E, initial_beta=initial_beta,
    314                                          show_progress=show_progress,
--> 315                                          include_likelihood=include_likelihood)
    316 
    317         self.hazards_ = pd.DataFrame(hazards_.T, columns=df.columns,

/Users/trevormurphy/.pyenv/versions/3.5.1/envs/analytics-3.5.1/lib/python3.5/site-packages/lifelines/fitters/coxph_fitter.py in _newton_rhaphson(self, X, T, E, initial_beta, step_size, precision, show_progress, include_likelihood)
    223             delta = solve(-h, step_size * g.T)
    224             if np.any(np.isnan(delta)):
--> 225                 raise ValueError("delta contains nan value(s). Convergence halted.")
    226 
    227             # Save these as pending result

ValueError: delta contains nan value(s). Convergence halted.

trevor-m-murphy avatar Sep 19 '16 16:09 trevor-m-murphy

cool, I'll look at this tonight or tomorrow!

CamDavidsonPilon avatar Sep 19 '16 17:09 CamDavidsonPilon

I'm seeing this same bug - any progress? Thanks!

bwgroff avatar Dec 13 '16 21:12 bwgroff

@trevor-m-murphy, I believe your problem was the "Intercept" col you added. Lifelines automatically will add an intercept col., and another col with constant values will probably fail convergence. I will add some checks before handle this.

@slipss, I have not been able to reproduce your error. Can you send me the full data set + the code you are running? (though I suspect the same problem above, see the bolded text)

@bwgroff can you also provide your dataset, or check for a constant column like above?

CamDavidsonPilon avatar Dec 14 '16 02:12 CamDavidsonPilon

i have the same problem. when i fit all dataset i get true result. but if i devide dataset into 2 samples (test and train) I catch a error delta contains nan value(s). Convergence halted.

modvala avatar Dec 14 '16 09:12 modvala

https://github.com/CamDavidsonPilon/lifelines/pull/265 <-- that should help users identify their problem.

CamDavidsonPilon avatar Dec 14 '16 14:12 CamDavidsonPilon

@CamDavidsonPilon You were right, I had columns that were constant across the particular subset of data I'm working on. Thanks!

bwgroff avatar Dec 14 '16 14:12 bwgroff