-
Notifications
You must be signed in to change notification settings - Fork 4
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
added delta method for glm links #37
Merged
palday
merged 15 commits into
beacon-biosignals:main
from
spinkney:feature-transformed-effects
Mar 24, 2022
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9935032
add delta method for glm links
spinkney fe0c2d0
add ForwardDiff to deps
palday 35c1db5
rename inverse link
palday c275951
fix math; add tests
palday 2f07233
actually run tests
palday 3bc3ce7
JuliaFormatter
palday bac44ed
more tests
palday b9399e1
more docstring
palday 3fed5fb
typo
palday 00ed89d
JuliaFormatter
palday 492a3ff
identity by any other name
palday 114fea6
Update src/regressionmodel.jl
palday fb38da4
Apply suggestions from code review
palday 256ae4d
patch bump
palday 1d72d16
Merge branch 'feature-transformed-effects' of github.com:spinkney/Eff…
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 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
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,107 @@ | ||
using DataFrames | ||
using Effects | ||
using GLM | ||
using LinearAlgebra | ||
using RDatasets: dataset as rdataset | ||
using StableRNGs | ||
using Test | ||
|
||
@testset "transformed response" begin | ||
dat = rdataset("car", "Prestige") | ||
model = lm(@formula(log(Prestige) ~ 1 + Income * Education), dat) | ||
design = Dict(:Income => [1, 2], | ||
:Education => [3, 4]) | ||
eff_original_scale = effects(design, model; invlink=exp) | ||
eff_logscale = effects(design, model) | ||
@test all(eff_logscale[!, 3] .≈ log.(eff_original_scale[!, 3])) | ||
# the derivative of the exponential function is the exponential function.... | ||
deriv = exp.(eff_logscale[!, 3]) | ||
err = eff_logscale.err .* deriv | ||
@test all(eff_original_scale.err .≈ err) | ||
|
||
# compare with results from emmeans in R | ||
# relatively high tolerances for the point estimates b/c that's very susceptible | ||
# to variation in the coef estimates, but the vcov estimates are more stable | ||
# emmeans(model, ~ income * education, level=0.68) | ||
eff_emm = effects(Dict(:Income => [6798], :Education => [10.7]), model; | ||
eff_col="log(Prestige)") | ||
@test isapprox(only(eff_emm[!, "log(Prestige)"]), 3.84; atol=0.01) | ||
@test isapprox(only(eff_emm.err), 0.023; atol=0.005) | ||
@test isapprox(only(eff_emm.lower), 3.81; atol=0.005) | ||
@test isapprox(only(eff_emm.upper), 3.86; atol=0.005) | ||
|
||
# emmeans(model, ~ income * education, level=0.68, transform="response") | ||
eff_emm_trans = effects(Dict(:Extraversion => [12.4], :Neuroticism => [11.5]), model; | ||
invlink=exp, eff_col="Prestige") | ||
@test isapprox(only(eff_emm_trans[!, "Prestige"]), 46.4; atol=0.05) | ||
@test isapprox(only(eff_emm_trans.err), 1.07; atol=0.005) | ||
@test isapprox(only(eff_emm_trans.lower), 45.3; atol=0.05) | ||
@test isapprox(only(eff_emm_trans.upper), 47.5; atol=0.05) | ||
end | ||
|
||
@testset "link function" begin | ||
dat = rdataset("car", "Cowles") | ||
dat[!, :vol] = dat.Volunteer .== "yes" | ||
model = glm(@formula(vol ~ Extraversion * Neuroticism), dat, Bernoulli()) | ||
invlink = Base.Fix1(GLM.linkinv, Link(model.model)) | ||
design = Dict(:Extraversion => [13], | ||
:Neuroticism => [16]) | ||
eff = effects(design, model; invlink) | ||
X = [1.0 13.0 16.0 13 * 16] | ||
# compare with results from GLM.predict | ||
pred = DataFrame(predict(model.model, X; | ||
interval=:confidence, | ||
interval_method=:delta, | ||
level=0.68)) # 0.68 is 1 normal quantile, which is just the SE | ||
@test all(pred.prediction .≈ eff.vol) | ||
@test all(isapprox.(pred.lower, eff.lower; atol=0.001)) | ||
@test all(isapprox.(pred.upper, eff.upper; atol=0.001)) | ||
|
||
eff_trans = effects(design, model) | ||
transform!(eff_trans, | ||
:vol => ByRow(invlink), | ||
:lower => ByRow(invlink), | ||
:upper => ByRow(invlink); renamecols=false) | ||
# for this model, things play out nicely | ||
@test all(eff_trans.vol .≈ eff.vol) | ||
@test all(isapprox.(eff_trans.lower, eff.lower; atol=0.001)) | ||
@test all(isapprox.(eff_trans.upper, eff.upper; atol=0.001)) | ||
|
||
# compare with results from emmeans in R | ||
# emmeans(model, ~ neuroticism * extraversion, level=0.68) | ||
eff_emm = effects(Dict(:Extraversion => [12.4], :Neuroticism => [11.5]), model) | ||
@test isapprox(only(eff_emm.vol), -0.347; atol=0.005) | ||
@test isapprox(only(eff_emm.err), 0.0549; atol=0.005) | ||
@test isapprox(only(eff_emm.lower), -0.402; atol=0.005) | ||
@test isapprox(only(eff_emm.upper), -0.292; atol=0.005) | ||
|
||
# emmeans(model, ~ neuroticism * extraversion, level=0.68, transform="response") | ||
eff_emm_trans = effects(Dict(:Extraversion => [12.4], :Neuroticism => [11.5]), model; | ||
invlink) | ||
@test isapprox(only(eff_emm_trans.vol), 0.414; atol=0.005) | ||
@test isapprox(only(eff_emm_trans.err), 0.0133; atol=0.005) | ||
@test isapprox(only(eff_emm_trans.lower), 0.401; atol=0.005) | ||
@test isapprox(only(eff_emm_trans.upper), 0.427; atol=0.005) | ||
end | ||
|
||
@testset "identity by another name" begin | ||
b0, b1, b2, b1_2 = beta = [0.0, 1.0, 1.0, -1.0] | ||
x = collect(-10:10) | ||
dat = (; :x => x, :y => x .* b1 .+ b0 + randn(StableRNG(42), length(x))) | ||
model = lm(@formula(y ~ x), dat) | ||
|
||
invlink = x -> x # same as identity but won't trigger that branch | ||
@test invlink !== identity | ||
|
||
design = Dict(:x => 1:20) | ||
# if our math on the delta method is correct, then it should work for the | ||
# identity case, even though we special case identity() to reduce the | ||
# computation | ||
eff = effects(design, model; invlink=identity) | ||
eff_link = effects(design, model; invlink) | ||
# these should be exactly equal b/c derivative is just a bunch of ones | ||
# however, we may have to loosen this to approximate equality if | ||
# the linear algebra gets very optimized and we start seeing the effects | ||
# of associativity in SIMD operations | ||
@test eff == eff_link | ||
end |
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
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.
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.
why is this the default when the link can (in principle) be obtained from the model itself? to avoid a dependency on GLM?
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.
Exactly, to avoid the dependency on GLM. As part of the associated docs issue (#38), I think it would be good to have a GLM example. If we decide to take GLM as a dependency, then we should add more specialized methods that take advantage of
GLM.mueta
rather than using AD for the derivative.