[R] Bug(?) in predict.tree() --- Evaluation order of Boolean operators

christian-za.keller@ubs.com christian-za.keller at ubs.com
Fri Feb 15 16:27:16 CET 2002


Dear R users

I have a problem with predict.tree() when I want the fitted values
of a tree object to be returned (i.e. argument 'newdata' is missing). 
Whatever I specify for argument 'type' the fitted model object is 
returned.

Example:
   library(tree)
   data(kyphosis)
   fit.tree <- tree(Kyphosis ~ Age + Number + Start, data=kyphosis)

   p <- predict(fit.tree, type="class")
   class(p)
   [1] "tree"

I expected p to be a factor with levels absent or present.

Note that S-Plus has (almost) the same code for predict.tree()
and gives
   > class(p)
   [1] "factor"
   > p
   [1] absent  absent  present absent  absent  absent  absent  absent
       [...]
   [79] absent  present absent 


I think the source of the problem is located in the following lines
of predict.tree():
   if (missing(newdata) || is.null(newdata) & type == "tree") 
       return(object)

When  missing(newdata)  is TRUE the logical expression
   missing(newdata) || is.null(newdata) & type == "tree"
is always TRUE, whatever is.null(newdata) or type == "tree" are:
   TRUE || FALSE & FALSE
   [1] TRUE

Note that S-Plus 6 for Windows gives
   TRUE || FALSE & FALSE
   [1] F

I don't want to judge which one is correct.

Btw. I use 
> version
         _              
platform i386-pc-mingw32
arch     x86            
os       Win32          
system   x86, Win32     
status                  
major    1              
minor    4.1            
year     2002           
month    01             
day      30             
language R   

------------------------------------------

Christian Keller
================
UBS AG
Analytical CRM UBS CH
Nüschelerstrasse 22, CH-8098 Zürich
Tel. +41-1-234 51 48, Fax. +41-1-234 26 83
christian-za.keller at ubs.com

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