rmixed

How to get convergence error messages or max gradient in lme4


How could I get max gradient values after doing random effects models with lme4? By default, it is shown if it is more than 0.002, but I want to make an original function as below, which needs a returned value of max gradient, whether the value is more than 0.002 or not.

  1. Doing the original model
MM<- lmer(Y ~ 1 + X + (1 |cluster_ID ), data=data)
summary(MM)
  1. If the above original model have the max gradient more than 0.002, then do the below process.
MM_par <- getME(MM, c("theta", "fixef"))
RS_MM <- update(MM, start=MM_par, control=lmerControl(optCtrl = list(maxeval=1e4)))
summary(RS_MM)

Solution

  • Example:

    library(lme4)
    fm1 <- lmer(Reaction~Days+(Days|Subject), sleepstudy)
    

    Extract the @optinfo slot and see what's there:

    names(fm1@optinfo)
    

    Check the $derivs element:

    fm1@optinfo$derivs
    

    Here's the gradient:

    max(abs(fm1@optinfo$derivs$gradient))
    

    But we want the scaled gradient (thanks @RobertLong for the reminder)

    dd <- fm1@optinfo$derivs
    sc_grad <- with(dd, solve(Hessian, gradient))
    max(abs(sc_grad))
    

    notes