Skip to content

ENH: GLM profile confidence intervals - #1791

Open
josef-pkt wants to merge 2 commits into
statsmodels:mainfrom
josef-pkt:confint_profile
Open

josef-pkt wants to merge 2 commits into
statsmodels:mainfrom
josef-pkt:confint_profile

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

This creates profile confidence intervals for individual (single) parameters using offset to fix a parameter.

Example GLM binomial

test case: compares with regular conf_int, only tried for GLM binomial case
see test case for example

@josef-pkt

Copy link
Copy Markdown
Member Author

@kshedden another quick implementation, now that I figured out how to do these things.

profile loglikelihood confidence intervals based on restricting a parameter through offset.
code is again generic, and not optimized for specific models.

I haven't looked at R, and I think Stata doesn't have it.
Do you know if there is something in R to test against?

@kshedden

Copy link
Copy Markdown
Contributor

I found this R package, but I haven't tried it:

http://cran.r-project.org/web/packages/ProfileLikelihood/ProfileLikelihood.pdf

On Thu, Jun 26, 2014 at 11:41 AM, Josef Perktold [email protected]
wrote:

@kshedden https://github.com/kshedden another quick implementation, now
that I figured out how to do these things.

profile loglikelihood confidence intervals based on restricting a
parameter through offset.
code is again generic, and not optimized for specific models.

I haven't looked at R, and I think Stata doesn't have it.
Do you know if there is something in R to test against?

—
Reply to this email directly or view it on GitHub
#1791 (comment)
.

@josef-pkt josef-pkt added this to the 0.7 milestone Aug 17, 2014
@jseabold

Copy link
Copy Markdown
Member

Needs a rebase.

@josef-pkt josef-pkt modified the milestones: 0.8, 0.7 Feb 4, 2015
@josef-pkt josef-pkt removed the PR label Mar 5, 2016
@josef-pkt josef-pkt modified the milestones: 0.9, 0.8 Feb 10, 2017
@josef-pkt

Copy link
Copy Markdown
Member Author

needs more unit tests and better start_params, based on comment in code

for start_params: do I have a function for one-step constraint estimation from unconstrained estimate?

@josef-pkt

Copy link
Copy Markdown
Member Author

about one-step estimate of constrained params, see #3676 (comment) formula in Stata documentation.
(I think same formula as going from OLS to constrained OLS.)

@josef-pkt

Copy link
Copy Markdown
Member Author

rebased (minor merge conflict)

@josef-pkt

Copy link
Copy Markdown
Member Author

Note: we can add consistency cross-checks in unit tests against compare_lr_test (when we have them)

@coveralls

coveralls commented May 20, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.006%) to 90.439% when pulling 8a97470 on josef-pkt:confint_profile into 90e2af8 on statsmodels:master.

offset_model = 0


def llf_p(p1, idx, return_res=False):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

outsource this so we can get the full profile likelihood function as Kerby wanted

from scipy import optimize


def conf_int_profile(results, index=None, alpha=0.05):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

add optional _llf=None for cross checking

@josef-pkt

Copy link
Copy Markdown
Member Author

Another idea as safeguard
We still have no full guarantees on recreating models.
As a cross check we could add an assert that the loglike in the recreated model is the same as llf for the full estimate. The cost would be one additional estimation.

Weakening the requirements: I think we only need deviance in the GLM case. That would avoid computing the, currently missing, loglike in Tweedie.

@josef-pkt

Copy link
Copy Markdown
Member Author

bump to 0.10

@josef-pkt josef-pkt modified the milestones: 0.9, 0.10 Oct 20, 2017
@josef-pkt

Copy link
Copy Markdown
Member Author

rebased, no merge conflicts

@coveralls

coveralls commented Nov 1, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.05%) to 83.569% when pulling 6ec9f26 on josef-pkt:confint_profile into 4124437 on statsmodels:master.

@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #1791 into master will increase coverage by 0.01%.
The diff coverage is 92.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1791      +/-   ##
==========================================
+ Coverage   79.32%   79.33%   +0.01%     
==========================================
  Files         544      545       +1     
  Lines       81718    81771      +53     
  Branches     9255     9261       +6     
==========================================
+ Hits        64822    64873      +51     
  Misses      14802    14802              
