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)