For each bin of var, the function fixes that feature at the bin midpoint
(numeric) or bin label (categorical), runs predict() across a sample of
the full dataset, and averages the predictions. The result shows the
marginal effect of var on model output, stripped of all correlations with
other features.
Usage
pdp(data, ...)
# Default S3 method
pdp(
data,
var,
obs,
model,
exposure = "exposure",
bins = 10L,
sample_size = 10000L,
type_agg = c("equal_exposure", "equal_range"),
model_name = "model",
ret = c("plot", "data"),
pre_process_fun = function(df) df,
feat_eng_fun = function(df) df,
post_process_fun = function(preds, df_raw) preds,
seed = 2024L,
verbose = FALSE,
...
)Arguments
- data
A
data.frameordata.table.- ...
Arguments passed to methods.
- var
[character(1)]Feature column to vary on the x-axis.- obs
[character(1)]Observed target column name.- model
A fitted model object. Standard R models (lm, glm, xgb, ranger, tidymodels workflows, etc.) and H2O models are supported automatically - no extra arguments needed.
- exposure
[character(1)]Exposure weight column. If absent, every row is given weight 1. Default"exposure".- bins
[integer(1)]Number of bins for numericvar. Default 10.- sample_size
[integer(1)]Rows to sample for PDP computation. Reducing this speeds up prediction at the cost of accuracy. Default 10,000. The full dataset is always used for the one-way actuals.- type_agg
[character(1)]Binning strategy:"equal_exposure"(default) or"equal_range".- model_name
[character(1)]Label shown in the plot legend. Default"model".- ret
[character(1)]"plot"(default) returns a plotly object;"data"returns the aggregated data.table.- pre_process_fun
function(df) -> dfapplied to the data before feature engineering. Default is the identity function.- feat_eng_fun
function(df) -> df(or matrix) applied after pre-processing to produce the model input. Default is the identity function.- post_process_fun
function(preds, df_raw) -> numericapplied to raw model predictions. Default is the identity function.- seed
[integer(1)]Seed for the PDP row sample, applied viawithr::with_seed()so the global RNG stream is left undisturbed. Default2024L.- verbose
[logical(1)]Announce the variable being computed? DefaultFALSE.
Value
A plotly object, or a data.table when ret = "data", or NULL
with a warning when the variable cannot be plotted.
Details
Alongside the PDP line the chart also shows:
Observed mean per bin (actual target, exposure-weighted)
Model average prediction per bin (in-sample, not PDP)
Global average observed and predicted reference lines
Yellow exposure bars (left axis) - identical style to
one_way()
See also
one_way() for observed-only one-way analysis.
Examples
# \donttest{
m <- lm(mpg ~ wt + hp + cyl, data = mtcars)
# Basic usage
pdp(mtcars, var = "wt", obs = "mpg", model = m)
# GLM - predict() is dispatched automatically
g <- glm(vs ~ wt + hp, data = mtcars, family = binomial)
pdp(mtcars, var = "wt", obs = "vs", model = g)
# Return aggregated data instead of a plot
pdp(mtcars, var = "wt", obs = "mpg", model = m, ret = "data")
#> wt obs_mean pred_mean exposure pdp_mean global_obs global_pred
#> <char> <num> <num> <int> <num> <num> <num>
#> 1: [1.513,1.835] 31.56667 28.36317 3 24.97806 20.09062 20.09062
#> 2: (1.835,2.2] 28.56667 27.02024 3 23.89020 20.09062 20.09062
#> 3: (2.2,2.62] 21.76667 24.73662 3 22.64716 20.09062 20.09062
#> 4: (2.62,2.875] 20.70000 22.46691 3 21.57831 20.09062 20.09062
#> 5: (2.875,3.215] 21.10000 21.10340 4 20.63614 20.09062 20.09062
#> 6: (3.215,3.44] 17.72500 18.69510 4 19.74147 20.09062 20.09062
#> 7: (3.44,3.52] 16.80000 18.80788 2 19.25850 20.09062 20.09062
#> 8: (3.52,3.73] 15.53333 15.17420 3 18.79929 20.09062 20.09062
#> 9: (3.73,3.845] 15.90000 15.50812 3 18.28466 20.09062 20.09062
#> 10: (3.845,5.424] 12.97500 11.57062 4 15.60223 20.09062 20.09062
# }