| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.ggplot.R, R/xgb.plot.deepness.R |
| 3 |
\name{xgb.ggplot.deepness} |
| 4 |
\alias{xgb.ggplot.deepness} |
| 5 |
\alias{xgb.plot.deepness} |
| 6 |
\title{Plot model tree depth} |
| 7 |
\usage{ |
| 8 |
xgb.ggplot.deepness( |
| 9 |
model = NULL, |
| 10 |
which = c("2x1", "max.depth", "med.depth", "med.weight") |
| 11 |
) |
| 12 |
|
| 13 |
xgb.plot.deepness( |
| 14 |
model = NULL, |
| 15 |
which = c("2x1", "max.depth", "med.depth", "med.weight"), |
| 16 |
plot = TRUE, |
| 17 |
... |
| 18 |
) |
| 19 |
} |
| 20 |
\arguments{ |
| 21 |
\item{model}{Either an \code{xgb.Booster} model, or the "data.table" returned |
| 22 |
by \code{\link[=xgb.model.dt.tree]{xgb.model.dt.tree()}}.} |
| 23 |
|
| 24 |
\item{which}{Which distribution to plot (see details).} |
| 25 |
|
| 26 |
\item{plot}{Should the plot be shown? Default is \code{TRUE}.} |
| 27 |
|
| 28 |
\item{...}{Other parameters passed to \code{\link[graphics:barplot]{graphics::barplot()}} or \code{\link[graphics:plot.default]{graphics::plot()}}.} |
| 29 |
} |
| 30 |
\value{ |
| 31 |
The return value of the two functions is as follows: |
| 32 |
\itemize{ |
| 33 |
\item \code{xgb.plot.deepness()}: A "data.table" (invisibly). |
| 34 |
Each row corresponds to a terminal leaf in the model. It contains its information |
| 35 |
about depth, cover, and weight (used in calculating predictions). |
| 36 |
If \code{plot = TRUE}, also a plot is shown. |
| 37 |
\item \code{xgb.ggplot.deepness()}: When \code{which = "2x1"}, a list of two "ggplot" objects, |
| 38 |
and a single "ggplot" object otherwise. |
| 39 |
} |
| 40 |
} |
| 41 |
\description{ |
| 42 |
Visualizes distributions related to the depth of tree leaves. |
| 43 |
\itemize{ |
| 44 |
\item \code{xgb.plot.deepness()} uses base R graphics, while |
| 45 |
\item \code{xgb.ggplot.deepness()} uses "ggplot2". |
| 46 |
} |
| 47 |
} |
| 48 |
\details{ |
| 49 |
When \code{which = "2x1"}, two distributions with respect to the leaf depth |
| 50 |
are plotted on top of each other: |
| 51 |
\enumerate{ |
| 52 |
\item The distribution of the number of leaves in a tree model at a certain depth. |
| 53 |
\item The distribution of the average weighted number of observations ("cover") |
| 54 |
ending up in leaves at a certain depth. |
| 55 |
} |
| 56 |
|
| 57 |
Those could be helpful in determining sensible ranges of the \code{max_depth} |
| 58 |
and \code{min_child_weight} parameters. |
| 59 |
|
| 60 |
When \code{which = "max.depth"} or \code{which = "med.depth"}, plots of either maximum or |
| 61 |
median depth per tree with respect to the tree number are created. |
| 62 |
|
| 63 |
Finally, \code{which = "med.weight"} allows to see how |
| 64 |
a tree's median absolute leaf weight changes through the iterations. |
| 65 |
|
| 66 |
These functions have been inspired by the blog post |
| 67 |
\url{https://github.com/aysent/random-forest-leaf-visualization}. |
| 68 |
} |
| 69 |
\examples{ |
| 70 |
|
| 71 |
data(agaricus.train, package = "xgboost") |
| 72 |
## Keep the number of threads to 2 for examples |
| 73 |
nthread <- 2 |
| 74 |
data.table::setDTthreads(nthread) |
| 75 |
|
| 76 |
## Change max_depth to a higher number to get a more significant result |
| 77 |
model <- xgboost( |
| 78 |
agaricus.train$data, factor(agaricus.train$label), |
| 79 |
nrounds = 50, |
| 80 |
max_depth = 6, |
| 81 |
nthreads = nthread, |
| 82 |
subsample = 0.5, |
| 83 |
min_child_weight = 2 |
| 84 |
) |
| 85 |
|
| 86 |
xgb.plot.deepness(model) |
| 87 |
xgb.ggplot.deepness(model) |
| 88 |
|
| 89 |
xgb.plot.deepness( |
| 90 |
model, which = "max.depth", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2 |
| 91 |
) |
| 92 |
|
| 93 |
xgb.plot.deepness( |
| 94 |
model, which = "med.weight", pch = 16, col = rgb(0, 0, 1, 0.3), cex = 2 |
| 95 |
) |
| 96 |
|
| 97 |
} |
| 98 |
\seealso{ |
| 99 |
\code{\link[=xgb.train]{xgb.train()}} and \code{\link[=xgb.model.dt.tree]{xgb.model.dt.tree()}}. |
| 100 |
} |