# How to check the linearity of continuous variable in linear mixed model

I'm doing a linear mixed model using lme.

In my adjustement factors, I have a continuous varaible (named X1). And I want to check the linearity of this variable using a spline function or a fractional polynomial but I don't succeed.

Can someone help me ? How to do that ?

Ex with spline :

lme(Y ~ T + X + pspline(X1, 4), data = df, random = ~ T | SUBJECT )


But then, I don't know how to conclude for the linearity.

## 1 Answer

You can check for linearity using natural cubic splines from the splines package and a likelihood ratio test - note that you need to fit the model using maximum likelihood (not restricted maximum likelihood) because of the different fixed-effects parts. For example,

library("splines")
fm0 <- lme(Y ~ T + X + X1, data = df, random = ~ T | SUBJECT, method = "ML")
fm1 <- lme(Y ~ T + X + ns(X1, 4), data = df, random = ~ T | SUBJECT, method = "ML")
anova(fm0, fm1)

• What is the difference between using a spline function like pspline(X1,4) and a natural cubic spline like in your example ? If the likelihood ratio test is significant, does this mean that the variable is not linear ? Thanks a lot for your answer. – Klmce Apr 11 at 19:28
• Indeed, if the likelihood ratio test is significant it means that the model with the spline provides a better fit to the data than the model with the linear term. – Dimitris Rizopoulos Apr 11 at 19:31
• Dimitris, what strategy do you recommend for choosing the degrees of freedom for the ns() term in an lme model? – Isabella Ghement Apr 11 at 22:34
• Check Section 2.4 (slides 65-57) in my course notes for my suggestions: drizopoulos.com/courses/EMC/CE08.pdf – Dimitris Rizopoulos Apr 12 at 4:28