Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify maximum number of iterations #700

Closed
venkatasg opened this issue Jul 10, 2023 · 1 comment
Closed

Specify maximum number of iterations #700

venkatasg opened this issue Jul 10, 2023 · 1 comment

Comments

@venkatasg
Copy link

I'm new to Julia and the MixedModels package, so I apologize if this has already been implemented. Is it possible to specify the maximum number of iterations when running a model fit, in addition to the fast=true and nAGQ parameters? I find that even with fast=true, the model sometimes takes far too long for my liking. Interrupting the model fit with Ctrl+C lets me inspect model parameters earlier (in a notebook), and I find that sometimes the estimates are reasonably good enough - not too far from the final fit.

@palday
Copy link
Member

palday commented Jul 12, 2023

You can modify the optsum property of the MixedModel before fitting.

using MixedModels
julia> verbagg = MixedModels.dataset(:verbagg)
Arrow.Table with 7584 rows, 9 columns, and schema:
 :subj    String
 :item    String
 :anger   Int8
 :gender  String
 :btype   String
 :situ    String
 :mode    String
 :resp    String
 :r2      String

julia> const vaform = @formula(r2 ~ 1 + anger + gender + btype + situ + (1|subj) + (1|item));

julia> mdl = GeneralizedLinearMixedModel(vaform, verbagg, Bernoulli());

julia> mdl.optsum
Initial parameter vector: [1.0, 1.0]
Initial objective value:  Inf

Optimizer (from NLopt):   LN_BOBYQA
Lower bounds:             [0.0, 0.0]
ftol_rel:                 1.0e-12
ftol_abs:                 1.0e-8
xtol_rel:                 0.0
xtol_abs:                 [1.0e-10, 1.0e-10]
initial_step:             Float64[]
maxfeval:                 -1
maxtime:                  -1.0

Function evaluations:     -1
Final parameter vector:   [1.0, 1.0]
Final objective value:    Inf
Return code:              FAILURE


julia> mdl.optsum.maxfeval = 10 # limit to 10 evalations
10

julia> fit!(mdl)
Minimizing 11    Time: 0:00:00 (12.65 ms/it)
┌ Warning: NLopt optimization failure: MAXEVAL_REACHED
└ @ MixedModels ~/.julia/packages/MixedModels/8zfgx/src/optsummary.jl:182
Generalized Linear Mixed Model fit by maximum likelihood (nAGQ = 1)
  r2 ~ 1 + anger + gender + btype + situ + (1 | subj) + (1 | item)
  Distribution: Bernoulli{Float64}
  Link: LogitLink()

   logLik    deviance     AIC       AICc        BIC    
 -4096.7549  8193.5098  8209.5098  8209.5288  8264.9802

Variance components:
        Column   VarianceStd.Dev.
subj (Intercept)  3.06 1.75
item (Intercept)  1.00 1.00

 Number of obs: 7584; levels of grouping factors: 316, 24

Fixed-effects parameters:
─────────────────────────────────────────────────────
                   Coef.  Std. Error      z  Pr(>|z|)
─────────────────────────────────────────────────────
(Intercept)    0.206053    0.604625    0.34    0.7333
anger          0.0399404   0.0213439   1.87    0.0613
gender: M      0.231317    0.243805    0.95    0.3427
btype: scold  -0.794186    0.504748   -1.57    0.1156
btype: shout  -1.53919     0.505766   -3.04    0.0023
situ: self    -0.776656    0.412493   -1.88    0.0597
─────────────────────────────────────────────────────

julia> mdl.optsum.maxfeval = -1 # unlimited evaluations
-1

julia> mdl.optsum.ftol_rel = 1e-8 # loosen convergence criterion
1.0e-8

julia> fit!(mdl)
ERROR: ArgumentError: This model has already been fitted. Use refit!() instead.
Stacktrace:
 [1] fit!(m::GeneralizedLinearMixedModel{Float64, Bernoulli{Float64}}; verbose::Bool, fast::Bool, nAGQ::Int64, progress::Bool, thin::Int64, init_from_lmm::Set{Any})
   @ MixedModels ~/.julia/packages/MixedModels/8zfgx/src/generalizedlinearmixedmodel.jl:259
 [2] fit!(m::GeneralizedLinearMixedModel{Float64, Bernoulli{Float64}})
   @ MixedModels ~/.julia/packages/MixedModels/8zfgx/src/generalizedlinearmixedmodel.jl:241
 [3] top-level scope
   @ REPL[27]:1

julia> refit!(mdl)
Minimizing 149   Time: 0:00:00 ( 3.26 ms/it)
Generalized Linear Mixed Model fit by maximum likelihood (nAGQ = 1)
  r2 ~ 1 + anger + gender + btype + situ + (1 | subj) + (1 | item)
  Distribution: Bernoulli{Float64}
  Link: LogitLink()

   logLik    deviance     AIC       AICc        BIC    
 -4075.7308  8151.4615  8167.4615  8167.4806  8222.9319

Variance components:
        Column   Variance Std.Dev. 
subj (Intercept)  1.794784 1.339696
item (Intercept)  0.247374 0.497367

 Number of obs: 7584; levels of grouping factors: 316, 24

Fixed-effects parameters:
─────────────────────────────────────────────────────
                   Coef.  Std. Error      z  Pr(>|z|)
─────────────────────────────────────────────────────
(Intercept)    0.295769    0.405585    0.73    0.4659
anger          0.0545254   0.0167561   3.25    0.0011
gender: M      0.308611    0.191255    1.61    0.1066
btype: scold  -1.07484     0.257802   -4.17    <1e-04
btype: shout  -2.12194     0.259521   -8.18    <1e-15
situ: self    -1.07715     0.211115   -5.10    <1e-06
─────────────────────────────────────────────────────

@palday palday closed this as completed Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants