I have rainfall and temperature data across multiple locations and I want to model yield as a function of these two. I know rainfall and temperature relationship is not linear i.e. there is a gain in yield first and then a decline with these two increasing.

I did this:

dat$temp2 <- temp^2
dat$rain2 <- rain^2

dat.z <- scale(dat[,2:5], center = T)

lmer(yield ~ temp + temp2 + rain + rain2 + (1| loc), data = dat.z)

I wanted to ask do I have to standarise temp and rain first and then take the square or the above step is fine?

  • $\begingroup$ What does dat[,2:5] contain? Is it temp, temp2, rain, and rain2? $\endgroup$
    – Firebug
    Oct 7, 2018 at 15:26
  • 2
    $\begingroup$ dat[,2:5] contains temp, temp2, rain, and rain2. $\endgroup$
    – 89_Simple
    Oct 7, 2018 at 15:31

2 Answers 2


A better way to model non-linear effects is using splines. This can be easily done in R using the splines package. For example, the code below specifies a natural cubic spline with three degrees of freedom for temp and rain:

fm <- lmer(yield ~ ns(temp, 3) + ns(rain, 3) + (1 | loc), data = dat)

You can test whether the effect of temp is linear using:

gm <- lmer(yield ~ temp + ns(rain, 3) + (1 | loc), data = dat)
anova(gm, fm)

P.s., to decrease potentially multicollinearity issues between polynomial terms, typically you first standardize and then calculate the polynomials. However, a better way is to use orthogonal polynomials that resolve these issues. This is available in R using the poly() function, i.e., you could use:

lmer(yield ~ poly(temp, 2) + poly(rain, 2) + (1 | loc), data = dat)

+1 to Dimitri's answer because it is on the right track. Using splines to account for non-linearities is generally a better solution than immediately transforming our variables

Some complementary points:

  1. Kenward-Roger's approximation from pbkrtest or in general using to the package lmerTest probably is a better idea than using stats::anova from the lmer object. The tests in these packages are specifically design to deal with subtleties of mixed effects models.
  2. Using generalised additive mixed models through gamm (or gamm4 - from the packages mgcv/gamm4 respectively) is a more stream-lined overall approach than using lmer and splines as it has in-built checks on accessing and visualising the spline fit (e.g. see function gam.check from mgcv or check the package RLRsim that specifically focuses on questions like "is this smooth effect significantly different from a constant effect?").
  3. Two relevant papers on the matter of selecting relevant effects are: Scheipl et al. (2008) Size and power of tests for a zero random effect variance or polynomial regression in additive and linear mixed models and Greven and Kneib (2010) On the behaviour of marginal and conditional AIC in linear mixed models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.