library(pacman)

p_load(ggplot2)
p_load(tidyr)
p_load(dplyr)
p_load(ggpubr)

sum_imp <- function(imp_res, method) {
  mean = colMeans(imp_res, na.rm = TRUE)
  se = apply(imp_res, 2, sd, na.rm=TRUE) / sqrt(NROW(imp_res) - 1)
  
  data = data.frame(feature = colnames(imp_res), 
                    mean = mean, se = se, 
                    lower= mean - 2 * se, upper = mean + 2 * se, method = method)
  data$lower = pmax(0, data$lower)
  data$upper = pmax(0, data$upper)
  return(data)
}

get_res <- function(files, key='errRank') {
  methods = c()
  res = list()
  N = length(files)
  xvars = paste0('x', 1:10)
  for (i in 1:length(files)) {
    f = files[i]
    tmp = readRDS(f)
    for (meth in names(tmp)) {
      if (!meth %in% names(res)) {
        methods = c(methods, meth)
        m = matrix(0, N, 10)
        colnames(m) = xvars
        
        res[[meth]] <- list(errRank = m, imp = m)
      }
      
      if (meth %in% c('rf', 'gbm')) {
        names(tmp[[meth]]$errRank) = names(tmp[['guide']]$errRank)
        names(tmp[[meth]]$imp) = names(tmp[['guide']]$imp)
      }
      
      if ("errRank" %in% names(tmp[[meth]])) {
        res[[meth]]$errRank[i,] = tmp[[meth]]$errRank[xvars]
        res[[meth]]$imp[i,] = tmp[[meth]]$imp[xvars]
      } else {
        res[[meth]]$imp[i,] = tmp[[meth]]
      }
    }
  }
  
  sum_list = list(errRank = c(), imp = c())
  for (meth in methods) {
    sum_list$errRank = rbind(sum_list$errRank, sum_imp(res[[meth]]$errRank, meth))
    sum_list$imp = rbind(sum_list$imp, sum_imp(res[[meth]]$imp, meth))
  }
  
  for (quan in c('errRank', 'imp')) {
    sum_list[[quan]] = sum_list[[quan]] %>%
      mutate(feature = factor(feature, levels = paste0('x', 1:10)))
  }
  sum_list
}




