[Rd] making makepredictcall() work

D. Rizopoulos d.rizopoulos at erasmusmc.nl
Tue May 21 13:19:18 CEST 2013

Dear All,

I'm interested in creating a function similar to ns() from package 
splines that can be passed in a model formula. The idea is to produce 
"safe" predictions from a model using this function. As I have seen, to 
do this I need to use makepredictcall(). Consider the following toy example:

myns <- function (x, df = NULL, knots = NULL, intercept = FALSE, 
Boundary.knots = range(x),
                    extraArg = 0) {
     ns.x <- if (is.null(knots)) {
         ns(x, df = df, intercept = intercept, Boundary.knots = 
     } else {
         ns(x, knots = knots, intercept = intercept, Boundary.knots = 
     out <- ns.x + extraArg
     attr(out, "class") <- c("myns", "basis", "matrix")

makepredictcall.myns <- function (var, call) {
     # based on splines:::makepredictcall.ns
     if (as.character(call)[1L] != "myns")
     at <- attributes(var)[c("knots", "Boundary.knots", "intercept", 
     xxx <- call[1L:2L]
     xxx[names(at)] <- at

dd <- data.frame(y = rnorm(12))
terms(model.frame(terms(~ myns(y, df = 3, extraArg = 0.5)), data = dd))

As it can be seen, makepredictcall.myns() succeeds in correctly passing 
the knots and Boundary.knots from the original data in the "predvars" 
attribute of the terms objects but it does not work for the new argument 
'extraArg' I introduced in myns().

Any pointers on how to resolve this will be highly appreciated.

Thanks in advance.


Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/

More information about the R-devel mailing list