@@ -68,7 +68,7 @@ function StatsBase.deviance(m::GeneralizedLinearMixedModel{T}, nAGQ=1) where {T}
6868 u = vec (first (m. u))
6969 u₀ = vec (first (m. u₀))
7070 copyto! (u₀, u)
71- ra = RaggedArray (m. resp. devresid, first (m. LMM. reterms ). refs)
71+ ra = RaggedArray (m. resp. devresid, first (m. LMM. allterms ). refs)
7272 devc0 = sum! (map! (abs2, m. devc0, u), ra) # the deviance components at z = 0
7373 sd = map! (inv, m. sd, m. LMM. L[Block (1 ,1 )]. diag)
7474 mult = fill! (m. mult, 0 )
@@ -105,8 +105,17 @@ function deviance!(m::GeneralizedLinearMixedModel, nAGQ=1)
105105 deviance (m, nAGQ)
106106end
107107
108- GLM. dispersion (m:: GeneralizedLinearMixedModel , sqr:: Bool = false ) =
109- dispersion (m. resp, dof_residual (m), sqr)
108+ function GLM. dispersion (m:: GeneralizedLinearMixedModel{T} , sqr:: Bool = false ) where {T}
109+ # adapted from GLM.dispersion(::AbstractGLM, ::Bool)
110+ # TODO : PR for a GLM.dispersion(resp::GLM.GlmResp, dof_residual::Int, sqr::Bool)
111+ r = m. resp
112+ if dispersion_parameter (r. d)
113+ s = sum (wt * abs2 (re) for (wt, re) in zip (r. wrkwt, r. wrkresid)) / dof_residual (m)
114+ sqr ? s : sqrt (s)
115+ else
116+ one (T)
117+ end
118+ end
110119
111120GLM. dispersion_parameter (m:: GeneralizedLinearMixedModel ) = dispersion_parameter (m. resp. d)
112121
@@ -233,9 +242,9 @@ GeneralizedLinearMixedModel(f::FormulaTerm, tbl,
233242GeneralizedLinearMixedModel (f:: FormulaTerm , tbl:: Tables.ColumnTable ,
234243 d:: Normal ,
235244 l:: IdentityLink ;
236- wts = [] ,
237- offset = [] ,
238- contrasts = Dict {Symbol,Any} () ) =
245+ wts = wts ,
246+ offset = offset ,
247+ contrasts = contrasts ) =
239248 throw (ArgumentError (" use LinearMixedModel for Normal distribution with IdentityLink" ))
240249function GeneralizedLinearMixedModel (f:: FormulaTerm , tbl:: Tables.ColumnTable ,
241250 d:: Distribution ,
@@ -247,7 +256,14 @@ function GeneralizedLinearMixedModel(f::FormulaTerm, tbl::Tables.ColumnTable,
247256 d = Bernoulli ()
248257 end
249258 (isa (d, Normal) && isa (l, IdentityLink)) &&
250- throw (ArgumentError (" use LinearMixedModel for Normal distribution with IdentityLink" ))
259+ throw (ArgumentError (" use LinearMixedModel for Normal distribution with IdentityLink" ))
260+
261+ if ! any (isa (d, dist) for dist in (Bernoulli, Binomial, Poisson))
262+ @warn """ Results for families with a dispersion parameter are not reliable.
263+ It is best to avoid trying to fit such models in MixedModels until
264+ the authors get a better understanding of those cases."""
265+ end
266+
251267 LMM = LinearMixedModel (f, tbl, contrasts = contrasts; wts = wts)
252268 y = copy (LMM. y)
253269 # the sqrtwts field must be the correct length and type but we don't know those
@@ -288,7 +304,11 @@ function Base.getproperty(m::GeneralizedLinearMixedModel, s::Symbol)
288304 m. β
289305 elseif s ∈ (:σ , :sigma )
290306 sdest (m)
291- elseif s ∈ (:A , :L , :λ , :lowerbd , :optsum , :X , :reterms , :feterms , :formula , :σs , :σρs )
307+ elseif s == :σs
308+ σs (m)
309+ elseif s == :σρs
310+ σρs (m)
311+ elseif s ∈ (:A , :L , :λ , :lowerbd , :corr , :PCA , :rePCA , :optsum , :X , :reterms , :feterms , :formula )
292312 getproperty (m. LMM, s)
293313 elseif s == :y
294314 m. resp. y
@@ -298,18 +318,17 @@ function Base.getproperty(m::GeneralizedLinearMixedModel, s::Symbol)
298318end
299319
300320function StatsBase. loglikelihood (m:: GeneralizedLinearMixedModel{T} ) where {T}
301- accum = zero (T)
321+ r = m . resp
302322 D = Distribution (m. resp)
303- if D <: Binomial
304- for (μ, y, n) in zip (m. resp. mu, m. resp. y, m. wt)
305- accum += logpdf (D (round (Int, n), μ), round (Int, y * n))
306- end
307- else
308- for (μ, y) in zip (m. resp. mu, m. resp. y)
309- accum += logpdf (D (μ), y)
323+ accum = (
324+ if D <: Binomial
325+ sum (logpdf (D (round (Int, n), μ), round (Int, y * n))
326+ for (μ, y, n) in zip (r. mu, r. y, m. wt))
327+ else
328+ sum (logpdf (D (μ), y) for (μ, y) in zip (r. mu, r. y))
310329 end
311- end
312- accum - (mapreduce (u -> sum (abs2, u), + , m. u) + logdet (m)) / 2
330+ )
331+ accum - (sum ( sum (abs2, u) for u in m. u) + logdet (m)) / 2
313332end
314333
315334StatsBase. nobs (m:: GeneralizedLinearMixedModel ) = length (m. η)
@@ -460,13 +479,14 @@ varest(m::GeneralizedLinearMixedModel{T}) where {T} = one(T)
460479for f in (
461480 :describeblocks ,
462481 :feL ,
482+ :fetrm ,
463483 :(LinearAlgebra. logdet),
464484 :lowerbd ,
485+ :ranef ,
486+ :rePCA ,
487+ :(StatsBase. coefnames),
465488 :(StatsModels. modelmatrix),
466- :(StatsBase. vcov),
467- :σs ,
468- :σρs ,
469- )
489+ )
470490 @eval begin
471491 $ f (m:: GeneralizedLinearMixedModel ) = $ f (m. LMM)
472492 end
0 commit comments