Skip to content

Commit

Permalink
Use Julia's standard debug loggin mechanism instead of the custom
Browse files Browse the repository at this point in the history
verbose flag.
  • Loading branch information
andreasnoack committed Nov 21, 2024
1 parent f6f4f70 commit d978dd0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
14 changes: 14 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
11 changes: 4 additions & 7 deletions src/glmfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Check warning on line 510 in src/glmfit.jl

View check run for this annotation

Codecov / codecov/patch

src/glmfit.jl#L510

Added line #L510 was not covered by tests
else
m
end
Expand All @@ -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)`.
Expand Down
10 changes: 4 additions & 6 deletions src/negbinfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit d978dd0

Please sign in to comment.