@@ -68,7 +68,7 @@ function StatsBase.deviance(m::GeneralizedLinearMixedModel{T}, nAGQ=1) where {T}
68
68
u = vec (first (m. u))
69
69
u₀ = vec (first (m. u₀))
70
70
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)
72
72
devc0 = sum! (map! (abs2, m. devc0, u), ra) # the deviance components at z = 0
73
73
sd = map! (inv, m. sd, m. LMM. L[Block (1 ,1 )]. diag)
74
74
mult = fill! (m. mult, 0 )
@@ -105,8 +105,17 @@ function deviance!(m::GeneralizedLinearMixedModel, nAGQ=1)
105
105
deviance (m, nAGQ)
106
106
end
107
107
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
110
119
111
120
GLM. dispersion_parameter (m:: GeneralizedLinearMixedModel ) = dispersion_parameter (m. resp. d)
112
121
@@ -233,9 +242,9 @@ GeneralizedLinearMixedModel(f::FormulaTerm, tbl,
233
242
GeneralizedLinearMixedModel (f:: FormulaTerm , tbl:: Tables.ColumnTable ,
234
243
d:: Normal ,
235
244
l:: IdentityLink ;
236
- wts = [] ,
237
- offset = [] ,
238
- contrasts = Dict {Symbol,Any} () ) =
245
+ wts = wts ,
246
+ offset = offset ,
247
+ contrasts = contrasts ) =
239
248
throw (ArgumentError (" use LinearMixedModel for Normal distribution with IdentityLink" ))
240
249
function GeneralizedLinearMixedModel (f:: FormulaTerm , tbl:: Tables.ColumnTable ,
241
250
d:: Distribution ,
@@ -247,7 +256,14 @@ function GeneralizedLinearMixedModel(f::FormulaTerm, tbl::Tables.ColumnTable,
247
256
d = Bernoulli ()
248
257
end
249
258
(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
+
251
267
LMM = LinearMixedModel (f, tbl, contrasts = contrasts; wts = wts)
252
268
y = copy (LMM. y)
253
269
# 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)
288
304
m. β
289
305
elseif s ∈ (:σ , :sigma )
290
306
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 )
292
312
getproperty (m. LMM, s)
293
313
elseif s == :y
294
314
m. resp. y
@@ -298,18 +318,17 @@ function Base.getproperty(m::GeneralizedLinearMixedModel, s::Symbol)
298
318
end
299
319
300
320
function StatsBase. loglikelihood (m:: GeneralizedLinearMixedModel{T} ) where {T}
301
- accum = zero (T)
321
+ r = m . resp
302
322
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))
310
329
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
313
332
end
314
333
315
334
StatsBase. nobs (m:: GeneralizedLinearMixedModel ) = length (m. η)
@@ -460,13 +479,14 @@ varest(m::GeneralizedLinearMixedModel{T}) where {T} = one(T)
460
479
for f in (
461
480
:describeblocks ,
462
481
:feL ,
482
+ :fetrm ,
463
483
:(LinearAlgebra. logdet),
464
484
:lowerbd ,
485
+ :ranef ,
486
+ :rePCA ,
487
+ :(StatsBase. coefnames),
465
488
:(StatsModels. modelmatrix),
466
- :(StatsBase. vcov),
467
- :σs ,
468
- :σρs ,
469
- )
489
+ )
470
490
@eval begin
471
491
$ f (m:: GeneralizedLinearMixedModel ) = $ f (m. LMM)
472
492
end
0 commit comments