[R] predict with model (rms package)

Mark Seeto markseeto at gmail.com
Wed Jun 8 02:09:16 CEST 2011


Dear R-help,

In the rms package, I have fitted an ols model with a variable
represented as a restricted cubic spline, with the knot locations
specified as a previously defined vector. When I save the model object
and open it in another workspace which does not contain the vector of
knot locations, I get an error message if I try to predict with that
model. This also happens if only one workspace is used, but the vector
of knot locations is removed:

library(rms)
set.seed(1)
x <- rnorm(100)
y <- 1 + x + x^2 + rnorm(100)

x.knots <- quantile(x, c(0.2, 0.5, 0.8))
ols1 <- ols(y ~ rcs(x, x.knots))

predict(ols1, data.frame(x = 0))  # This works
rm(x.knots)
predict(ols1, data.frame(x = 0))  # Gives error

The first predict gives
        1
0.8340293

while the second predict gives
Error in rcs(x, x.knots) : object 'x.knots' not found

The same error happens if x.knots is simply defined as a vector like
c(-1, 0, 1) (i.e. not using quantile). Is this the intended behaviour?
The requirement that x.knots be in the workspace seems strange, given
that the knot locations are stored in ols1$Design$parms.

Thanks for any help you can give.

Mark Seeto
National Acoustic Laboratories, Australia



More information about the R-help mailing list