How to use `pdp:: partial()` in `tidymodels`

Hi,

I have a question about how to create a Partial Dependence Plots using pdppackage in tidymodels.

For example: Let use the case study in tidymodels website: https://www.tidymodels.org/start/case-study/.

Is there any way we can have a Partial Dependence Plots between predictor country and the predicted value.

Thank you so much in advance.

1 Like

This works for me but is not extensively tested. I'm not sure why we get more lines in the 2nd and 3rd plot.

library(tidymodels)
library(pdp)

## -----------------------------------------------------------------------------
# results should look like:

lm(mpg ~ wt + I(1/disp^2), data = mtcars) %>%
  partial(pred.var = "disp") %>%
  plotPartial()

## -----------------------------------------------------------------------------

# From ?partial:
# `pred.fun`: Optional prediction function that requires two arguments:
#             `object` and `newdata`. If specified, then the function must
#              return a single prediction or a vector of predictions (i.e., not
#              a matrix or data frame). Default is NULL.
pdp_pred_fun <- function(object, newdata) {
  predict(object, newdata, type = "numeric")$.pred
}

## -----------------------------------------------------------------------------

parsnip_fit <-
  linear_reg() %>%
  set_engine("lm") %>%
  fit(mpg ~ wt + I(1/disp^2), data = mtcars)

# testing:
pdp_pred_fun(parsnip_fit, mtcars[1:3,])

parsnip_partial <-
  partial(parsnip_fit,
          pred.var = "disp",
          pred.fun = pdp_pred_fun,
          train = mtcars)
plotPartial(parsnip_partial)

## -----------------------------------------------------------------------------

workflow_fit <-
  workflow() %>%
  add_model(linear_reg() %>% set_engine("lm")) %>%
  add_formula(mpg ~ wt + I(1/disp^2)) %>%
  fit(data = mtcars)

# testing:
pdp_pred_fun(workflow_fit, mtcars[1:3,])

workflow_partial <-
  partial(workflow_fit,
          pred.var = "disp",
          pred.fun = pdp_pred_fun,
          train = mtcars)
plotPartial(parsnip_partial)
1 Like

I forgot... this is also easy to do with recipes. step_profile() was made specifically for this:

grid <- 
  recipe(mpg ~ ., data = mtcars) %>% 
  step_profile(all_predictors(), -disp, profile = vars(disp)) %>% 
  prep() %>% 
  juice()

predict(workflow_fit, grid) %>% 
  bind_cols(grid %>% select(disp)) %>% 
  ggplot(aes(y = .pred, x = disp)) + 
  geom_path()
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.