0

I have an output for my printcp but I don't know how to call certain variables from it. For example:

data(iris)
library(caret)
idx <- createDataPartition(iris$Species,list=FALSE, p = .67,times = 1)
IrisTrain <- iris[ idx,]
IrisTest  <- iris[-idx,]

iris_model <- rpart(Species ~ ., data = IrisTrain)
err_mat<-printcp(iris_model)
err_mat

gives me an output that looks like this: enter image description here

Now I know that I can calculate resubstitution error by computing: (.66667*.044118)*100 = 2.94% But is there a way that I don't have to do this manually?

Also I can call upon 0.044118 by:

colnames(err_mat)

which outputs certain variables: "CP" "nsplit" "rel error" "xerror" "xstd" and I can extract 0.044118 by:

lenn=length(err_mat[,'rel error'])
err_mat[lenn,'rel error']

But how do I grab the Root Node Error????

I am trying to run this multiple times and I need to be able to call whatever the rootnodeerror is. Then I can just compute with the variables.

Otherwise if this is not possible what is the code to compute re substitution error? I feel like I have looked everywhere and it's all manual from results and not actual automated code as I want to loop multiple times and need to call variables.

Thank you.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
hope288
  • 725
  • 12
  • 23
  • Look at `str(err_mat)` and `str(summary(err_mat))`. Root node error should be in there somewhere. – MichaelChirico Feb 07 '16 at 20:05
  • `sos::findFn("printcp")` suggests there are three packages on CRAN with that function. Which are you using, `rpart`, `itree`, or `mvpart`? the `rpart` function is in both `rpart` and `mvpart`. – MichaelChirico Feb 07 '16 at 20:07

0 Answers0