Skip to content

Commit 8f55718

Browse files
authored
Better loglikelihood for GLMM (#419)
* return the dispersion for varest of GLMMs with a dispersion param * export additional links * new computation of loglikelihood for GLMM that works with all families * sdest for GLMM * fix sdest * fix loglik calc * change GLMM objective to use NLopt's tracking of function calls (like in LMM)
1 parent 6f80d5b commit 8f55718

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/MixedModels.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export @formula,
4444
GeneralizedLinearMixedModel,
4545
HelmertCoding,
4646
HypothesisCoding,
47+
IdentityLink,
4748
InverseGaussian,
4849
InverseLink,
4950
LinearMixedModel,
@@ -54,6 +55,7 @@ export @formula,
5455
Normal,
5556
OptSummary,
5657
Poisson,
58+
ProbitLink,
5759
RaggedArray,
5860
RandomEffectsTerm,
5961
ReMat,

src/generalizedlinearmixedmodel.jl

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,15 @@ function fit!(
264264
optsum.final = copy(optsum.initial)
265265
end
266266
setpar! = fast ? setθ! : setβθ!
267-
feval = 0
268267
function obj(x, g)
269-
isempty(g) || error("gradient not defined for this model")
270-
feval += 1
268+
isempty(g) || throw(ArgumentError("g should be empty for this objective"))
271269
val = deviance(pirls!(setpar!(m, x), fast, verbose), nAGQ)
272-
feval == 1 && (optsum.finitial = val)
273-
if verbose
274-
println("f_", feval, ": ", val, " ", x)
275-
end
270+
verbose && println(round(val, digits = 5), " ", x)
276271
val
277272
end
278273
opt = Opt(optsum)
279274
NLopt.min_objective!(opt, obj)
275+
optsum.finitial = obj(optsum.initial, T[])
280276
fmin, xmin, ret = NLopt.optimize(opt, copyto!(optsum.final, optsum.initial))
281277
## check if very small parameter values bounded below by zero can be set to zero
282278
xmin_ = copy(xmin)
@@ -294,7 +290,7 @@ function fit!(
294290
## ensure that the parameter values saved in m are xmin
295291
pirls!(setpar!(m, xmin), fast, verbose)
296292
optsum.nAGQ = nAGQ
297-
optsum.feval = feval
293+
optsum.feval = opt.numevals
298294
optsum.final = xmin
299295
optsum.fmin = fmin
300296
optsum.returnvalue = ret
@@ -436,17 +432,27 @@ getθ(m::GeneralizedLinearMixedModel) = copy(m.θ)
436432
getθ!(v::AbstractVector{T}, m::GeneralizedLinearMixedModel{T}) where {T} = copyto!(v, m.θ)
437433

438434
function StatsBase.loglikelihood(m::GeneralizedLinearMixedModel{T}) where {T}
439-
r = m.resp
440-
D = Distribution(m.resp)
441-
accum = (
442-
if D <: Binomial
443-
sum(logpdf(D(round(Int, n), μ), round(Int, y * n))
444-
for (μ, y, n) in zip(r.mu, r.y, m.wt))
445-
else
446-
sum(logpdf(D(μ), y) for (μ, y) in zip(r.mu, r.y))
435+
accum = zero(T)
436+
# adapted from GLM.jl
437+
# note the use of loglik_obs to handle the different parameterizations
438+
# of various response distributions which may not just be location+scale
439+
r = m.resp
440+
wts = r.wts
441+
y = r.y
442+
mu = r.mu
443+
d = r.d
444+
if length(wts) == length(y)
445+
ϕ = deviance(r)/sum(wts)
446+
@inbounds for i in eachindex(y, mu, wts)
447+
accum += GLM.loglik_obs(d, y[i], mu[i], wts[i], ϕ)
447448
end
448-
)
449-
accum - (sum(sum(abs2, u) for u in m.u) + logdet(m)) / 2
449+
else
450+
ϕ = deviance(r)/length(y)
451+
@inbounds for i in eachindex(y, mu)
452+
accum += GLM.loglik_obs(d, y[i], mu[i], 1, ϕ)
453+
end
454+
end
455+
accum - (mapreduce(u -> sum(abs2, u), +, m.u) + logdet(m)) / 2
450456
end
451457

452458
StatsBase.nobs(m::GeneralizedLinearMixedModel) = length(m.η)
@@ -636,7 +642,7 @@ which returns `1` for models without a dispersion parameter.
636642
637643
For Gaussian models, this parameter is often called σ.
638644
"""
639-
sdest(m::GeneralizedLinearMixedModel{T}) where {T} = dispersion_parameter(m) ? dispersion(m, true) : missing
645+
sdest(m::GeneralizedLinearMixedModel{T}) where {T} = dispersion_parameter(m) ? dispersion(m, false) : missing
640646

641647
function Base.show(io::IO, m::GeneralizedLinearMixedModel)
642648
if m.optsum.feval < 0

0 commit comments

Comments
 (0)