Appendix B — Cheat sheet

B.1 Formula syntax

Table B.1: Formula syntax
Formula Interpretation
y ~ x Intercept + slope
y ~ 1 intercept only
y ~ x - 1 or x + 0 only slope, for categorical
log(x) ~ sqrt(x) predictor and response variable can be transformed
y ~ x1 + x2 multiple predictors can be added
y ~ x1 * x2 interaction and main effects
y ~ x1:x2 only interaction
y ~ (x1 + x2)^2 all 2nd order interactions, works also with 3rd order ^3 etc
y ~ I(x^2) I() forces mathematical interpretation of whatever is in the parenthesis
y ~ s(x) spline (works only for mgcv)
y ~ te(x1, x2) tesnsor spline (works only for mgcv)
y ~. include all variables as main effects
y~.^2 include all variables as main effects and their 2nd order interactions

B.2 Random effect syntax

The following formulas are used by many R packages (e.g. glmmTMB and lme4). For a detailed overview, see Ben Bolkers glmmFAQ

Formula Interpretation
(1|group) Random intercept within group
(x|group) Random intercept within group and random slope for x within group; correlation between random intercept and random slope
(0+x|group) or (x-1|group) Random slope for x within group without random intercept for group
(0+x|group) + (1|group) or (x||group) Random intercept and slope but uncorrelated
(1|group:site) Random intercept for sites within group (nested random effect)
(1|group) + (1|site) Random intercept within groups and within sites (crossed random effects)

B.2.1 Correlation structures:

B.2.1.1 glmmTMB

glmmTMB supports several correlation structures but we will show here only two common representatives. See also the glmmTMB vignette about correlation structures.

Type Formula Interpretation
Spatial

coords = numFactor(cbind(Latitude, Longitude))

y~x + exp(0+coords|group)

Use glmmTMB::numFactor() function to represent your coordinates as a factor.

Conditional autoregressive random intercepts within group based on the coords (sites).

Temporal

yearFactor = factor(years)

y~x + ar1(0+yearFactor|group)

Change your time variable to factor.

Conditional autoregressive random intercepts within group based on years.

B.2.1.2 lme4

lme4 does not support correlation structures

B.2.1.3 nlme::gls

Type Formula Interpretation
Spatial gls(y~.., correlation = corExp( ~ Lat + Long|group) Spatial correlation structure (no random effects) between sites within group (group is optional)
Temporal gls(y~.., correlation = corAR1( ~ years | group) Temoral correlation structure (no random effects) between years within group (group is optional)
Phylogeny library(ape)gls(y~.., correlation = corBrownian(phy = pyhTree, form ~ species | group) Pyhlogenetic correlation structure (no random effects) between species within group (group is optional)