Skip to content

Instantly share code, notes, and snippets.

@davebraze
Last active September 27, 2018 10:46
Show Gist options
  • Save davebraze/281bb993bf0beee966f49e2b23262a83 to your computer and use it in GitHub Desktop.
Save davebraze/281bb993bf0beee966f49e2b23262a83 to your computer and use it in GitHub Desktop.
Basic random effect structures and non-convergence in lmer()
library(lme4)
library(nlme)
library(ggplot2)
data(Oxboys)
head(Oxboys, 12)
ggplot(aes(y=height, x=age), data=Oxboys) +
geom_point() + geom_path(aes(group=Subject))
## random intercepts only
m1 <- lmer(height ~ age + (1|Subject), data=Oxboys)
## uncorrelated random intercepts and random slopes
m2 <- lmer(height ~ age + (1|Subject) + (0+age|Subject), data=Oxboys)
## m2 <- lmer(height ~ age + (1+age||Subject), data=Oxboys) ## alt syntax for uncorr. slope/int. Note double "|".
## random intercepts, random slopes, and their correlation
m3 <- lmer(height ~ age + (1+age|Subject), data=Oxboys)
summary(m1)
summary(m2)
summary(m3)
## In our work, a major reason for using less than a maximal random effect structure is
## the presence of convergence issues in models that include rich random effects. So, if
## if a model with maximal raneff does not converge, our typical first step is to retry with
## orthogonal slopes and intercepts (e.g., m2 above). If that fails, then we will try
## different optimizers as proposed here
## https://rstudio-pubs-static.s3.amazonaws.com/33653_57fc7b8e5d484c909b615d8633c01d51.html
## and here (pp14-15)
## https://rstudio-pubs-static.s3.amazonaws.com/33653_57fc7b8e5d484c909b615d8633c01d51.html
##
## In the second case, pay particular attention to this advice:
## "try all available optimizers (e.g. several different implementations of BOBYQA and
## NelderMead, L-BFGS-B from optim, nlminb, . . . ) via the allFit() function, see ‘5.’ in
## the examples. While this will of course be slow for large fits, we consider it the gold
## standard; if all optimizers converge to values that are practically equivalent, then we
## would consider the convergence warnings to be false positives."
## ** Sub-optimal random effects
## In LMMs, sub-optimal handling of random effects can lead to shrinking of SEs
## for fixed effects with the end result being increased liklihood of type 1
## error (Barr et al., 2013; Mirman, 2014, p62).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment