-
Notifications
You must be signed in to change notification settings - Fork 1
Bootstrap ICC #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
palday
wants to merge
17
commits into
main
Choose a base branch
from
pa/booticc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Bootstrap ICC #24
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5754487
icc tolerances
palday 204d66a
icc on bootstrap
palday 276f995
patch bump
palday b7cfaa1
Apply suggestions from code review
palday f08fddd
Apply suggestions from code review
palday a7dce02
newline
palday 3ffe49b
bootstrapped LRT
palday e72d16f
varargs
palday 2d4d2ad
tweak
palday 0c68d51
docstring
palday 2eb6c6d
YAS
palday 5098058
try-catch instead of copy?
palday 540285c
d'oh
palday 39e74a2
Merge branch 'main' of github.com:palday/MixedModelsExtras.jl into pa…
palday ae70051
Merge branch 'pa/bootstrap_lrt' of github.com:palday/MixedModelsExtra…
palday 07f0ca1
YAS
palday 7bd4c40
Merge branch 'main' into pa/booticc
palday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """ | ||
| bootstrap_lrt(rng::AbstractRNG, n::Integer, m0::MixedModel, ms::MixedModel...; | ||
| optsum_overrides=(;), progress=true) | ||
|
|
||
| Bootstrapped likelihood ratio test applied to a set of nested models. | ||
|
|
||
| The first model is used to simulate `n` dataset replicates, where the ground truth is that | ||
| specified by the first model. Each of the other models is then refit to those | ||
| null data and the underlying distribution of deviance differences is then captured. | ||
| For final computation of the p-values, the observed deviance is differencce between the | ||
| original models is compared against this null distribution. | ||
|
|
||
| !!! note | ||
| The precision of the resulting p-value cannot exceed ``1/n``. | ||
|
|
||
| !!! warn | ||
| This method is **not** thread safe. For efficiency , the models are modified | ||
| during bootstrapping and the original fits are only restored at the end. | ||
|
|
||
| !!! note | ||
| The nesting of the models is not checked. It is incumbent on the user | ||
| to check this. This differs from `StatsModels.lrtest` as nesting in | ||
| mixed models, especially in the random effects specification, may be non obvious. | ||
|
|
||
| This functionality may be deprecated in the future in favor of `StatsModels.lrtest`. | ||
| """ | ||
| function bootstrap_lrt(rng::AbstractRNG, n::Integer, m0::MixedModel, ms::MixedModel...; | ||
| optsum_overrides=(;), progress=true) | ||
| y0 = copy(response(m0)) | ||
| ys = [copy(response(m)) for m in ms] | ||
| local models | ||
| local devs | ||
| local dofs | ||
| local dofs | ||
| local formulas | ||
| local dofdiffs | ||
| local devdiffs | ||
| local pvals | ||
| try | ||
| models = [m0; ms...] | ||
| dofs = dof.(models) | ||
| formulas = string.(formula.(models)) | ||
| ord = sortperm(dofs) | ||
| dofs = dofs[ord] | ||
| formulas = formulas[ord] | ||
| devs = deviance.(models)[ord] | ||
| dofdiffs = diff(dofs) | ||
| devdiffs = .-(diff(devs)) | ||
|
|
||
| for (key, val) in pairs(optsum_overrides) | ||
| setfield!(m0.optsum, key, val) | ||
| for m in ms | ||
| setfield!(m.optsum, key, val) | ||
| end | ||
| end | ||
| nulldist = replicate(n; hide_progress=!progress) do | ||
| simulate!(rng, m0) | ||
| refit!(m0; progress=false) | ||
| for m in ms | ||
| refit!(m, response(m0); progress=false) | ||
| end | ||
| return [deviance(m) for m in models] | ||
| end | ||
| nulldist = stack(nulldist; dims=1) | ||
| nulldist = diff(nulldist; dims=2) | ||
| pvals = map(enumerate(devdiffs)) do (idx, dev) | ||
| if dev > 0 | ||
| mean(>=(dev), view(nulldist, :, idx)) | ||
| else | ||
| NaN | ||
| end | ||
| end | ||
| catch ex | ||
| rethrow(ex) | ||
| finally | ||
| # restore the original fits | ||
| if progress | ||
| @info "Bootstrapping complete, cleaning up..." | ||
| end | ||
| refit!(m0, y0; progress=false) | ||
| for (m, y) in zip(ms, ys) | ||
| refit!(m, y; progress=false) | ||
| end | ||
| end | ||
| return MixedModels.LikelihoodRatioTest(formulas, | ||
| (dof=dofs, deviance=devs), | ||
| (dofdiff=dofdiffs, deviancediff=devdiffs, | ||
| pvalues=pvals), | ||
| first(models) isa LinearMixedModel) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using MixedModels | ||
| using MixedModelsExtras | ||
| using Random | ||
| using Test | ||
|
|
||
| using MixedModels: likelihoodratiotest | ||
|
|
||
| progress = false | ||
| sleepstudy = MixedModels.dataset(:sleepstudy) | ||
| fm0 = fit(MixedModel, @formula(reaction ~ 1 + days + (1 | subj)), | ||
palday marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sleepstudy; progress) | ||
| fm1 = fit(MixedModel, @formula(reaction ~ 1 + days + (1 + days | subj)), | ||
palday marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sleepstudy; progress) | ||
| fmzc = fit(MixedModel, @formula(reaction ~ 1 + days + zerocorr(1 + days | subj)), | ||
| sleepstudy; progress) | ||
|
|
||
| lrt = likelihoodratiotest(fm0, fm1, fmzc) | ||
| boot = bootstrap_lrt(MersenneTwister(42), 1000, fm0, fm1, fmzc) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.