6
$\begingroup$

How can I extract the R-square from a fit rpart model?

rsq.rpart(fit)

plots the two graphs, but I simply want to extract the R-square value for the full tree.

I assume this is fairly obvious, but numerous searches didn't really lend anything useful. Any help you can provide will be greatly appreciated.

$\endgroup$
7
$\begingroup$

The advantage of R is that most of the time you can easily access the source code. So in your case, start with

> rsq.rpart

(without parenthesis) to see what the function actually does. The $R^2$ values are obtained as

tmp <- printcp(fit)
rsq.val <- 1-tmp[,c(3,4)]  

where for each row (aka, No. splits) we have the "apparent" and "relative" (wrt. cross-validation) statistics.

$\endgroup$
  • 2
    $\begingroup$ +1, pressed post the same second your answer appeared. I would add that for a full tree rsq.val[nrow(rsq.val),] is probably what OP wants. $\endgroup$ – mpiktas Dec 28 '10 at 20:34
  • $\begingroup$ @mpiktas Oups... Needless to say, your comment is very appreciated. $\endgroup$ – chl Dec 28 '10 at 20:39
  • $\begingroup$ Thanks a bunch. I am (obviously) new to R, and never once did I think to look at the function as you first mentioned. I was trying something similar to your answer, but that is perfect! Thx $\endgroup$ – Btibert3 Dec 28 '10 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.