@@ -155,19 +155,24 @@ end
155155StatsBase. coef (m:: MixedModel ) = fixef (m, false )
156156
157157"""
158- fit!(m::LinearMixedModel[, verbose::Bool =false] )
158+ fit!(m::LinearMixedModel; verbose=false, REML=false )
159159
160160Optimize the objective of a `LinearMixedModel`. When `verbose` is `true` the values of the
161- objective and the parameters are printed on stdout at each function evaluation.
161+ objective and the parameters are printed on stdout at each function evaluation. The
162+ objective is negative twice the log-likelihood when `REML` is `false` (the default) or
163+ the REML criterion.
162164"""
163- function StatsBase. fit! (m:: LinearMixedModel{T} ; verbose:: Bool = false , REML:: Bool = false ) where {T}
165+ function StatsBase. fit! (m:: LinearMixedModel{T} ; verbose= false , REML= nothing ) where {T}
164166 optsum = m. optsum
165167 opt = Opt (optsum)
166168 feval = 0
169+ if isa (REML, Bool)
170+ optsum. REML = REML
171+ end
167172 function obj (x, g)
168173 isempty (g) || error (" gradient not defined" )
169174 feval += 1
170- val = objective (updateL! (setθ! (m, x)), REML )
175+ val = objective (updateL! (setθ! (m, x)))
171176 feval == 1 && (optsum. finitial = val)
172177 verbose && println (" f_" , feval, " : " , round (val, digits= 5 ), " " , x)
173178 val
@@ -229,13 +234,14 @@ lowerbd(m::LinearMixedModel) = lowerbd(m.trms)
229234StatsBase. model_response (m:: LinearMixedModel ) = vec (m. trms[end ]. x)
230235
231236"""
232- objective(m::LinearMixedModel, REML::Bool=false )
237+ objective(m::LinearMixedModel)
233238
234- Return negative twice the log-likelihood of model `m` or the REML criterion
239+ Return negative twice the log-likelihood of model `m` or the REML criterion,
240+ according to the value of `m.optsum.REML`
235241"""
236- function objective (m:: LinearMixedModel , REML :: Bool = false )
242+ function objective (m:: LinearMixedModel )
237243 wts = m. sqrtwts
238- logdet (m, REML ) + dof_residual (m, REML )* (1 + log2π + log (varest (m, REML ))) -
244+ logdet (m) + varest_denom (m )* (1 + log2π + log (varest (m))) -
239245 (isempty (wts) ? 0 : 2 sum (log, wts))
240246end
241247
269275
270276StatsBase. dof (m:: LinearMixedModel ) = size (m)[2 ] + sum (nθ, m. trms) + 1
271277
272- function StatsBase. dof_residual (m:: LinearMixedModel , REML :: Bool = false )
278+ function StatsBase. dof_residual (m:: LinearMixedModel )
273279 (n, p, q, k) = size (m)
274280 n - REML * p
275281end
290296
291297Return the estimate of σ, the standard deviation of the per-observation noise.
292298"""
293- sdest (m:: LinearMixedModel ) = sqrtpwrss (m) / √ nobs (m)
299+ sdest (m:: LinearMixedModel ) = √ varest (m)
294300
295301"""
296302 setθ!{T}(m::LinearMixedModel{T}, v::Vector{T})
@@ -321,12 +327,17 @@ This value is the contents of the `1 × 1` bottom right block of `m.L`
321327"""
322328sqrtpwrss (m:: LinearMixedModel ) = @views m. L. data. blocks[end , end ][1 ]
323329
330+ function varest_denom (m:: LinearMixedModel )
331+ (n, p, q, k) = size (m)
332+ n - m. optsum. REML * p
333+ end
334+
324335"""
325- varest(m::LinearMixedModel, REML::Bool=false )
336+ varest(m::LinearMixedModel)
326337
327338Returns the estimate of σ², the variance of the conditional distribution of Y given B.
328339"""
329- varest (m:: LinearMixedModel , REML :: Bool = false ) = pwrss (m) / dof_residual (m, REML )
340+ varest (m:: LinearMixedModel ) = pwrss (m) / varest_denom (m )
330341
331342"""
332343 pwrss(m::LinearMixedModel)
@@ -392,19 +403,25 @@ function Base.show(io::IO, m::LinearMixedModel)
392403 return nothing
393404 end
394405 n, p, q, k = size (m)
395- println (io, " Linear mixed model fit by maximum likelihood" )
406+ REML = m. optsum. REML
407+ println (io, " Linear mixed model fit by " , REML ? " REML" : " maximum likelihood" )
396408 println (io, " " , m. formula)
397409 oo = objective (m)
398- nums = showoff ([- oo/ 2 , oo, aic (m), bic (m)])
399- fieldwd = max (maximum (textwidth .(nums)) + 1 , 11 )
400- for label in [" logLik" , " -2 logLik" , " AIC" , " BIC" ]
401- print (io, rpad (lpad (label, (fieldwd + textwidth (label)) >> 1 ), fieldwd))
410+ if REML
411+ println (io, " REML criterion at convergence: " , oo)
412+ else
413+ nums = showoff ([- oo/ 2 , oo, aic (m), bic (m)])
414+ fieldwd = max (maximum (textwidth .(nums)) + 1 , 11 )
415+ for label in [" logLik" , " -2 logLik" , " AIC" , " BIC" ]
416+ print (io, rpad (lpad (label, (fieldwd + textwidth (label)) >> 1 ), fieldwd))
417+ end
418+ println (io)
419+ for num in nums
420+ print (io, lpad (num, fieldwd))
421+ end
422+ println (io)
402423 end
403424 println (io)
404- for num in nums
405- print (io, lpad (num, fieldwd))
406- end
407- println (io); println (io)
408425
409426 show (io,VarCorr (m))
410427
0 commit comments