This is a barebone implementation of Friedman's partial dependence intended for developers. To get more information on partial dependence, see partial_dependence().

.pd(
  object,
  v,
  data,
  grid,
  pred_fun = stats::predict,
  trafo = NULL,
  which_pred = NULL,
  w = NULL,
  ...
)

Arguments

object

Fitted model.

v

Variable name in data to calculate partial dependence.

data

Matrix or data.frame.

grid

Vector or factor of values to calculate partial dependence for.

pred_fun

Prediction function, by default stats::predict. The function takes three arguments (names irrelevant): object, data, and ....

trafo

How should predictions be transformed? A function or NULL (default). Examples are log (to switch to link scale) or exp (to switch from link scale to the original scale). Applied after which_pred.

which_pred

If the predictions are multivariate: which column to pick (integer or column name). By default NULL (picks last column). Applied before trafo.

w

Optional vector with case weights.

...

Further arguments passed to pred_fun(), e.g., type = "response" in a glm() or (typically) prob = TRUE in classification models.

Value

Vector of partial dependence values in the same order as grid.

References

Friedman, Jerome H. 2001, Greedy Function Approximation: A Gradient Boosting Machine. Annals of Statistics 29 (5): 1189-1232. doi:10.1214/aos/1013203451.

Examples

fit <- lm(Sepal.Length ~ ., data = iris)
.pd(fit, "Sepal.Width", data = iris, grid = hist(iris$Sepal.Width)$mids)

#>  [1] 5.368602 5.467780 5.566958 5.666136 5.765313 5.864491 5.963669 6.062847
#>  [9] 6.162025 6.261202 6.360380 6.459558
.pd(fit, "Species", data = iris, grid = levels(iris$Species))
#> [1] 6.425687 5.702125 5.402189