Skip to content

Phreg predict - #2924

Open
lbybee wants to merge 25 commits into
statsmodels:mainfrom
lbybee:phreg_predict
Open

lbybee wants to merge 25 commits into
statsmodels:mainfrom
lbybee:phreg_predict

Conversation

@lbybee

@lbybee lbybee commented May 4, 2016

Copy link
Copy Markdown
Contributor

See #2894

This should map to what was mentioned, there are still some issues with the MICE tests but I wanted to get this up.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.02%) to 85.401% when pulling e844b3a on lbybee:phreg_predict into 0fac349 on statsmodels:master.

@kshedden

kshedden commented May 7, 2016

Copy link
Copy Markdown
Contributor

@josef-pkt this looks reasonable to me, what do you think?

@josef-pkt

Copy link
Copy Markdown
Member

2 design question (I don't know or remember many of the model specific details):

  • could _prediction.py use inheritance so we know what's different from the current generic version?
    It's not a big problem but it would make generic enhancement easier in future if there is a more common structure.
  • predict has many different predict_types: Is there much overhead (common calculations) if we want it all? mainly if we want the full insample results. Related, are some of them important enough to add them as cached attributes for insample version?

To the second item: The main idea between the separation of predict and get_prediction is to cater to two different usecases. predict for fast specific results (as in cross-validation) and get_prediction for the full results.
For example get_prediction could create a DataFrame with all results, then we don't want to have a lot of redundant or duplicate computation.

(a bit later)
I started to read through the details of predict, lhr is relatively cheap, but qsurv and cumhaz use both the baseline hazard. I haven't checked whether that one is cheap.
I need to look more closely tomorrow, if the kids leave me some quite time, otherwise I'll look at it beginning next week.

another thing that is bothering me is that the Bunch is using the same attribute predicted_values for different things. I think that will be confusing. On the other hand returning the Bunch is mainly for backwards compatibility. Is it intended to be deprecated and go away?

@kshedden

kshedden commented May 9, 2016

Copy link
Copy Markdown
Contributor

The main reason for returning the bunch was when returning estimates, standard errors, and confidence intervals, without having a long list of return values. If lists of return values are preferred that is OK with me.

@kshedden

kshedden commented May 9, 2016

Copy link
Copy Markdown
Contributor

Trying to establish some analogies between what we are doing with PHReg.predict and predict in other models:

The default return value of PHReg.predict is the log hazard ratio. This is simply the linear predictor so easy to compute. It is monotonically related to the mean and all quantiles of the survival distribution if there is only one stratum. If there are multiple strata then the relationship is only monotone within a stratum.

My main use case for PHReg prediction is MICE. We use PMM in MICE which can match on various things that are monotonically related to the mean or median. Log hazard could be used for MICE imputation as long as there is only one stratum. If more than one stratum this isn't correct, but can be handled on the MICE side using predict_kwds. This PR adds estimation of survival time quntiles which correctly account for different strata. So the MICE user would just need to add pred_type="qsurv" and prob = 0.5 as predict_kwds to get MICE to work with multiple strata.

@kshedden

Copy link
Copy Markdown
Contributor

This PR is also needed for predict_functional with values2/summaries2 to work with PHReg.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 85.436% when pulling 9d77787 on lbybee:phreg_predict into de15ec8 on statsmodels:master.

@lbybee

lbybee commented May 22, 2016

Copy link
Copy Markdown
Contributor Author

Ok, I think what I've done here should take care of some of the concerns. Basically I just modified _prediction in regression so that we can use it for Phreg. This was I was able to get rid of _prediction in duration.

@josef-pkt

Copy link
Copy Markdown
Member

One more question (I lost my comment that I started last week)

What should be the default pred_type?

In general, I prefer that if we standardize to returning the expected value of the response, ie. the mean prediction and not the linear prediction if the conditional expectation is transformed as in GLM type models.
We don't have a proper expected value of the response in the prediction, so this doesn't completely apply in this case. However, wouldn't it be more consistent to have the hazard rate instead of the log hazard rate (linear predictor) as default pred_type?
Is there something that could be interpreted as standard fittedvalues in PHReg?

Comment thread statsmodels/regression/_prediction.py Outdated
var_pred_mean = (exog * np.dot(covb, exog.T).T).sum(1)
# handle var_pred_mean for the PHReg case
if 'pred_type' in pred_kwds:
cov_params = pred_kwds['cov_params']

@josef-pkt josef-pkt May 22, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the usecase for overriding the attached default cov_params?
Is this useful in general, so it should be added as a main keyword option and not just for lhr?

(we allow it in t_test and wald_test, but not in discrete get_margeff margins.)
The old keyword in t_test uses a shorter keyword cov_p=None, I guess from a time when having shorter names was still a stronger consideration.

@lbybee

lbybee commented May 23, 2016

Copy link
Copy Markdown
Contributor Author

@kshedden should probably weigh in on some of this. I'm not complete sure what the use case is for an external cov_params. I'd be comfortable taking it out but I'd want to double check that before I did. Same with adding cov_type.

@josef-pkt

Copy link
Copy Markdown
Member

Note, I'm not against adding cov_p as argument and option for users, but if it's useful, then we add this to get_prediction in general without special casing PHReg.
That's related to my initial question, whether you see some useful changes to the current get_prediction API.

Generic or general PHReg improvements don't need to be in this PR, but then we need to open new issues to work on this later.
(I opened #2963 for refactoring cov_type)

Comment thread statsmodels/regression/_prediction.py Outdated
# handle var_pred_mean for the PHReg case
if 'pred_type' in pred_kwds:
cov_params = pred_kwds['cov_params']
if pred_kwds['pred_type'] == 'lhr':

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lbybee can we avoid referring to lhr (duration-specific term) here in the base prediction code? I think it would be better to have a subclass for phreg, and if pred_type is lhr we just invoke the superclass, otherwise we do phreg-specific things in the subclass.

@kshedden

Copy link
Copy Markdown
Contributor

@lbybee, @josef-pkt in PredictionResults can we add some comments around the lines copied below to define what these things actually mean? For example, is predicted_mean the expected value of the linear predictor (when that makes sense), and not of the response? Do var_resid or df have meaning outside linear regression?

Also, should PredictionResults be privatized as _PredictionResults?

        self.predicted_mean = predicted_mean
        self.var_pred_mean = var_pred_mean
        self.df = df
        self.var_resid = var_resid
        self.row_labels = row_labels

@kshedden

Copy link
Copy Markdown
Contributor

Maybe the general idea here is that the base PredictionResults can handle everything related to prediction for a linear predictor, anything else needs to go in a subclass.

Perhaps the base PredictionResults can handle data transforms (e.g. log(y) ~ x), but not GLM-style link functions, I'm not sure.

@josef-pkt

Copy link
Copy Markdown
Member

a few comments on prediction. It's still not settled, so more usecases and ideas for improvements are still necessary.

  • terminology:
    • predicted_mean is supposed to be conditional expectation E(y | x) for whatever model
    • for the confidence interval and standard error in the linear model we need terminology for the mean versus observation, i.e. a confidence interval for E(y|x) versus confidence interval for a new observation y_i (in the normal case, this just adds the standard error of the residual). In GLM and other nonlinear model this is not directly available
    • in GLM and similar we need to distinguish predicted_mean from the linear prediction.
  • Results classes cannot really be private because we return instances of Results classes to users, and they need to be able to get the docstrings for it.
  • we cannot handle transformed endog yet e.g. log(y) ~ x) because we don't have any information about transformation that have been specified in the formula or done by the user. We have a Box-Cox transformation in a work in progress pull request that will go in this direction.
    However, GLM has a get_prediction version that handles transformation through link functions. IIRC, I only added it to GLM because other models like those in discrete don't have yet explicit link functions available, those are hard coded.
  • related to log/exp transformation. There is some extra code to get exponentiated parameters exp(params) when they have a specific interpretation in the models (Stata's eform). But that's nonlinear parameter transformation and not prediction.

not yet clear to me:

How to we get confidence interval or similar for a new observation with models with a non-additive error like Poisson taking paramer uncertainty into account?
That would be the analog of the prediction interval for a new observation in the normal case.
Right now we have the scipy.stats distribution given the estimated parameters to calculate properties of the conditional distribution for a new observation, but this ignores that we have estimated the parameters.
The only code I wrote but didn't commit to any branch, is using Monte Carlo sampling using the normal distribution for the parameters to get the predictive distribution, a bit similar to what was in MICE. I used the Poisson case to see how this would work.
I don't know in which form we should add this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants