require(pacman)

ctree_imp <- function(x, y, ...) {
  p_load(partykit)
  data <- data.frame(x, y)
  fit <- partykit::ctree(y ~ ., data = data)
  xImp = partykit::varimp(fit, nperm=100, conditional=TRUE)
  
  tmp = rep(0, NCOL(x))
  names(tmp) = colnames(x)
  
  
  if (length(xImp) != 0) {
    tmp[names(xImp)] = xImp
  }
  xImp = tmp
  
  errRank = imp_measure(xImp)
  
  return(list(errRank = errRank, 
              imp = imp_scale(xImp),
              oimp = xImp))
}

