Skip to content

Commit c8261be

Browse files
committed
new computation of loglikelihood for GLMM that works with all families
1 parent 2760775 commit c8261be

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/generalizedlinearmixedmodel.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,27 @@ end
402402

403403
function StatsBase.loglikelihood(m::GeneralizedLinearMixedModel{T}) where {T}
404404
accum = zero(T)
405-
D = Distribution(m.resp)
406-
if D <: Binomial
407-
for (μ, y, n) in zip(m.resp.mu, m.resp.y, m.wt)
408-
accum += logpdf(D(round(Int, n), μ), round(Int, y * n))
405+
# adapted from GLM.jl
406+
# note the use of loglik_obs to handle the different parameterizations
407+
# of various response distributions which may not just be location+scale
408+
r = m.resp
409+
wts = r.wts
410+
y = r.y
411+
mu = r.mu
412+
d = r.d
413+
if length(wts) == length(y)
414+
# in GLM.jl, they use the deviance of the
415+
ϕ = deviance(r)/sum(wts)
416+
@inbounds for i in eachindex(y, mu, wts)
417+
accum += GLM.loglik_obs(d, y[i], mu[i], wts[i], ϕ)
409418
end
410419
else
411-
for (μ, y) in zip(m.resp.mu, m.resp.y)
412-
accum += logpdf(D(μ), y)
420+
ϕ = deviance(r)/length(y)
421+
@inbounds for i in eachindex(y, mu)
422+
accum += GLM.loglik_obs(d, y[i], mu[i], 1, ϕ)
413423
end
414424
end
415-
accum - (mapreduce(u -> sum(abs2, u), +, m.u) + logdet(m)) / 2
425+
accum
416426
end
417427

418428
StatsBase.nobs(m::GeneralizedLinearMixedModel) = length(m.η)

0 commit comments

Comments
 (0)