# R/plot_importance.R In EIX: Explain Interactions in 'XGBoost'

#### Documented in plot.importance

#' Plot importance measures
#'
#' This functions plots selected measures of importance for variables and interactions.
#' It is possible to visualise importance table in two ways: radar plot with six measures
#' and scatter plot with two choosen measures.
#'
#' Available measures:
#'\itemize{
#'\item "sumGain" - sum of Gain value in all nodes, in which given variable occurs,
#'\item "sumCover" - sum of Cover value in all nodes, in which given variable occurs; for LightGBM models: number of observation, which pass through the node,
#'\item "mean5Gain" - mean gain from 5 occurrences of given variable with the highest gain,
#'\item "meanGain" - mean Gain value in all nodes, in which given variable occurs,
#'\item "meanCover" - mean Cover value in all nodes, in which given variable occurs; for LightGBM models: mean number of observation, which pass through the node,
#'\item "freqency" - number of occurrences in the nodes for given variable.
#'}
#'
#' Additionally for plots with single variables:
#'\itemize{
#'\item "meanDepth"  - mean depth weighted by gain,
#'\item "numberOfRoots" - number of occurrences in the root,
#'\item "weightedRoot" - mean number of occurrences in the root, which is weighted by gain.
#'}
#'
#' @param x a result from the \code{importance} function.
#' @param top number of positions on the plot or NULL for all variable. Default 10.
#' @param radar TRUE/FALSE. If TRUE the plot shows
#'               six measures of variables' or interactions' importance in the model.
#'               If FALSE the plot containing two chosen measures
#'               of variables' or interactions' importance in the model.
#' @param text_start_point place, where the names of the particular feature start. Available for radar=TRUE. Range from 0 to 1. Default 0.5.
#' @param text_size size of the text on the plot. Default 3.5.
#' @param xmeasure measure on the x-axis.Available for radar=FALSE. Default "sumCover".
#' @param ymeasure measure on the y-axis. Available for radar=FALSE. Default "sumGain".
#' @param ... other parameters.
#'
#' @return a ggplot object
#'
#' @import ggplot2
#' @import data.table
#' @importFrom DALEX theme_drwhy
#' @importFrom ggrepel geom_label_repel
#'
#' @examples
#' library("EIX")
#' library("Matrix")
#' sm <- sparse.model.matrix(left ~ . - 1,  data = HR_data)
#'
#' library("xgboost")
#' param <- list(objective = "binary:logistic", max_depth = 2)
#' xgb_model <- xgboost(sm, params = param, label = HR_data[, left] == 1, nrounds = 25, verbose=0)
#'
#' imp <- importance(xgb_model, sm, option = "both")
#' imp
#' plot(imp,  top = 10)
#'
#' imp <- importance(xgb_model, sm, option = "variables")
#' imp
#' plot(imp,  top = nrow(imp))
#'
#'  imp <- importance(xgb_model, sm, option = "interactions")
#'  imp
#' plot(imp,  top =  nrow(imp))
#'
#'  imp <- importance(xgb_model, sm, option = "variables")
#'  imp
#' plot(imp, top = NULL, radar = FALSE, xmeasure = "sumCover", ymeasure = "sumGain")
#'
#'\donttest{
#'library(lightgbm)
#'train_data <- lgb.Dataset(sm, label =  HR_data[, left] == 1)
#'params <- list(objective = "binary", max_depth = 2)
#'lgb_model <- lgb.train(params, train_data, 25)
#'
#' imp <- importance(lgb_model, sm, option = "both")
#' imp
#' plot(imp,  top = nrow(imp))
#'
#' imp <- importance(lgb_model, sm, option = "variables")
#' imp
#' plot(imp, top = NULL, radar = FALSE, xmeasure = "sumCover", ymeasure = "sumGain")
#'
#'}
#'
#' @export

plot.importance <- function(x, ...,  top = 10, radar = TRUE, text_start_point = 0.5, text_size=3.5,
xmeasure = "sumCover", ymeasure = "sumGain"){

Feature <- sumGain <- sumCover <- meanGain <- meanCover <-
mean5Gain <- . <- value <- variable <- hjust <- NULL

if (is.null(top))
top <- nrow(x)

ggplot(data.frame(x[1:top, ]),
aes_string(x = xmeasure, y = ymeasure, label = "Feature")) +
geom_point() +
scale_size() + geom_label_repel() + theme_drwhy()

}else{
import <- as.data.table(x[1:top, ])
import <- import[1:top, .(Feature,
sumGain = sumGain / max(import[, sumGain]),
sumCover = sumCover / max(import[, sumCover]),
meanGain = meanGain / max(import[, meanGain]),
meanCover = meanCover / max(import[, meanCover]),
mean5Gain = mean5Gain / max(import[, mean5Gain]),
frequency = frequency / max(import[, frequency]))]
data<-import[,Feature:= ifelse(nchar(import[,Feature])>20, gsub(":", ": :",import[,Feature]),Feature)]

import$Feature <- factor(import$Feature, levels = import$Feature[order(import$sumGain, decreasing = TRUE)])

#angles and hjust of labels
numberOfBars=nrow(import)
angle= 90-360*(row(import)[,1]-0.5)/numberOfBars

import$hjust<-ifelse( angle < -90, 1, 0) import$angle<-ifelse(angle < -90, angle+180, angle)

data_to_plot <- melt(import, id = c(1,8,9), measures = 2:6, value.factor = FALSE)
data<-data_to_plot[,.(hjust=mean(hjust),angle=mean(angle)), by=Feature]

ggplot(data.frame(data_to_plot),
aes(x = Feature, y = value, colour = variable, group = variable)) +
geom_line(size = 1.5) +
geom_point(size = 2.5) +
theme_drwhy() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "bottom",
panel.grid.major.y = element_line(colour = "gray68", linetype = "dashed", size = 0.4),
axis.line = element_blank(),
axis.text.x=element_blank(),) +
labs(fill = "Measures")+
{"ft_lang_label":"__label__en","ft_lang_prob":0.5409977,"math_prob":0.9975895,"size":5731,"snap":"2022-05-2022-21","text_gpt3_token_len":1613,"char_repetition_ratio":0.13305396,"word_repetition_ratio":0.19454771,"special_character_ratio":0.3226313,"punctuation_ratio":0.20056765,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99928886,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-16T06:22:54Z\",\"WARC-Record-ID\":\"<urn:uuid:8c3b423d-a10b-45ba-a13d-9f4815deca96>\",\"Content-Length\":\"55759\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a79ea229-8000-492b-bdf1-b5a34619c70b>\",\"WARC-Concurrent-To\":\"<urn:uuid:cf6f7bc5-f3cd-474c-9e7d-8c5f86ff9192>\",\"WARC-IP-Address\":\"51.81.83.12\",\"WARC-Target-URI\":\"https://rdrr.io/cran/EIX/src/R/plot_importance.R\",\"WARC-Payload-Digest\":\"sha1:SORJQSYQT6TMIL3RF364BIEFPAGIGMJL\",\"WARC-Block-Digest\":\"sha1:J4Y6FZ3TX3BCNFDLS6RLCNEHGOKAQQNO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662509990.19_warc_CC-MAIN-20220516041337-20220516071337-00637.warc.gz\"}"}