-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support rank deficiency in MixedModels.jl extension (#66)
* ignore version specific manifest * support rank deficiency in MixedModels extension * version bump * YAS * test that shows we're aware of weirdness and it's consistent
- Loading branch information
Showing
5 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,9 +1,32 @@ | ||
module EffectsMixedModelsExt | ||
|
||
using DataFrames | ||
using Effects | ||
using MixedModels | ||
using StatsBase | ||
|
||
using Effects: typify, _difference_method!, _responsename | ||
using LinearAlgebra: diag | ||
using StatsModels: modelcols | ||
using GLM: Link | ||
|
||
Effects._model_link(m::GeneralizedLinearMixedModel, ::AutoInvLink) = Link(m) | ||
|
||
function Effects.effects!(reference_grid::DataFrame, model::MixedModel; | ||
eff_col=nothing, err_col=:err, typical=mean, invlink=identity, | ||
vcov=StatsBase.vcov) | ||
# right now this is written for a RegressionModel and implicitly assumes | ||
# the existence of an appropriate formula method | ||
form = formula(model) | ||
form_typical = typify(reference_grid, form, modelmatrix(model); typical=typical) | ||
piv = view(model.feterm.piv, 1:(model.feterm.rank)) | ||
X = view(modelcols(form_typical, reference_grid), :, piv) | ||
eff = X * fixef(model) | ||
err = sqrt.(diag(X * vcov(model)[piv, piv] * X')) | ||
_difference_method!(eff, err, model, invlink) | ||
reference_grid[!, something(eff_col, _responsename(model))] = eff | ||
reference_grid[!, err_col] = err | ||
return reference_grid | ||
end | ||
|
||
end # module |
This file contains 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,35 @@ | ||
using DataFrames | ||
using Effects | ||
using MixedModels | ||
using StableRNGs | ||
using Suppressor | ||
using Test | ||
|
||
rng = StableRNG(42) | ||
x = rand(rng, 100) | ||
data = (x=x, x2=1.5 .* x, y=rand(rng, [0, 1], 100), z=repeat('A':'T', 5)) | ||
model = @suppress fit(MixedModel, @formula(y ~ x + x2 + (1 | z)), data; progress=false) | ||
dropped_idx = model.feterm.piv[end] | ||
dropped_coef = coefnames(model)[dropped_idx] | ||
kept_coef = last(fixefnames(model)) | ||
|
||
# due to the vagaries of numerical linear algebra and pivoting, it's | ||
# not completely deterministic which of x and x2 gets dropped, though it | ||
# will tend to be x2 | ||
design = Dict(Symbol(kept_coef) => [0]) | ||
eff = effects(design, model) | ||
@test dropped_coef ∉ names(eff) | ||
@test kept_coef ∈ names(eff) | ||
@test !isnan(only(eff.err)) | ||
@test eff.y - eff.err ≈ eff.lower | ||
@test eff.y + eff.err ≈ eff.upper | ||
|
||
# there is one bit of weirdness -- the removed coefficients behave like any other | ||
# variable in the "design" that is missing from the model. | ||
design = Dict(Symbol(dropped_coef) => [0]) | ||
eff_dropped = effects(design, model) | ||
|
||
design = Dict(:q => [0]) | ||
eff_not_present = effects(design, model) | ||
|
||
@test Matrix(eff_dropped) ≈ Matrix(eff_not_present) |
This file contains 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
9e70180
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
9e70180
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/103256
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: