require(pacman)

gbm_imp <- function(x, y, ...)  {
  p_load(gbm)
  p_load(caret)
  
  data <- data.frame(x, y)
  if (class(y) == 'factor') {
    fit <- gbm(y ~ ., data = data, distribution = 'bernoulli')
  } else {
    fit <- gbm(y ~ ., data = data, distribution = 'gaussian')
  }
  xImp = varImp(fit, numTrees = 100, scale = TRUE)
  
  oimp = xImp$Overall
  errRank = imp_measure(oimp)
  imp = imp_scale(oimp)
  names(errRank) = xvars
  names(imp) = xvars 
  names(oimp) = xvars
  return(list(errRank = errRank, 
              imp = imp_scale(imp),
              oimp = oimp))
}
