@@ -177,7 +177,6 @@ function LinearMixedModel(
177
177
θ = foldl (vcat, getθ (c) for c in reterms)
178
178
optsum = OptSummary (θ, lbd)
179
179
optsum. sigma = isnothing (σ) ? nothing : T (σ)
180
- fill! (optsum. xtol_abs, 1.0e-10 )
181
180
return LinearMixedModel (
182
181
form,
183
182
reterms,
@@ -459,55 +458,10 @@ function StatsAPI.fit!(
459
458
ArgumentError (" The response is constant and thus model fitting has failed" )
460
459
)
461
460
end
462
- opt = Opt (optsum)
463
461
optsum. REML = REML
464
462
optsum. sigma = σ
465
- prog = ProgressUnknown (; desc= " Minimizing" , showspeed= true )
466
- # start from zero for the initial call to obj before optimization
467
- iter = 0
463
+ xmin, fmin = optimize! (m; progress, thin)
468
464
fitlog = optsum. fitlog
469
- function obj (x, g)
470
- isempty (g) || throw (ArgumentError (" g should be empty for this objective" ))
471
- iter += 1
472
- val = if isone (iter) && x == optsum. initial
473
- optsum. finitial
474
- else
475
- try
476
- objective (updateL! (setθ! (m, x)))
477
- catch ex
478
- # This can happen when the optimizer drifts into an area where
479
- # there isn't enough shrinkage. Why finitial? Generally, it will
480
- # be the (near) worst case scenario value, so the optimizer won't
481
- # view it as an optimum. Using Inf messes up the quadratic
482
- # approximation in BOBYQA.
483
- ex isa PosDefException || rethrow ()
484
- optsum. finitial
485
- end
486
- end
487
- progress && ProgressMeter. next! (prog; showvalues= [(:objective , val)])
488
- ! isone (iter) && iszero (rem (iter, thin)) && push! (fitlog, (copy (x), val))
489
- return val
490
- end
491
- NLopt. min_objective! (opt, obj)
492
- try
493
- # use explicit evaluation w/o calling opt to avoid confusing iteration count
494
- optsum. finitial = objective (updateL! (setθ! (m, optsum. initial)))
495
- catch ex
496
- ex isa PosDefException || rethrow ()
497
- # give it one more try with a massive change in scaling
498
- @info " Initial objective evaluation failed, rescaling initial guess and trying again."
499
- @warn """ Failure of the initial evaluation is often indicative of a model specification
500
- that is not well supported by the data and/or a poorly scaled model.
501
- """
502
- optsum. initial ./=
503
- (isempty (m. sqrtwts) ? 1.0 : maximum (m. sqrtwts)^ 2 ) *
504
- maximum (response (m))
505
- optsum. finitial = objective (updateL! (setθ! (m, optsum. initial)))
506
- end
507
- empty! (fitlog)
508
- push! (fitlog, (copy (optsum. initial), optsum. finitial))
509
- fmin, xmin, ret = NLopt. optimize! (opt, copyto! (optsum. final, optsum. initial))
510
- ProgressMeter. finish! (prog)
511
465
# # check if small non-negative parameter values can be set to zero
512
466
xmin_ = copy (xmin)
513
467
lb = optsum. lowerbd
@@ -518,7 +472,7 @@ function StatsAPI.fit!(
518
472
end
519
473
loglength = length (fitlog)
520
474
if xmin ≠ xmin_
521
- if (zeroobj = obj (xmin_, T[] )) ≤ (fmin + optsum. ftol_zero_abs)
475
+ if (zeroobj = objective! (m, xmin_ )) ≤ (fmin + optsum. ftol_zero_abs)
522
476
fmin = zeroobj
523
477
copyto! (xmin, xmin_)
524
478
elseif length (fitlog) > loglength
@@ -529,11 +483,8 @@ function StatsAPI.fit!(
529
483
# # ensure that the parameter values saved in m are xmin
530
484
updateL! (setθ! (m, xmin))
531
485
532
- optsum. feval = opt. numevals
533
486
optsum. final = xmin
534
487
optsum. fmin = fmin
535
- optsum. returnvalue = ret
536
- _check_nlopt_return (ret)
537
488
return m
538
489
end
539
490
@@ -828,25 +779,6 @@ function objective(m::LinearMixedModel{T}) where {T}
828
779
return isempty (wts) ? val : val - T (2.0 ) * sum (log, wts)
829
780
end
830
781
831
- """
832
- objective!(m::LinearMixedModel, θ)
833
- objective!(m::LinearMixedModel)
834
-
835
- Equivalent to `objective(updateL!(setθ!(m, θ)))`.
836
-
837
- When `m` has a single, scalar random-effects term, `θ` can be a scalar.
838
-
839
- The one-argument method curries and returns a single-argument function of `θ`.
840
-
841
- Note that these methods modify `m`.
842
- The calling function is responsible for restoring the optimal `θ`.
843
- """
844
- function objective! end
845
-
846
- function objective! (m:: LinearMixedModel )
847
- return Base. Fix1 (objective!, m)
848
- end
849
-
850
782
function objective! (m:: LinearMixedModel{T} , θ) where {T}
851
783
return objective (updateL! (setθ! (m, θ)))
852
784
end
0 commit comments