[R] bug in residuals.rpart?

Kevin Murphy murphyk at cs.berkeley.edu
Tue Jul 24 03:00:31 CEST 2001


The following code

  tr <- rpart(Y ~ ., dat, method="class")
  dev <- residuals(tr, "deviance")

produces the following error

Error in log(x) : Non-numeric argument to mathematical function

> .Traceback
[[1]]
[1] "log(yhat)" # line 588 of rpart

[[2]]
[1] "switch(type, usual = as.integer(y != yhat), pearson = (1 -
yhat)/yhat, "
[2] "    deviance = -2 *
log(yhat))"                                         

[[3]]
[1] "residuals.rpart(tr, \"deviance\")"

[[4]]
[1] "residuals(tr, \"deviance\")"


The problem is that tr$frame does not contain a field called 'yprob'.

I created the dat matrix above as follows

set.seed(123)
Nvars <- 4;
Nsamples <- 1000;
N <- Nvars*Nsamples;
dat <- matrix(sample(1:2, N, c(0.5,0.5), replace=TRUE), Nsamples, Nvars)
#dat <- sample.bnet(bnet, 30)
dat <- as.data.frame(dat)
for (i in 1:Nvars) {
   dat[,i]<- factor(dat[,i], 1:2)
}
names(dat) <- c(paste("X", 1:3, sep=""), "Y")


This is the tree it learned

1) root 1000 493 1 (0.5070000 0.4930000)  
  2) X2=1 485 221 1 (0.5443299 0.4556701) *
  3) X2=2 515 243 2 (0.4718447 0.5281553) *


This also happens if the distribution  generating 'dat' is not uniform
(e.g., sampled from a Bayes net, so the tree is "more interesting"),
although mysteriously rpart seems to work on MASS/cpus.


Kevin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list