% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/explainPredictions.R
\name{explainPredictions}
\alias{explainPredictions}
\title{Step 2: Get multiple prediction breakdowns from a trained xgboost model}
\usage{
explainPredictions(xgb.model, explainer, data)
}
\arguments{
\item{xgb.model}{A trained xgboost model}

\item{explainer}{The output from the buildExplainer function, for this model}

\item{data}{A DMatrix of data to be explained}
}
\value{
A data table where each row is an observation in the data and each column is the impact of each feature on the prediction.

The sum of the row equals the prediction of the xgboost model for this observation (log-odds if binary response).
}
\description{
This function outputs the feature impact breakdown of a set of predictions made using an xgboost model.
}
\examples{
library(xgboost)
library(xgboostExplainer)

set.seed(123)

data(agaricus.train, package='xgboost')

X = as.matrix(agaricus.train$data)
y = agaricus.train$label

train_idx = 1:5000

train.data = X[train_idx,]
test.data = X[-train_idx,]

xgb.train.data <- xgb.DMatrix(train.data, label = y[train_idx])
xgb.test.data <- xgb.DMatrix(test.data)

param <- list(objective = "binary:logistic")
xgb.model <- xgboost(param =param,  data = xgb.train.data, nrounds=3)

col_names = colnames(X)

pred.train = predict(xgb.model,X)
nodes.train = predict(xgb.model,X,predleaf =TRUE)
trees = xgb.model.dt.tree(col_names, model = xgb.model)

#### The XGBoost Explainer
explainer = buildExplainer(xgb.model,xgb.train.data, type="binary", base_score = 0.5, trees = NULL)
pred.breakdown = explainPredictions(xgb.model, explainer, xgb.test.data)

showWaterfall(xgb.model, explainer, xgb.test.data, test.data,  2, type = "binary")
showWaterfall(xgb.model, explainer, xgb.test.data, test.data,  8, type = "binary")
}
