@@ -53,6 +53,20 @@ struct GeneralizedLinearMixedModel{T <: AbstractFloat} <: MixedModel{T}
5353 mult:: Vector{T}
5454end
5555
56+ function StatsBase. coeftable (m:: GeneralizedLinearMixedModel )
57+ co = fixef (m)
58+ se = stderror (m)
59+ z = co ./ se
60+ pvalue = ccdf .(Chisq (1 ), abs2 .(z))
61+ CoefTable (
62+ hcat (co, se, z, pvalue),
63+ [" Estimate" , " Std.Error" , " z value" , " P(>|z|)" ],
64+ coefnames (m),
65+ 4 , # pvalcol
66+ 3 , # teststatcol
67+ )
68+ end
69+
5670"""
5771 deviance(m::GeneralizedLinearMixedModel{T}, nAGQ=1)::T where {T}
5872
@@ -68,7 +82,7 @@ function StatsBase.deviance(m::GeneralizedLinearMixedModel{T}, nAGQ=1) where {T}
6882 u = vec (first (m. u))
6983 u₀ = vec (first (m. u₀))
7084 copyto! (u₀, u)
71- ra = RaggedArray (m. resp. devresid, first (m. LMM. reterms ). refs)
85+ ra = RaggedArray (m. resp. devresid, first (m. LMM. allterms ). refs)
7286 devc0 = sum! (map! (abs2, m. devc0, u), ra) # the deviance components at z = 0
7387 sd = map! (inv, m. sd, m. LMM. L[Block (1 ,1 )]. diag)
7488 mult = fill! (m. mult, 0 )
@@ -105,8 +119,17 @@ function deviance!(m::GeneralizedLinearMixedModel, nAGQ=1)
105119 deviance (m, nAGQ)
106120end
107121
108- GLM. dispersion (m:: GeneralizedLinearMixedModel , sqr:: Bool = false ) =
109- dispersion (m. resp, dof_residual (m), sqr)
122+ function GLM. dispersion (m:: GeneralizedLinearMixedModel{T} , sqr:: Bool = false ) where {T}
123+ # adapted from GLM.dispersion(::AbstractGLM, ::Bool)
124+ # TODO : PR for a GLM.dispersion(resp::GLM.GlmResp, dof_residual::Int, sqr::Bool)
125+ r = m. resp
126+ if dispersion_parameter (r. d)
127+ s = sum (wt * abs2 (re) for (wt, re) in zip (r. wrkwt, r. wrkresid)) / dof_residual (m)
128+ sqr ? s : sqrt (s)
129+ else
130+ one (T)
131+ end
132+ end
110133
111134GLM. dispersion_parameter (m:: GeneralizedLinearMixedModel ) = dispersion_parameter (m. resp. d)
112135
@@ -295,7 +318,11 @@ function Base.getproperty(m::GeneralizedLinearMixedModel, s::Symbol)
295318 m. β
296319 elseif s ∈ (:σ , :sigma )
297320 sdest (m)
298- elseif s ∈ (:A , :L , :λ , :lowerbd , :optsum , :X , :reterms , :feterms , :formula , :σs , :σρs )
321+ elseif s == :σs
322+ σs (m)
323+ elseif s == :σρs
324+ σρs (m)
325+ elseif s ∈ (:A , :L , :λ , :lowerbd , :corr , :PCA , :rePCA , :optsum , :X , :reterms , :feterms , :formula )
299326 getproperty (m. LMM, s)
300327 elseif s == :y
301328 m. resp. y
@@ -305,18 +332,17 @@ function Base.getproperty(m::GeneralizedLinearMixedModel, s::Symbol)
305332end
306333
307334function StatsBase. loglikelihood (m:: GeneralizedLinearMixedModel{T} ) where {T}
308- accum = zero (T)
335+ r = m . resp
309336 D = Distribution (m. resp)
310- if D <: Binomial
311- for (μ, y, n) in zip (m. resp. mu, m. resp. y, m. wt)
312- accum += logpdf (D (round (Int, n), μ), round (Int, y * n))
337+ accum = (
338+ if D <: Binomial
339+ sum (logpdf (D (round (Int, n), μ), round (Int, y * n))
340+ for (μ, y, n) in zip (r. mu, r. y, m. wt))
341+ else
342+ sum (logpdf (D (μ), y) for (μ, y) in zip (r. mu, r. y))
313343 end
314- else
315- for (μ, y) in zip (m. resp. mu, m. resp. y)
316- accum += logpdf (D (μ), y)
317- end
318- end
319- accum - (mapreduce (u -> sum (abs2, u), + , m. u) + logdet (m)) / 2
344+ )
345+ accum - (sum (sum (abs2, u) for u in m. u) + logdet (m)) / 2
320346end
321347
322348StatsBase. nobs (m:: GeneralizedLinearMixedModel ) = length (m. η)
@@ -467,13 +493,14 @@ varest(m::GeneralizedLinearMixedModel{T}) where {T} = one(T)
467493for f in (
468494 :describeblocks ,
469495 :feL ,
496+ :fetrm ,
470497 :(LinearAlgebra. logdet),
471498 :lowerbd ,
499+ :PCA ,
500+ :rePCA ,
501+ :(StatsBase. coefnames),
472502 :(StatsModels. modelmatrix),
473- :(StatsBase. vcov),
474- :σs ,
475- :σρs ,
476- )
503+ )
477504 @eval begin
478505 $ f (m:: GeneralizedLinearMixedModel ) = $ f (m. LMM)
479506 end
0 commit comments