Skip to content

Commit

Permalink
fix and test invlink for bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
palday committed Jul 9, 2024
1 parent 51e1aab commit a535ff5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ext/EffectsMixedModelsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ using Effects
using MixedModels
using StatsBase

using Effects: typify, _difference_method!, _responsename
using Effects: typify, _difference_method!,
_responsename, _model_link
using LinearAlgebra: diag
using StatsModels: modelcols
using GLM: Link
using GLM: GLM, Link

Effects._model_link(m::GeneralizedLinearMixedModel, ::AutoInvLink) = Link(m)

Expand Down Expand Up @@ -82,8 +83,9 @@ function Effects.effects!(reference_grid::DataFrame, model::MixedModel,
return quantile(col, [lower_tail, upper_tail])
end

reference_grid[!, lower_col] = first.(ci)
reference_grid[!, upper_col] = last.(ci)
invlink = Base.Fix1(GLM.linkinv, _model_link(model, invlink))
reference_grid[!, lower_col] = invlink.(first.(ci))
reference_grid[!, upper_col] = invlink.(last.(ci))
end
return reference_grid
end
Expand Down
16 changes: 16 additions & 0 deletions test/mixedmodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ eff_boot95 = effects(design, model, boot; level=0.95)
@test eff_boot95.y eff95.y
@test eff_boot95.lower eff95.lower atol = 0.1
@test eff_boot95.upper eff95.upper atol = 0.1

glmm = @suppress fit(MixedModel,
@formula(use ~ 1 + age + abs2(age) + livch + urban + (1 | dist)),
MixedModels.dataset(:contra),
Bernoulli(); fast=true)

gboot = parametricbootstrap(StableRNG(345), 250, glmm)

design = Dict(:age => -1:1:1)
geff_boot = effects(design, glmm, gboot; eff_col="y", invlink=AutoInvLink(), level=0.95)
geff = effects(design, glmm; eff_col="y", invlink=AutoInvLink(), level=0.95)

@test geff_boot.y geff.y
@test geff_boot.err geff.err atol = 0.05
@test geff_boot.lower geff.lower atol = 0.05
@test geff_boot.upper geff.upper atol = 0.05

0 comments on commit a535ff5

Please sign in to comment.