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.
MM<- lmer(Y ~ 1 + X + (1 |cluster_ID ), data=data)
summary(MM)
MM_par <- getME(MM, c("theta", "fixef"))
RS_MM <- update(MM, start=MM_par, control=lmerControl(optCtrl = list(maxeval=1e4)))
summary(RS_MM)
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))
@
is not 100% guaranteed to be future-proof (although lme4
is pretty stable)?lme4::convergence
)