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

#### Documented in lollipop

```#' Tables needed for lollipop plot
#'
#' This function calculates two tables needed to generate lollipop plot, which visualise the model.
#' The first table contains information about all nodes in the trees forming a model.
#' It includes gain value, depth and ID of each nodes.
#' The second table contains similarly information about roots in the trees.
#'
#' @param xgb_model a xgboost or lightgbm model.
#' @param data a data table with data used to train the model.
#'
#' @return an object of the lollipop class
#'
#' @import data.table
#'
#' @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)
#'
#' lolli <- lollipop(xgb_model, sm)
#' plot(lolli, labels = "topAll", log_scale = TRUE)
#'
#'\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)
#'
#' lolli <- lollipop(lgb_model, sm)
#' plot(lolli, labels = "topAll", log_scale = TRUE)
#'
#'}
#'
#' @export

lollipop <- function(xgb_model, data){

Feature <- Quality <- Node <- Tree <- ID <- depth <-
interaction <- . <- parentsName <- name_pair <- NULL

trees = rbindlist(calculateGain(xgb_model, data))
roots <- trees[Node == 0, .(Quality, Feature, Tree, ID, depth)]
nodes <- trees[Feature != "Leaf", .(Quality,
Feature,
Node,
Tree,
ID,
interaction,
depth,
parentsName,
name_pair)]
nodes <- nodes[interaction == TRUE, Feature := name_pair]

lollipop <- list(nodes, roots)
class(lollipop) <- c("lollipop", "list")

return(lollipop)
}
```

## Try the EIX package in your browser

Any scripts or data that you put into this service are public.

EIX documentation built on March 23, 2021, 9:06 a.m.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5327331,"math_prob":0.94189274,"size":1959,"snap":"2022-05-2022-21","text_gpt3_token_len":541,"char_repetition_ratio":0.12685423,"word_repetition_ratio":0.046666667,"special_character_ratio":0.32261357,"punctuation_ratio":0.17630059,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99399006,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-17T03:28:04Z\",\"WARC-Record-ID\":\"<urn:uuid:5c6dcdff-082e-4740-afd1-807e57892ca1>\",\"Content-Length\":\"34957\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b47caeb9-cdbb-47dc-b897-288523b40c6f>\",\"WARC-Concurrent-To\":\"<urn:uuid:e302f5d2-304b-4219-b0ad-014010f3fc44>\",\"WARC-IP-Address\":\"51.81.83.12\",\"WARC-Target-URI\":\"https://rdrr.io/cran/EIX/src/R/lollipop.R\",\"WARC-Payload-Digest\":\"sha1:OUTZG2DLXK7PNZ4XEJXFXIWBSTBVJZN6\",\"WARC-Block-Digest\":\"sha1:LYX6MNEWH75PWBLIFMV344QLHGTHNLS3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662515501.4_warc_CC-MAIN-20220517031843-20220517061843-00264.warc.gz\"}"}