require(pacman)

cforest_imp <- function(x, y, conditional=TRUE, ...) {
  p_load(partykit, permimp, party)
  data <- data.frame(x, y)
  # for (vx in colnames(data)) {
  #   if (class(data[, vx]) == 'factor') data[,vx] <- as.integer(data[,vx]) 
  # }
  fit <- party::cforest(y ~ ., data = data)
  ##xImp <- permimp(fit, asParty=TRUE)$values
  xImp <- permimp(fit, conditional=conditional, asParty=TRUE)$values
  
  ## xImp2 = varimp(fit)
  
  errRank = imp_measure(xImp)
  
  return(list(errRank = errRank, 
              imp = imp_scale(xImp),
              oimp = xImp))
}
