@@ -264,19 +264,15 @@ function fit!(
264264 optsum. final = copy (optsum. initial)
265265 end
266266 setpar! = fast ? setθ! : setβθ!
267- feval = 0
268267 function obj (x, g)
269- isempty (g) || error (" gradient not defined for this model" )
270- feval += 1
268+ isempty (g) || throw (ArgumentError (" g should be empty for this objective" ))
271269 val = deviance (pirls! (setpar! (m, x), fast, verbose), nAGQ)
272- feval == 1 && (optsum. finitial = val)
273- if verbose
274- println (" f_" , feval, " : " , val, " " , x)
275- end
270+ verbose && println (round (val, digits = 5 ), " " , x)
276271 val
277272 end
278273 opt = Opt (optsum)
279274 NLopt. min_objective! (opt, obj)
275+ optsum. finitial = obj (optsum. initial, T[])
280276 fmin, xmin, ret = NLopt. optimize (opt, copyto! (optsum. final, optsum. initial))
281277 # # check if very small parameter values bounded below by zero can be set to zero
282278 xmin_ = copy (xmin)
@@ -294,7 +290,7 @@ function fit!(
294290 # # ensure that the parameter values saved in m are xmin
295291 pirls! (setpar! (m, xmin), fast, verbose)
296292 optsum. nAGQ = nAGQ
297- optsum. feval = feval
293+ optsum. feval = opt . numevals
298294 optsum. final = xmin
299295 optsum. fmin = fmin
300296 optsum. returnvalue = ret
@@ -436,17 +432,27 @@ getθ(m::GeneralizedLinearMixedModel) = copy(m.θ)
436432getθ! (v:: AbstractVector{T} , m:: GeneralizedLinearMixedModel{T} ) where {T} = copyto! (v, m. θ)
437433
438434function StatsBase. loglikelihood (m:: GeneralizedLinearMixedModel{T} ) where {T}
439- r = m. resp
440- D = Distribution (m. resp)
441- accum = (
442- if D <: Binomial
443- sum (logpdf (D (round (Int, n), μ), round (Int, y * n))
444- for (μ, y, n) in zip (r. mu, r. y, m. wt))
445- else
446- sum (logpdf (D (μ), y) for (μ, y) in zip (r. mu, r. y))
435+ accum = zero (T)
436+ # adapted from GLM.jl
437+ # note the use of loglik_obs to handle the different parameterizations
438+ # of various response distributions which may not just be location+scale
439+ r = m. resp
440+ wts = r. wts
441+ y = r. y
442+ mu = r. mu
443+ d = r. d
444+ if length (wts) == length (y)
445+ ϕ = deviance (r)/ sum (wts)
446+ @inbounds for i in eachindex (y, mu, wts)
447+ accum += GLM. loglik_obs (d, y[i], mu[i], wts[i], ϕ)
447448 end
448- )
449- accum - (sum (sum (abs2, u) for u in m. u) + logdet (m)) / 2
449+ else
450+ ϕ = deviance (r)/ length (y)
451+ @inbounds for i in eachindex (y, mu)
452+ accum += GLM. loglik_obs (d, y[i], mu[i], 1 , ϕ)
453+ end
454+ end
455+ accum - (mapreduce (u -> sum (abs2, u), + , m. u) + logdet (m)) / 2
450456end
451457
452458StatsBase. nobs (m:: GeneralizedLinearMixedModel ) = length (m. η)
@@ -636,7 +642,7 @@ which returns `1` for models without a dispersion parameter.
636642
637643For Gaussian models, this parameter is often called σ.
638644"""
639- sdest (m:: GeneralizedLinearMixedModel{T} ) where {T} = dispersion_parameter (m) ? dispersion (m, true ) : missing
645+ sdest (m:: GeneralizedLinearMixedModel{T} ) where {T} = dispersion_parameter (m) ? dispersion (m, false ) : missing
640646
641647function Base. show (io:: IO , m:: GeneralizedLinearMixedModel )
642648 if m. optsum. feval < 0
0 commit comments