[R] help with predict.rpart

Sarah Goslee sarah.goslee at gmail.com
Fri Jul 29 17:40:16 CEST 2011


On Fri, Jul 29, 2011 at 11:26 AM,  <mark at statcourse.com> wrote:
>        data=read.table("http://statcourse.com/research/boston.csv", ,
> sep=",", header = TRUE)
>        library(rpart)
>        fit=rpart (MV~ CRIM+ZN+INDUS+CHAS+NOX+RM+AGE+DIS+RAD+TAX+
> PT+B+LSTAT)
>        predict(fit,data[4,])
>
> plot only reveals part of the tree in contrast to the results on obtains
> with CART or C5

Using the working version of your code already presented to the list,
plot(fit);text(fit) shows all 8 terminal nodes described by print(fit), which
is the complete tree as created by the default options to rpart().

If your problem is actually that R is not returning the same results as
some other software, that's a different issue yet, and suggests to me
that the default options in rpart() and in CART are not the same. You'll
have to check yourself whether the algorithm is the same, as there
are many options for creating trees, and find out what the default
options are in CART or C5 or whatever you've decided is "right," and
then figure out how to implement them in rpart().

Sarah


> -------- Original Message --------
> Subject: Re: [R] help with rpart
> From: Sarah Goslee <sarah.goslee at gmail.com>
> Date: Fri, July 29, 2011 7:48 am
> To: mark at statcourse.com
> Cc: r-help at r-project.org
>
> Mark,
>
> The R-help list is not telepathic. I asked entirely reasonable follow-up
> questions intended to elicit from you a more extensive explanation of
> your problem.
>
> On Fri, Jul 29, 2011 at 10:26 AM, <mark at statcourse.com> wrote:
>> 1. I did not receive your reply, which, not incidentally, does not address
>> either of my questions.
>
> Nor did you address mine, except to repeat them with more exclamation
> marks.
>
>> 2. I wish to plot.rpart the entire tree!  How can I do this?
>
>
> What do you want to see that plot() does not show?
> library(rpart)
> fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)
> par(mfrow=c(1,2), xpd=NA) # otherwise on some devices the text is
> clipped
> plot(fit)
> text(fit, use.n=TRUE)
>
> You want the "whole tree" - what is missing? That's the entire tree as
> shown by print(fit).
>
>
>> 3. I wish to predict.rpart the appropriate assignment for a single vector of
>> observations using the tree produced by rpart. How can I do this?
>
> newdata <- data.frame(Age=c(18, 22), Number=c(3,4), Start=c(15, 15))
> predict(fit, newdata = newdata, type="class")
>
> This seems to me to provide the predicted class information for new
> data.
> What do you want that predict() is not providing?
>
>> Can anyone be of practical assistance?
>
> Of course. But we need to know *what you want*.
> A minimal reproducible example is handy, and I've now provided one
> for you because I'm feeling generous.
>
>> Mark
>>
>> -------- Original Message --------
>> Subject: Re: [R] help with rpart
>> From: Sarah Goslee <sarah.goslee at gmail.com>
>> Date: Thu, July 28, 2011 1:10 pm
>> To: mark at statcourse.com
>> Cc: r-help at r-project.org
>>
>> Why repost after receiving a reply? Reposting is unnecessary. If the
>> first reply was unsatisfactory, why? More detail in your question
>> leads to a more useful and informative reply.
>>
>> Just in case you didn't get it:
>>
>> On Thu, Jul 28, 2011 at 11:52 AM, <mark at statcourse.com> wrote:
>>> 1. How can I plot the entire tree produced by rpart?
>>
>> What is plot() not doing that you need?
>>
>>> 2. How can I submit a vector of values to a tree produced by rpart and
>>> have
>>> it make an assignment?
>>
>> What is predict() not doing that you need?
>>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
>



-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org



More information about the R-help mailing list