Skip to content
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

Backtrack on non-finite deviance values #577

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/glmfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ function _fit!(m::AbstractGLM, verbose::Bool, maxiter::Integer, minstepfac::Real
updateμ!(r, lp)
end
devold = deviance(m)
if !isfinite(devold)
throw(DomainError(devold, "initial deviance was not finite. Try alternative starting values or model formulation"))
end

for i = 1:maxiter
f = 1.0 # line search factor
Expand All @@ -402,7 +405,7 @@ function _fit!(m::AbstractGLM, verbose::Bool, maxiter::Integer, minstepfac::Real
## If the deviance isn't declining then half the step size
## The rtol*dev term is to avoid failure when deviance
## is unchanged except for rouding errors.
while dev > devold + rtol*dev
while !isfinite(dev) || dev > devold + rtol*dev
andreasnoack marked this conversation as resolved.
Show resolved Hide resolved
f /= 2
f > minstepfac || error("step-halving failed at beta0 = $(p.beta0)")
try
Expand Down
13 changes: 12 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2032,4 +2032,15 @@ end
f2 = lm(X, filip_data_df.y, dropcollinear = false, method = :qr, wts = ones(length(filip_data_df.y)))
@test coef(f2) ≈ filip_estimates_df.estimate rtol = 1e-7
@test stderror(f2) ≈ filip_estimates_df.se rtol = 1e-7
end
end

@testset "Non-finite deviance. Issue 417" begin
X_fn = Downloads.download("https://github.com/JuliaStats/GLM.jl/files/6279630/x.txt")
y_fn = Downloads.download("https://github.com/JuliaStats/GLM.jl/files/6279632/y.txt")
X_df = CSV.read(X_fn, DataFrame, header = false)
y_df = CSV.read(y_fn, DataFrame, header = false)
df = hcat(y_df, select(X_df, Not("Column1")))
ft = glm(@formula(Column1 ~ Column2 + Column3 + Column4), df, Gamma(), LogLink())
@test coef(ft) ≈ [9.648767705301294, -0.11274823562143056, 0.1907889126252095, -0.8123086879222496]
@test_throws DomainError glm(@formula(Column1 ~ Column2 + Column3 + Column4), df, Gamma(), LogLink(), start = fill(NaN, 4))
end
Loading