DW1310
DW1310

Reputation: 323

Using restricted spline functions in a multivariable regression model

I am just struggling to find an answer to a statistical/R questions concerning the use of splines.

I have been building a linear model such as below

lm(imaging~bloodtest + age + sex + timetoblood +timetoimage, data=df) 

but have found from the residuals and examination of the plot that the fit of the model is not great, and is curvilinear relationship.

I am wanting to examine the use of restricted cubic splines in the regression model, but am wondering how to go about examining this? All the examples I can find are of univariate models on the various worked examples and I am wondering how to include splines in a multivariate model?

Upvotes: 0

Views: 1066

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226097

I believe that a restricted cubic spline (linear at the endpoints) is the same as a natural spline, implemented as ns() in the splines package (a "recommended" package, so it comes with R).

You can replace any or all of the continuous predictors in your model with natural spline terms with an appropriate number of degrees of freedom (this is a decision you have to make: see e.g. Harrell's Regression Modeling Strategies for guidance).

library(splines)
lm(imaging~ bloodtest + ns(age, 7) + sex + ns(timetoblood,7) + ns(timetoimage,7), 
   data=df)

If you want smooth interaction terms you might need to move to the mgcv package, which offers tensor product smooths (and uses penalized or regression splines).

Upvotes: 1

Related Questions