[R] user defined function in rpart

Nicholas Lewin-Koh nikko at hailmail.net
Wed Aug 28 08:41:09 CEST 2002

```Hi,
I am trying to use the rpart library with my own set of functions on a
survival object. I get an immeadiate segmentation fault when i try
calling rpart with my list of functions. I get the same problem with the
logrank example from Therneau,s S-rpart library though their anova
example works. Should I report this as a bug, as even if my functions
are structured improperly, that should lead to an error in R (or
warning) not a seg fault.

If anyone has gotten this to work with survival objects i would
appreciate any tips. My test function is below. Note that there is
nothing for categorical cases because i am not using any caterorical
variables in my test.

Thanks Nicholas

tst.eval<-function(y, wt, parms){
dev<- sum(residuals(coxph(y~-1))^2)
list(label= sum(y[,2]), deviance=dev)
}

tst.split<-function(y, wt, x, parms, continuous){
xun<-sort(unique(x))
n <- nrow(y)
ndev<-sum(residuals(coxph(y~-1))^2)
gd<-rep(0,n-1)
dir<-rep(1,n-1)
if (continuous) {
for(i in xun[xun<max(xun)]){
lft<-try(sum(residuals(coxph(y[x>i]~-1))^2))
rt<-try(sum(residuals(coxph(y[x<=i]~-1))^2))
gd[x==i]<-ndev-(lft+rt)
#dir[x==i]<-1
}

list(goodness=gd , direction=)
}
else{
list(goodness= , direction = )
}
}

tst.init<-function(y, offset, parms, wt){
browser()
if (!is.null(offset)) y <- y-offset
list(y=y, parms=0, numresp=2, numy=2,
summary= function(yval, dev, wt, ylevel, digits ) {
paste("  mean=", format(signif(yval, digits)),
", MSE=" , format(signif(dev/wt, digits)),
sep='')
},
text= function(yval, dev, wt, ylevel, digits, n, use.n ) {
if(use.n) {paste(formatg(yval,digits),"\nn=", n,sep="")}
else{paste(formatg(yval,digits))}
})

}

tst.lst<-list(eval=tst.eval, split=tst.split, init=tst.init)

data(lung)
fit1 <- rpart(Surv(time, status) ~ age +  ph.karno +
meal.cal,data=lung,control=rpart.control(minsplit=30, xval=0,
cp=.011),method=tst.lst)

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```