8

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.

chl
  • 50,972
  • 18
  • 205
  • 364
Btibert3
  • 1,154
  • 1
  • 13
  • 23

1 Answers1

9

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.

chl
  • 50,972
  • 18
  • 205
  • 364
  • 3
    +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. – mpiktas Dec 28 '10 at 20:34
  • @mpiktas Oups... Needless to say, your comment is very appreciated. – chl Dec 28 '10 at 20:39
  • 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 – Btibert3 Dec 28 '10 at 20:50