Skip to content

Taking weighting seriously #487

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

Open
wants to merge 208 commits into
base: master
Choose a base branch
from

Conversation

gragusa
Copy link

@gragusa gragusa commented Jul 15, 2022

This PR addresses several problems with the current GLM implementation.

Current status
In master, GLM/LM only accepts weights through the keyword wts. These weights are implicitly frequency weights.

With this PR
FrequencyWeights, AnalyticWeights, and ProbabilityWeights are possible. The API is the following

## Frequency Weights
lm(@formula(y~x), df; wts=fweights(df.wts)
## Analytic Weights
lm(@formula(y~x), df; wts=aweights(df.wts)
## ProbabilityWeights
lm(@formula(y~x), df; wts=pweights(df.wts)

The old behavior -- passing a vector wts=df.wts is deprecated and for the moment, the array os coerced df.wts to FrequencyWeights.

To allow dispatching on the weights, CholPred takes a parameter T<:AbstractWeights. The unweighted LM/GLM has UnitWeights as the parameter for the type.

This PR also implements residuals(r::RegressionModel; weighted::Bool=false) and modelmatrix(r::RegressionModel; weighted::Bool = false). The new signature for these two methods is pending in StatsApi.

There are many changes that I had to make to make everything work. Tests are passing, but some new feature needs new tests. Before implementing them, I wanted to ensure that the approach taken was liked.

I have also implemented momentmatrix, which returns the estimating function of the estimator. I arrived to the conclusion that it does not make sense to have a keyword argument weighted. Thus I will amend JuliaStats/StatsAPI.jl#16 to remove such a keyword from the signature.

Update

I think I covered all the suggestions/comments with this exception as I have to think about it. Maybe this can be addressed later. The new standard errors (the one for ProbabilityWeights) also work in the rank deficient case (and so does cooksdistance).

Tests are passing and I think they cover everything that I have implemented. Also, added a section in the documentation about using Weights and updated jldoc with the new signature of CholeskyPivoted.

To do:

  • Deal with weighted standard errors with rank deficient designs
  • Document the new API
  • Improve testing

Closes #186, #259.

@codecov-commenter
Copy link

codecov-commenter commented Jul 16, 2022

Codecov Report

Attention: Patch coverage is 95.44419% with 20 lines in your changes missing coverage. Please review.

Project coverage is 95.20%. Comparing base (8f58b34) to head (aff48d6).

Files with missing lines Patch % Lines
src/glmfit.jl 93.00% 10 Missing ⚠️
src/lm.jl 93.13% 7 Missing ⚠️
src/glmtools.jl 92.30% 2 Missing ⚠️
src/negbinfit.jl 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #487      +/-   ##
==========================================
+ Coverage   94.82%   95.20%   +0.37%     
==========================================
  Files           8        8              
  Lines        1044     1251     +207     
==========================================
+ Hits          990     1191     +201     
- Misses         54       60       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lrnv
Copy link

lrnv commented Jul 20, 2022

Hey,

Would that fix the issue I am having, which is that if rows of the data contains missing values, GLM discard those rows, but does not discard the corresponding values of df.weights and then yells that there are too many weights ?

I think the interfacing should allow for a DataFrame input of weights, that would take care of such things (like it does for the other variables).

@gragusa
Copy link
Author

gragusa commented Jul 20, 2022

Would that fix the issue I am having, which is that if rows of the data contains missing values, GLM discard those rows, but does not discard the corresponding values of df.weights and then yells that there are too many weights ?

not really. But it would be easy to make this a feature. But before digging further on this I would like to know whether there is consensus on the approach of this PR.

@alecloudenback
Copy link

alecloudenback commented Aug 14, 2022

FYI this appears to fix #420; a PR was started in #432 and the author closed for lack of time on their part to investigate CI failures.

Here's the test case pulled from #432 which passes with the in #487.

@testset "collinearity and weights" begin
    rng = StableRNG(1234321)
    x1 = randn(100)
    x1_2 = 3 * x1
    x2 = 10 * randn(100)
    x2_2 = -2.4 * x2
    y = 1 .+ randn() * x1 + randn() * x2 + 2 * randn(100)
    df = DataFrame(y = y, x1 = x1, x2 = x1_2, x3 = x2, x4 = x2_2, weights = repeat([1, 0.5],50))
    f = @formula(y ~ x1 + x2 + x3 + x4)
    lm_model = lm(f, df, wts = df.weights)#, dropcollinear = true)
    X = [ones(length(y)) x1_2 x2_2]
    W = Diagonal(df.weights)
    coef_naive = (X'W*X)\X'W*y
    @test lm_model.model.pp.chol isa CholeskyPivoted
    @test rank(lm_model.model.pp.chol) == 3
    @test isapprox(filter(!=(0.0), coef(lm_model)), coef_naive)
end

Can this test set be added?

Is there any other feedback for @gragusa ? It would be great to get this merged if good to go.

@nalimilan
Copy link
Member

Sorry for the long delay, I hadn't realized you were waiting for feedback. Looks great overall, please feel free to finish it! I'll try to find the time to make more specific comments.

Copy link
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

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

I've read the code. Lots of comments, but all of these are minor. The main one is mostly stylistic: in most cases it seems that using if wts isa UnitWeights inside a single method (like the current structure) gives simpler code than defining several methods. Otherwise the PR looks really clean!

What are you thoughts regarding testing? There are a lot of combinations to test and it's not easy to see how to integrate that into the current organization of tests. One way would be to add code for each kind of test to each @testset that checks a given model family (or a particular case, like collinear variables). There's also the issue of testing the QR factorization, which isn't used by default.

@bkamins
Copy link
Contributor

bkamins commented Aug 31, 2022

A very nice PR. In the tests can we have some test set that compares the results of aweights, fweights, and pweights for the same set of data (coeffs, predictions, covariance matrix of the estimates, p-values etc.).

@gragusa
Copy link
Author

gragusa commented Apr 29, 2025

@nalimilan @ajinkya-k all tests pass.. (The two failures are due to HTTPS failures.) What is still needed?

@nalimilan
Copy link
Member

Thanks. A few uncovered lines really still need testing. I also think some of our comments haven't been addressed yet (I can check if you don't find them).

src/lm.jl Outdated
@@ -178,7 +178,7 @@ function fit(::Type{LinearModel}, X::AbstractMatrix{<:Real}, y::AbstractVector{<
dropcollinear::Bool=true, method::Symbol=:qr)
# For backward compatibility accept wts as AbstractArray and coerce them to FrequencyWeights
_wts = convert_weights(wts)
if !(wts isa AbstractWeights && isempty(_wts))
Copy link
Member

Choose a reason for hiding this comment

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

Did you revert this because it doesn't work?

end
@inbounds for i in eachindex(y, mu, wts)
ll += loglik_apweights_obs(d, y[i], mu[i], wts[i], δ, sum(wts), N)
Copy link
Member

Choose a reason for hiding this comment

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

This should be tested.

# For backward compatibility accept wts as AbstractArray and coerce them to FrequencyWeights
_wts = convert_weights(wts)
if !(wts isa AbstractWeights) && isempty(_wts)
Base.depwarn("Using `wts` of zero length for unweighted regression is deprecated in favor of " *
Copy link
Member

Choose a reason for hiding this comment

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

Can you test this too?

function loglikelihood(r::LmResp{T,<:AnalyticWeights}) where {T}
N = length(r.y)
n = sum(log, weights(r))
return (n - N * (log(2π * deviance(r) / N) + 1)) / 2
Copy link
Member

Choose a reason for hiding this comment

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

Needs testing too.

:fit)
fweights(wts)
else
throw(ArgumentError("`wts` should be an `AbstractVector` coercible to `AbstractWeights`"))
Copy link
Member

Choose a reason for hiding this comment

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

Also worth testing.

return wt * logpdf(Gamma(inv(ϕ / sumwt), μ * ϕ / sumwt), y)
end
function loglik_apweights_obs(::Geometric, y, μ, wt, ϕ, sumwt, n)
return wt * logpdf(Geometric(1 / (μ + 1)), y)
Copy link
Member

Choose a reason for hiding this comment

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

Also test this.

Copy link
Contributor

@ajinkya-k ajinkya-k left a comment

Choose a reason for hiding this comment

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

I may have missed some conversation but it should be possible to use multiple dispatch instead of using loglik_aweights_obs right?

## sumwt is sum(wt)
## n is the number of observations

function loglik_apweights_obs(::Gamma, y, μ, wt, ϕ, sumwt, n)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
function loglik_apweights_obs(::Gamma, y, μ, wt, ϕ, sumwt, n)
function loglik_obs(::Gamma, y, μ, wt::AnalyticWeights, ϕ, sumwt, n)

Copy link
Contributor

Choose a reason for hiding this comment

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

also the same in other places below

@gragusa
Copy link
Author

gragusa commented Apr 30, 2025

Thanks. A few uncovered lines really still need testing. I also think some of our comments haven't been addressed yet (I can check if you don't find them).

I don't know - there are so many comments that I cannot find anything among them.

@nalimilan
Copy link
Member

Yeah, GitHub makes it painful to find them, especially as you have to click many times to expand hidden comments. But unresolved comments are still there. Here's a list:
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)
#487 (comment)

@@ -187,7 +223,7 @@ function delbeta!(p::DensePredChol{T,<:CholeskyPivoted}, r::Vector{T}) where {T<
return p
end

function delbeta!(p::DensePredChol{T,<:Cholesky}, r::Vector{T},
function delbeta!(p::DensePredChol{T,<:Cholesky,<:AbstractWeights}, r::Vector{T},
Copy link
Member

Choose a reason for hiding this comment

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

Adding <:AbstractWeights doesn't seem necessary here nor below?

Co-authored-by: Milan Bouchet-Valat <[email protected]>
@nalimilan
Copy link
Member

Something I forgot: at #350 we wanted to rename the wts argument to weights. Can be done in a separate PR though if you prefer.

@ajinkya-k
Copy link
Contributor

Something I forgot: at #350 we wanted to rename the wts argument to weights. Can be done in a separate PR though if you prefer.

I think this should be in a different PR given that this one is huge already 😅

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.

Path towards GLMs with fweights, pweights, and aweights