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

Recover from PIRLS failure by returning finitial #616

Merged
merged 12 commits into from
Aug 9, 2023
10 changes: 9 additions & 1 deletion src/generalizedlinearmixedmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,15 @@ function StatsAPI.fit!(
fitlog = optsum.fitlog
function obj(x, g)
isempty(g) || throw(ArgumentError("g should be empty for this objective"))
val = deviance(pirls!(setpar!(m, x), fast, verbose), nAGQ)
val = try
deviance(pirls!(setpar!(m, x), fast, verbose), nAGQ)
catch ex
# this allows us to recover from models where e.g. the link isn't
# as constraining as it should be
ex isa Union{PosDefException,DomainError} || rethrow()
iter == 1 && rethrow()
m.optsum.finitial
end
iszero(rem(iter, thin)) && push!(fitlog, (copy(x), val))
verbose && println(round(val; digits=5), " ", x)
progress && ProgressMeter.next!(prog; showvalues=[(:objective, val)])
Expand Down
6 changes: 5 additions & 1 deletion test/pirls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ end
center(v::AbstractVector) = v .- (sum(v) / length(v))
grouseticks = DataFrame(dataset(:grouseticks))
grouseticks.ch = center(grouseticks.height)
gm4 = fit(MixedModel, only(gfms[:grouseticks]), grouseticks, Poisson(); fast=true, progress=false) # fails in pirls! with fast=false
gm4 = fit(MixedModel, only(gfms[:grouseticks]), grouseticks, Poisson(); fast=true, progress=false)
@test isapprox(deviance(gm4), 851.4046, atol=0.001)
# these two values are not well defined at the optimum
#@test isapprox(sum(x -> sum(abs2, x), gm4.u), 196.8695297987013, atol=0.1)
Expand All @@ -173,6 +173,10 @@ end
@test sdest(gm4) === missing
@test varest(gm4) === missing
@test gm4.σ === missing
gm4slow = fit(MixedModel, only(gfms[:grouseticks]), grouseticks, Poisson(); fast=false, progress=false)
# this tolerance isn't great, but then again the optimum isn't well defined
@test gm4.θ ≈ gm4slow.θ atol=0.05
@test gm4.β[2:end] ≈ gm4slow.β[2:end] atol=0.1
end

@testset "goldstein" begin # from a 2020-04-22 msg by Ben Goldstein to R-SIG-Mixed-Models
Expand Down