- Partials     2094     2096       +2
Impacted Files Coverage Δ
statsmodels/genmod/tests/test_glm.py 98.72% <100%> (+0.01%) ⬆️
statsmodels/genmod/generalized_linear_model.py 89% <100%> (+0.06%) ⬆️
statsmodels/stats/profile_likelihood.py 90.24% <90.24%> (ø)
statsmodels/tsa/vector_ar/vecm.py 93.87% <0%> (+0.24%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b8f20d...eeaed7d. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

rebased again, no merge conflict

@josef-pkt

Copy link
Copy Markdown
Member Author

travis 2.7 has early stopping as in #4716
I guess it is not related to either PR.

@josef-pkt

Copy link
Copy Markdown
Member Author

There might be an issue here with fixing the scale, if we don't use MLE for scale.

Maybe we should keep scale fixed at MLE during computation of profile likelihood, unless we consider it as full MLE (with scale estimated by pearson chi2 as approximation).

For dispersion QMLE, e.g. quasi-poisson, we might want to fix excess dispersion scale. I don't remember whether excess dispersion scale is correctly included in log like. It should be since thequackdaddy fixed it.

@josef-pkt

Copy link
Copy Markdown
Member Author

for unit tests against R:

R seems to switch automatically between wald and profile likelihood confint depending on whether MASS is loaded.
to force wald use confint.default

(I guess I had MASS already in my workspace, example for R docs or somewhere (I did this a week ago)

> ldose <- rep(0:5, 2)
> numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
> sex <- factor(rep(c("M", "F"), c(6, 6)))
> SF <- cbind(numdead, numalive = 20 - numdead)
> budworm.lg0 <- glm(SF ~ sex + ldose - 1, family = binomial)
> confint(budworm.lg0)
Waiting for profiling to be done...
           2.5 %    97.5 %
sexF  -4.4581438 -2.613610
sexM  -3.1728745 -1.655117
ldose  0.8228708  1.339058
> confint(budworm.lg0, "ldose")
Waiting for profiling to be done...
    2.5 %    97.5 % 
0.8228708 1.3390581 


> confint.default(budworm.lg0)    # Wald confint
           2.5 %    97.5 %
sexF  -4.3914381 -2.554873
sexM  -3.1279992 -1.616825
ldose  0.8073069  1.321121

@josef-pkt

Copy link
Copy Markdown
Member Author

as an aside:
The labels in summary column in R 2.5 % 97.5 % are not correct if we don't have equal tail confint.
We have the same problem in Results.summary, but in some cases, IIRC, I only use "low" and "upp" as labels, without indicating the probabilities.
(It was nicer in the old results summary with a single header 95% confint, but that doesn't have one header item per column and we needed to change it.)

@jseabold jseabold removed this from the 0.10 milestone Apr 2, 2021
@josef-pkt josef-pkt mentioned this pull request Nov 2, 2021
16 of 20 tasks
@josef-pkt

josef-pkt commented Nov 2, 2021 •

Copy link
Copy Markdown
Member Author

The function is in a new, specific module.
Should be put in a module with larger topic. Current module name is to specific to collect things in there.

optimization needs checking for robustness, convergence problems.

we might need scale options. in GLM

Also needs checking for reusability with other models, e.g. Poisson
The offset trick here will not work for extra_params as in NB, NBP, GPP, and other_mod

model requires offset

@josef-pkt

Copy link
Copy Markdown
Member Author

Converting this to a class would allow us to reuse code to compute the likelihood profile (concentrated likelihood as function of params_i)

  • __init__ does the setup
  • method for profile/concentrated/constrained likelihood given params_i
  • conf_int method
  • method to compute profile likelihood on a prespecified grid
  • maybe plot method, or return pandas DataFrame so users can plot. In plot, we could add extra info like params of original model

converting to class and doing ProfileLikelihood(results, index=i).conf_int(alpha=0.05) will not add much overhead compared to function

@josef-pkt

Copy link
Copy Markdown
Member Author

code sharing with #2722 score confidence intervals

Recreating the model and estimation of offset mode will be common to both.
Main difference is in target statistic LR test versus Score test.

@josef-pkt josef-pkt added this to the 0.14 milestone Feb 13, 2022
@josef-pkt

Copy link
Copy Markdown
Member Author

statsmodels/stats/profile_likelihood.py is not a good location for helper/results function
This function most likely should be private,

base has now _parameter_inference.py with score_test which might be ok as location

@josef-pkt

josef-pkt commented Nov 30, 2022 •

Copy link
Copy Markdown
Member Author

question: Can we use optimizers with bounds to speed this up?
#8317 has some comments, merged in #8545

I'm pushing this to 0.15 together with score_confint and looking at constrained estimation again.
Needs more checking and comparing some methods for performance.

@josef-pkt josef-pkt modified the milestones: 0.14, 0.15 Dec 1, 2022
@josef-pkt josef-pkt mentioned this pull request Dec 1, 2022
6 tasks
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.

5 participants