[R] predict with model (rms package)

Mark Seeto markseeto at gmail.com
Wed Jun 8 20:59:43 CEST 2011


Thanks for your reply, Frank. I've noticed that the x.knots object doesn't
actually have to be the vector of knots. Just having x.knots <- 0 or even
x.knots <- "a" will allow predict to work.

Mark Seeto


Frank Harrell wrote:
> 
> This is a consequence of predict.ols calling predictrms which relies on
> model.frame which re-issues the expression of x.knots.  You would have the
> same problem if using update(fit object) in another session.  For that
> reason you have to keep an external knots vector available in your
> environment.
> 
> Frank
> 
> 
> Mark Seeto wrote:
>> 
>> 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
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 


--
View this message in context: http://r.789695.n4.nabble.com/predict-with-model-rms-package-tp3581229p3583344.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list