require(pacman)
p_load(ranger, caret)

ranger_imp <- function(x, y, ...)  {

  if (anyNA(x)) {
    x = caret_impute(x)
  }
  data = data.frame(x, y)
  
  fit <- ranger::ranger(y ~ ., data = data, importance = 'impurity_corrected', ...)
  errImp = ranger::importance(fit)
  errRank = imp_measure(errImp)
  
  return(list(errRank = errRank, 
              imp = imp_scale(errImp),
              oimp = errImp))
}