I would like to get fitted values from a linear model that includes a restricted cubic spline term fit via rms::rcs()
, to pass into an effects plot. The issue is that the package I typically use to get fitted values, effects
, throws an error when I try to pass a model with an rcs
term.
Here's a minimal reprex:
library(rms)
library(effects)
mod <- lm(Sepal.Length ~ rcs(Sepal.Width, 3), iris)
Effect("Sepal.Width", mod)
##Error in rcspline.eval(x, nk = nknots, inclx = TRUE, pc = pc, fractied = fractied) : knots not specified, and < 6 non-missing observations
I have tried debugging this error, but I can't arrive at how rcspline.eval()
got 6+ NAs in x
and non-specified nknots
. How do I deal with this error? Alternatively, is there another package out there that can get fitted effects from a model with an rcs
term?
Users of the rms environment will need to use the specialized functions that support its activities:
library(rms)
ddist <- datadist(iris) # need both datadist and options
options(datadist='ddist')
mod <- ols(Sepal.Length ~ rcs(Sepal.Width, 3), iris) # need ols rather than lm
plot( Predict(mod, Sepal.Width)) # Predict can be done in 2 or 3 dimension
# gives a lattice output