0

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?

Flux
  • 9,805
  • 5
  • 46
  • 92
Dan Villarreal
  • 119
  • 1
  • 12
  • I would have expected that Predict or predict would give you what you expect. The `rms` planet is not in orbit around the `effects` center of gravity. – IRTFM Nov 10 '18 at 23:28
  • Can you give an example as an answer? I looked into predict but couldn't wrap my head around how to make it work. – Dan Villarreal Nov 10 '18 at 23:30

1 Answers1

1

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

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I see. I'm not sure if this solution will work for me since I ultimately want to apply this to a mixed-effects model, and AFAIK rms's builtin modeling functions don't allow random effects – Dan Villarreal Nov 10 '18 at 23:55
  • So you posed a question that didn't include the essential criteria. – IRTFM Nov 10 '18 at 23:58
  • I didn't know where the sticking point would be. You did answer the question, so I'll accept. – Dan Villarreal Nov 11 '18 at 00:00
  • 1
    My understanding is that the mgcv package provides cubic splines via the `s()` function and supports mixed effects. – IRTFM Nov 11 '18 at 00:29