# When should we use splines in regression? What's the justification?

I am examining the direct causal effect of x on y.

Let's assume we a model as follows

y ~ x


Shows no significant effect of x on y. The reason may be that x variable has extreme values (5 data points) that should not be excluded from dataset (they are not measuring errors). The distributions of x and y variable variable are lognormal.

y ~ s(x)


Shows nonlinear significant effect of x on y

Thus, I should use splines since there is a nonlinear relationship between x and y? Is my understanding correct?

Splines model has clearly better AIC. Plotting raw data and eyeballing these plots shows a similar non-linear relationship. I expect the relationship to be non-linear in real life/nature.

• No, that does not (necessarily) follow, also because that's not the direction in which you want to perform these actions. One (main) reason: overfitting. – user2974951 Oct 26 '20 at 8:53
• Thanks! I also added a plot for visualising the result of splines model. But what would be the indication for splines? And how should we examine such nonlinear effect with statistics? The plotting of raw data shows similar non-linear relationship. – st4co4 Oct 26 '20 at 9:01

There is a lot more to modelling than relying on the statistical significance of an association. I think it would be helpful if you graphed the two variables so as to get an eyeball on the possible association between them. There are also statistical tests to assess which of the two models provides a better fit to the data. You could use something like AIC or ANOVA based on deviance. You could do this using the R package mgcv.

library(mgcv)

mod.l <- gam(y ~ x, data = my.data)

mod.nl <- gam(y ~ s(x), data = my.data)

AIC(mod.nl, mod.l)

anova(mod.nl, mod.l, test = "Chisq")


I am assuming a gaussian distribution for x, otherwise use family = " " inside gam() with the appropriate distribution. Check the documentation for mgcv.

You can also ask yourself if the modelling results make sense based on the above. For example - do you have previous information that indicates the nature of an association between two variables.

The graph you displayed does not actually show the data points. The plot you displayed but including residuals would be helpful for you to assess the model fit. mgcv also has functions to assess residuals - see the documentation for gam.check.

I hope this helps.

• It definitely helps!! X variable distribution is lognormal with correctly measured extreme values. I expect nonlinear relationship in nature and eyeballing raw data shows it also. Should I prefer splines model if: it has clearly better AIC, raw data plotting shows the same, I expect the relationship to be nonlinear? – st4co4 Oct 26 '20 at 9:13
• Can you present the results of the anova() test I mentioned before? You may have improved AIC by a small magnitude that may not be justified by the extra degrees of freedom a non-linear model would have. If the p value from anova() is below your threshold of significance, then yes, the extra degrees of freedom for a non-linear model are justified and I would go with the non-linear. – user2888990 Oct 26 '20 at 22:41
• Also, from what you have said, it seems the x values have been logged to make the residuals normal. I assume you have checked the residuals to test the validity of a model that assumes 1) normality of the residuals, 2) constancy for the residuals' variance? You may want to check the documentation for gam.check() - a function within the mgcvpackage.Simon Wood, author of the mgcv package has a fantastic book explaining modelling with GAMs but also modelling generally. amazon.com.au/Generalized-Additive-Models-Introduction-R/dp/… – user2888990 Oct 26 '20 at 22:45