From d978dd065602a367dde27b63eab6f3fbffbe2ffe Mon Sep 17 00:00:00 2001 From: Andreas Noack Date: Thu, 21 Nov 2024 15:38:29 +0100 Subject: [PATCH] Use Julia's standard debug loggin mechanism instead of the custom verbose flag. --- docs/src/index.md | 14 ++++++++++++++ src/glmfit.jl | 11 ++++------- src/negbinfit.jl | 10 ++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 65fd104f..396666ab 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -256,3 +256,17 @@ of a ```LinPred``` type use a default step factor of 1. The value of ```linpred``` is the argument to the ```updatemu``` method for ```ModResp``` types. The ```updatemu``` method returns the updated deviance. + +## Debugging failed fits +In the rare cases when a fit of a generalized linear model fails, it can be useful +to enable more output from the fitting steps. This can be done through +the Julia logging mechanism by setting `ENV[JULIA_DEBUG] = GLM`. Enabling debug output +will result in ouput like the following +```julia +┌ Debug: Iteration: 1, deviance: 5.129147109764238, diff.dev.:0.05057195315968688 +└ @ GLM ~/.julia/dev/GLM/src/glmfit.jl:418 +┌ Debug: Iteration: 2, deviance: 5.129141077001254, diff.dev.:6.032762984276019e-6 +└ @ GLM ~/.julia/dev/GLM/src/glmfit.jl:418 +┌ Debug: Iteration: 3, deviance: 5.129141077001143, diff.dev.:1.1102230246251565e-13 +└ @ GLM ~/.julia/dev/GLM/src/glmfit.jl:418 +``` diff --git a/src/glmfit.jl b/src/glmfit.jl index ce6a247d..256dc040 100644 --- a/src/glmfit.jl +++ b/src/glmfit.jl @@ -353,7 +353,7 @@ end dof(obj::GeneralizedLinearModel) = linpred_rank(obj) + dispersion_parameter(obj.rr.d) -function _fit!(m::AbstractGLM, verbose::Bool, maxiter::Integer, minstepfac::Real, +function _fit!(m::AbstractGLM, maxiter::Integer, minstepfac::Real, atol::Real, rtol::Real, start) # Return early if model has the fit flag set @@ -415,7 +415,7 @@ function _fit!(m::AbstractGLM, verbose::Bool, maxiter::Integer, minstepfac::Real p.beta0 .+= p.delbeta .* f # Test for convergence - verbose && println("Iteration: $i, deviance: $dev, diff.dev.:$(devold - dev)") + @debug "Iteration: $i, deviance: $dev, diff.dev.:$(devold - dev)" if devold - dev < max(rtol*devold, atol) cvg = true break @@ -429,7 +429,6 @@ function _fit!(m::AbstractGLM, verbose::Bool, maxiter::Integer, minstepfac::Real end function StatsBase.fit!(m::AbstractGLM; - verbose::Bool=false, maxiter::Integer=30, minstepfac::Real=0.001, atol::Real=1e-6, @@ -461,14 +460,13 @@ function StatsBase.fit!(m::AbstractGLM; m.atol = atol m.rtol = rtol - _fit!(m, verbose, maxiter, minstepfac, atol, rtol, start) + _fit!(m, maxiter, minstepfac, atol, rtol, start) end function StatsBase.fit!(m::AbstractGLM, y; wts=nothing, offset=nothing, - verbose::Bool=false, maxiter::Integer=30, minstepfac::Real=0.001, atol::Real=1e-6, @@ -509,7 +507,7 @@ function StatsBase.fit!(m::AbstractGLM, m.atol = atol m.rtol = rtol if dofit - _fit!(m, verbose, maxiter, minstepfac, atol, rtol, start) + _fit!(m, maxiter, minstepfac, atol, rtol, start) else m end @@ -531,7 +529,6 @@ const FIT_GLM_DOC = """ $COMMON_FIT_KWARGS_DOCS - `offset::Vector=similar(y,0)`: offset added to `Xβ` to form `eta`. Can be of length 0 - - `verbose::Bool=false`: Display convergence information for each iteration - `maxiter::Integer=30`: Maximum number of iterations allowed to achieve convergence - `atol::Real=1e-6`: Convergence is achieved when the relative change in deviance is less than `max(rtol*dev, atol)`. diff --git a/src/negbinfit.jl b/src/negbinfit.jl index c73dd655..ce2ab001 100644 --- a/src/negbinfit.jl +++ b/src/negbinfit.jl @@ -65,7 +65,6 @@ In both cases, `link` may specify the link function - `maxiter::Integer=30`: See `maxiter` for [`glm`](@ref) - `atol::Real=1.0e-6`: See `atol` for [`glm`](@ref) - `rtol::Real=1.0e-6`: See `rtol` for [`glm`](@ref) -- `verbose::Bool=false`: See `verbose` for [`glm`](@ref) """ function negbin(F, D, @@ -78,7 +77,6 @@ function negbin(F, minstepfac::Real=0.001, atol::Real=1e-6, rtol::Real=1.e-6, - verbose::Bool=false, kwargs...) if haskey(kwargs, :maxIter) Base.depwarn("'maxIter' argument is deprecated, use 'maxiter' instead", :negbin) @@ -109,11 +107,11 @@ function negbin(F, if isinf(initialθ) regmodel = glm(F, D, Poisson(), args...; wts=wts, dropcollinear=dropcollinear, method=method, maxiter=maxiter, - atol=atol, rtol=rtol, verbose=verbose, kwargs...) + atol=atol, rtol=rtol, kwargs...) else regmodel = glm(F, D, NegativeBinomial(initialθ), args...; wts=wts, dropcollinear=dropcollinear, method=method, maxiter=maxiter, - atol=atol, rtol=rtol, verbose=verbose, kwargs...) + atol=atol, rtol=rtol, kwargs...) end μ = regmodel.rr.mu @@ -136,10 +134,10 @@ function negbin(F, converged = true break end - verbose && println("[ Alternating iteration ", i, ", θ = ", θ, " ]") + @debug "[ Alternating iteration $i, θ = $θ ]" regmodel = glm(F, D, NegativeBinomial(θ), args...; dropcollinear=dropcollinear, method=method, maxiter=maxiter, - atol=atol, rtol=rtol, verbose=verbose, kwargs...) + atol=atol, rtol=rtol, kwargs...) μ = regmodel.rr.mu prevθ = θ θ = mle_for_θ(y, μ, wts; maxiter=maxiter, tol=rtol)