the optimizer (NLMINB) claimed the model converged, but...

3,783 views
Skip to first unread message

Edward Rigdon

unread,
Jun 14, 2021, 4:30:56 PM6/14/21
to lav...@googlegroups.com
I am just sharing this in case someone might find it interesting--this isn't a case where I have a real problem and need assistance. (I am playing with methods to turn factor models into composite models.)

In particular, I have seen other people report this message:

5: In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
  lavaan WARNING: the optimizer (NLMINB) claimed the model converged,
                  but not all elements of the gradient are (near) zero;
                  the optimizer may not have found a local solution
                  use check.gradient = FALSE to skip this check.

and I didn't entirely understand what it meant, so I thought this example might be valuable.

Here is the R script:
library(lavaan)
R<-matrix(data=c(1, .72, .72,.63,
                .72, 1,  .64, .56,
                .72, .64, 1, .56,
                .63, .56, .56, 1),byrow=T,nrow=4)
row.names(R)<-c("y1","y2","y3","y4")
colnames(R)<-row.names(R)
M<-'f=~y1+y2+y3+y4
    y2~~a*y2
    y3~~a*y3
    y2~~a*y3'
fit4<-cfa(model=M,sample.cov=R,sample.nobs=1000)
summary(fit4)

and here is the output:

Warning messages:
1: In computeOmega(Sigma.hat = Sigma.hat, Mu.hat = NULL, lavsamplestats = lavsamplestats,  :
  lav_model_gradient: Sigma.hat is not positive definite

2: In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
  lavaan WARNING: the optimizer warns that a solution has NOT been found!
3: In computeOmega(Sigma.hat = Sigma.hat, Mu.hat = NULL, lavsamplestats = lavsamplestats,  :
  lav_model_gradient: Sigma.hat is not positive definite

4: In computeOmega(Sigma.hat = Sigma.hat, Mu.hat = NULL, lavsamplestats = lavsamplestats,  :
  lav_model_gradient: Sigma.hat is not positive definite

5: In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
  lavaan WARNING: the optimizer (NLMINB) claimed the model converged,
                  but not all elements of the gradient are (near) zero;
                  the optimizer may not have found a local solution
                  use check.gradient = FALSE to skip this check.
6: In computeOmega(Sigma.hat = Sigma.hat, Mu.hat = NULL, lavsamplestats = lavsamplestats,  :
  lav_model_gradient: Sigma.hat is not positive definite

7: In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
  lavaan WARNING: the optimizer warns that a solution has NOT been found!
8: In computeOmega(Sigma.hat = Sigma.hat, Mu.hat = NULL, lavsamplestats = lavsamplestats,  :
  lav_model_gradient: Sigma.hat is not positive definite

9: In computeOmega(Sigma.hat = Sigma.hat, Mu.hat = NULL, lavsamplestats = lavsamplestats,  :
  lav_model_gradient: Sigma.hat is not positive definite

10: In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
  lavaan WARNING: the optimizer (NLMINB) claimed the model converged,
                  but not all elements of the gradient are (near) zero;
                  the optimizer may not have found a local solution
                  use check.gradient = FALSE to skip this check.
> summary(fit4)
lavaan 0.6-7 did NOT end normally after 1 iterations
** WARNING ** Estimates below are most likely unreliable

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                          9
  Number of equality constraints                     2
                                                     
  Number of observations                          1000
                                                     
Model Test User Model:
                                                     
  Test statistic                                    NA
  Degrees of freedom                                NA

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Expected
  Information saturated (h1) model          Structured

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  f =~                                                
    y1                1.000                          
    y2                0.700       NA                  
    y3                0.700       NA                  
    y4                0.700       NA                  

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)
 .y2 ~~                                              
   .y3         (a)    0.667       NA                  

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .y2         (a)    0.667       NA                  
   .y3         (a)    0.667       NA                  
   .y1                1.000       NA                  
   .y4                1.000       NA                  
    f                 1.000       NA       

For the record, adding starting values, as in:

R<-matrix(data=c(1, .72, .72,.63,
                 .72, 1,  .64, .56,
                 .72, .64, 1, .56,
                 .63, .56, .56, 1),byrow=T,nrow=4)
row.names(R)<-c("y1","y2","y3","y4")
colnames(R)<-row.names(R)
M<-'f=~y1+y2+y3+y4
    y2~~a*y2 + 0.36?y2
    y3~~a*y3 + 0.36?y3
    y2~~a*y3 + 0.36?y3'
fit5<-cfa(model=M,sample.cov=R,sample.nobs=1000)
summary(fit5)

Got rid of most of the warnings, replaced by an error message:

> fit5<-cfa(model=M,sample.cov=R,sample.nobs=1000)
           [,1]       [,2]       [,3]       [,4]
[1,] 0.54950000 0.04444444 0.04444444 0.03888889
[2,] 0.04444444 0.39950617 0.39950617 0.03456790
[3,] 0.04444444 0.39950617 0.39950617 0.03456790
[4,] 0.03888889 0.03456790 0.03456790 0.52974691
Error in lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
  lavaan ERROR: initial model-implied matrix (Sigma) is not positive definite;
  check your model and/or starting parameters.
> summary(fit5)
lavaan 0.6-7 did NOT end normally after 1 iterations
** WARNING ** Estimates below are most likely unreliable

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                          9
  Number of equality constraints                     2
                                                     
  Number of observations                          1000
                                                     
Model Test User Model:
                                                     
  Test statistic                                    NA
  Degrees of freedom                                NA

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Expected
  Information saturated (h1) model          Structured

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  f =~                                                
    y1                1.000                          
    y2                0.700       NA                  
    y3                0.700       NA                  
    y4                0.700       NA                  

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)
 .y2 ~~                                              
   .y3         (a)    0.667       NA                  

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .y2         (a)    0.667       NA                  
   .y3         (a)    0.667       NA                  
   .y1                1.000       NA                  
   .y4                1.000       NA                  
    f                 1.000       NA                  


Yves Rosseel

unread,
Jun 16, 2021, 10:24:56 AM6/16/21
to lav...@googlegroups.com
> 5: In lav_model_estimate(lavmodel = lavmodel, lavpartable = lavpartable,  :
>   lavaan WARNING: the optimizer (NLMINB) claimed the model converged,
>                   but not all elements of the gradient are (near) zero;
>                   the optimizer may not have found a local solution
>                   use check.gradient = FALSE to skip this check.
>
> and I didn't entirely understand what it meant

I understand why this is a puzzling warning message. Let me try to
explain what it means.

Optimizers like nlminb() try to minimize an objective function. Some of
them (including nlminb()) use gradient information (and approximate
Hessian information) to guide this search to a (local) minimum.

Optimizers have built-in stopping criteria, otherwise they would go on
forever. These stopping criteria are based on changes (over several
iterations) in both the value of the objective function itself, and
changes in the parameters values. If those changes fall below a certain
threshold, the optimizer stops, and if everything is in order, the
optimizer will say 'converged'.

Importantly, the optimizer will not (explicitly) check the gradient at
the final solution. This is important for statistical models (but often
not needed for non-statistical problems), where this minimum should
truly be a minimum, in the sense that any change in the parameter values
would make the function value go up again. This check is always done by
lavaan to verify that the 'solution' found by the optimizer is indeed a
(local) minimum. In 99.9% of the cases, this will be ok. But in some
problematic model/data combinations, this check may fail, producing the
above warning.

If that happens, you can check the gradient (as used by the optimizer)
as follows:

lavInspect(fit, "optim.gradient")

or if you wish to see more details,

lavTech(fit, "optim.gradient")

If any of those elements (assuming no constraints were used) is larger
than a small number (in absolute value), the above warning is given. The
value for this tolerance (which can be changed by the user) can be
obtained as follows:

lavInspect(fit, "options")$optim.dx.tol

If you get this warning, check the usual suspects: scale of the observed
variables, model identification, ... If the sample size is small, you
can try using (automatic) parameter bounds, by using the option bounds =
TRUE.

Yves.

Edward Rigdon

unread,
Jun 16, 2021, 11:36:45 AM6/16/21
to lav...@googlegroups.com
Thank you!

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/4bcb073a-3140-9a96-c9c2-cc5958a964a8%40gmail.com.
Reply all
Reply to author
Forward
0 new messages