@@ -287,8 +287,12 @@ GLM.dispersion_parameter(m::LinearMixedModel) = true
287287StatsBase. dof (m:: LinearMixedModel ) = size (m)[2 ] + nθ (m) + 1
288288
289289function StatsBase. dof_residual (m:: LinearMixedModel ):: Int
290- (n, p, q, k) = size (m)
291- n - m. optsum. REML * p
290+ # nobs - rank(FE) - 1 (dispersion)
291+ # this differs from lme4 by not including nθ
292+ # a better estimate would be a number somewhere between the number of
293+ # variance components and the number of conditional modes
294+ # nobs, rank FE, num conditional modes, num grouping vars
295+ nobs (m) - size (m)[2 ] - 1
292296end
293297
294298"""
@@ -528,7 +532,7 @@ Return negative twice the log-likelihood of model `m`
528532"""
529533function objective (m:: LinearMixedModel )
530534 wts = m. sqrtwts
531- logdet (m) + dof_residual (m) * (1 + log2π + log (varest (m))) -
535+ logdet (m) + ssqdenom (m) * (1 + log2π + log (varest (m))) -
532536 (isempty (wts) ? 0 : 2 * sum (log, wts))
533537end
534538
@@ -778,8 +782,16 @@ function σρs(m::LinearMixedModel)
778782 NamedTuple {fnames(m)} (((σρs (t, σ) for t in m. reterms). .. ,))
779783end
780784
785+ """
786+ size(m::LinearMixedModel)
787+
788+ Returns the size of a mixed model as a tuple of length four:
789+ the number of observations, the number of (non-singular) fixed-effects parameters,
790+ the number of conditional modes (random effects), the number of grouping variables
791+ """
781792function Base. size (m:: LinearMixedModel )
782- n, p = size (first (m. feterms))
793+ n = size (first (m. feterms),1 )
794+ p = first (m. feterms). rank
783795 n, p, sum (size .(m. reterms, 2 )), length (m. reterms)
784796end
785797
@@ -792,6 +804,20 @@ This value is the contents of the `1 × 1` bottom right block of `m.L`
792804"""
793805sqrtpwrss (m:: LinearMixedModel ) = first (m. L. blocks[end , end ])
794806
807+ """
808+ ssqdenom(m::LinearMixedModel)
809+
810+ Return the denominator for penalized sums-of-squares.
811+
812+ For MLE, this value is either the number of observation for ML. For REML, this
813+ value is the number of observations minus the rank of the fixed-effects matrix.
814+ The difference is analagous to the use of n or n-1 in the denominator when
815+ calculating the variance.
816+ """
817+ function ssqdenom (m:: LinearMixedModel ):: Int
818+ nobs (m) - m. optsum. REML * first (m. feterms). rank
819+ end
820+
795821"""
796822 std(m::MixedModel)
797823
898924
899925Returns the estimate of σ², the variance of the conditional distribution of Y given B.
900926"""
901- varest (m:: LinearMixedModel ) = pwrss (m) / dof_residual (m)
927+ varest (m:: LinearMixedModel ) = pwrss (m) / ssqdenom (m)
902928
903929"""
904930 vcov(m::LinearMixedModel)
0 commit comments