R/prune.rpart.rt.R

Defines functions prune.rpart.rt

Documented in prune.rpart.rt

#' \code{prune.rpart} experimental replacement
#' @author Efstathios D. Gennatas
#' @export

prune.rpart.rt <- function(tree, cp, ...) {
  
  importance <- getFromNamespace("importance", "rpart")
  
  ff <- tree$frame
  id <- as.integer(row.names(ff))
  toss <- id[ff$complexity <= cp & ff$var != "<leaf>"]
  if (length(toss) == 0L) 
    return(tree)
  newx <- rpart::snip.rpart(tree, toss)
  temp <- pmax(tree$cptable[, 1L], cp)
  keep <- match(unique(temp), temp)
  newx$cptable <- tree$cptable[keep, , drop = FALSE]
  row.id <- which(rownames(newx$cptable) == max(keep)) # delta
  newx$cptable[row.id, 1L] <- cp
  newx$variable.importance <- importance(newx)
  newx
}
egenn/rtemis documentation built on April 3, 2020, 6:43 p.m.