I have some code where I fit a tree and then automatically prune the tree back by selecting the complexity parameter such that it minimizes the cross validation error, as displayed by the printcp()
function. In digesting my console output, I am annoyed by the mass that is printed out by printcp()
.
What I do is I convert the output of the printcp() function to a dataframe and then use some logic to extract the CP value for the lowest CV error. Is there anyway I can do this, WITHOUT printing the output of printcp to the console?
df_tree_1 <- rpart(formula(df_lm_2), cp = 0.0001, data = train)
cp_df <- data.frame(printcp(df_tree_1))
df_tree_1 <- prune.rpart(tree = df_tree_1, cp = cp_df$CP[which(cp_df$xerror == min(cp_df$xerror))])