[R] User defined split function in rpart
Tobias Guennel
tguennel at vcu.edu
Mon Feb 19 15:40:08 CET 2007
Maybe I should explain my Problem a little bit more detailed.
The rpart package allows for user defined split functions. An example is
given in the source/test directory of the package as usersplits.R.
The comments say that three functions have to be supplied:
1. "The 'evaluation' function. Called once per node.
Produce a label (1 or more elements long) for labeling each node,
and a deviance."
2. The split function, where most of the work occurs.
Called once per split variable per node.
3. The init function:
fix up y to deal with offsets
return a dummy parms list
numresp is the number of values produced by the eval routine's "label".
I have altered the evaluation function and the split function for my needs.
Within those functions, I need to fit a proportional odds model to the data
of the current node. I am using the polr() routine from the MASS package to
fit the model.
Now my problem is, how can I call the polr() function only with the data of
the current node. That's what I tried so far:
evalfunc <- function(y,x,parms,data) {
pomnode<-polr(data$y~data$x,data,weights=data$Freq)
parprobs<-predict(pomnode,type="probs")
dev<-0
K<-dim(parprobs)[2]
N<-dim(parprobs)[1]/K
for(i in 1:N){
tempsum<-0
Ni<-0
for(l in 1:K){
Ni<-Ni+data$Freq[K*(i-1)+l]
}
for(j in 1:K){
tempsum<-tempsum+data$Freq[K*(i-1)+j]/Ni*log(parprobs[i,j]*Ni/data$Freq[K*(i
-1)+j])
}
dev=dev+Ni*tempsum
}
dev=-2*dev
wmean<-1
list(label= wmean, deviance=dev)
}
I get the error: Error in eval(expr, envir, enclos) : argument "data" is
missing, with no default
How can I use the data of the current node?
Thank you
Tobias Guennel
More information about the R-help
mailing list