R/summarize_tree.R

Defines functions summarize_tree

Documented in summarize_tree

summarize_tree <-
function(TREE) {
  
  
  if( sum( class(TREE)%in%c("rpart") )==1 ) {
    y <- TREE$y
    n <- length(y)
    pred.y <- predict(TREE)
    k <- max(1+(TREE$cptable)[,2])
    SSE <-  sum( (y-pred.y)^2 )
    RMSE <- sqrt( SSE/( n-k-1))
    AIC <- n*log(SSE)-n*log(n)+2*k
    return(list(rmse=RMSE,aic=AIC,importance=TREE$var)) }
    
    
    
    
    #Random forest
    if( sum(class(TREE) %in% c("randomForest"))==1 )  {
      IMP <- TREE$importance
      if(TREE$type=="regression") {
        IMP <- IMP[order(-IMP[,ncol(IMP)]),]
      }
        if(TREE$type=="classification") {
          IMP <- IMP[order(-IMP[,ncol(IMP)]),]
          
        }

     return(list(importance=IMP))
    }
    
}

Try the regclass package in your browser

Any scripts or data that you put into this service are public.

regclass documentation built on March 26, 2020, 8:02 p.m.