|
1 | 1 | module MixedModelsPRIMAExt
|
2 | 2 |
|
3 | 3 | using MixedModels: MixedModels, LinearMixedModel, objective!
|
| 4 | +using MixedModels: ProgressMeter, ProgressUnknown |
4 | 5 | using PRIMA: PRIMA
|
5 | 6 |
|
6 |
| -function MixedModels.prfit!(m::LinearMixedModel) |
7 |
| - PRIMA.bobyqa!(objective!(m), copy(m.optsum.initial); xl=m.optsum.lowerbd) |
| 7 | +function MixedModels.prfit!(m::LinearMixedModel; |
| 8 | + progress::Bool=true, |
| 9 | + REML::Bool=m.optsum.REML, |
| 10 | + σ::Union{Real,Nothing}=m.optsum.sigma, |
| 11 | + thin::Int=1) |
| 12 | + optsum = m.optsum |
| 13 | + copyto!(optsum.final, optsum.initial) |
| 14 | + optsum.REML = REML |
| 15 | + optsum.sigma = σ |
| 16 | + optsum.finitial = objective!(m, optsum.initial) |
| 17 | + |
| 18 | + prog = ProgressUnknown(; desc="Minimizing", showspeed=true) |
| 19 | + # start from zero for the initial call to obj before optimization |
| 20 | + iter = 0 |
| 21 | + fitlog = empty!(optsum.fitlog) |
| 22 | + function obj(x) |
| 23 | + iter += 1 |
| 24 | + val = if isone(iter) && x == optsum.initial |
| 25 | + optsum.finitial |
| 26 | + else |
| 27 | + try |
| 28 | + objective!(m, x) |
| 29 | + catch ex |
| 30 | + # This can happen when the optimizer drifts into an area where |
| 31 | + # there isn't enough shrinkage. Why finitial? Generally, it will |
| 32 | + # be the (near) worst case scenario value, so the optimizer won't |
| 33 | + # view it as an optimum. Using Inf messes up the quadratic |
| 34 | + # approximation in BOBYQA. |
| 35 | + ex isa PosDefException || rethrow() |
| 36 | + optsum.finitial |
| 37 | + end |
| 38 | + end |
| 39 | + progress && ProgressMeter.next!(prog; showvalues=[(:objective, val)]) |
| 40 | + if isone(iter) || iszero(rem(iter, thin)) |
| 41 | + push!(fitlog, (copy(x), val)) |
| 42 | + end |
| 43 | + return val |
| 44 | + end |
| 45 | + |
| 46 | + ProgressMeter.finish!(prog) |
| 47 | + info = PRIMA.bobyqa!(obj, optsum.final; xl=m.optsum.lowerbd) |
| 48 | + optsum.feval = info.nf |
| 49 | + optsum.fmin = info.fx |
| 50 | + optsum.returnvalue = Symbol(info.status) |
| 51 | + optsum.optimizer = :PRIMA_BOBYQA |
8 | 52 | return m
|
9 | 53 | end
|
10 | 54 |
|
|
0 commit comments