I'm thinking of Julia Sigles example here:
There seems to be a generic one and a custom one- over the model-specific function predict().
Both with the tidymodels standard variable importance package VIP.
Reproduceable example (generic case, with a simple linear model):
`
library(lightgbm)
if (require("lightgbm")) {
library(AmesHousing)
library(janitor)
library(dplyr)
library(ggplot2)
library(rsample)
library(recipes)
library(parsnip)
library(tune)
library(dials)
library(workflows)
library(yardstick)
library(catboost)
library(tidymodels)
library(treesnip)
library(tidyverse)
library(vip)
info<-(.packages())
print(info)
}
set.seed(1234)
ames_data <- make_ames() %>%
janitor::clean_names()
ames_split <- rsample::initial_split(
ames_data,
prop = 0.8,
strata = sale_price
)
rec <- recipes::recipe(sale_price ~ ., data = training(ames_split)) %>%
recipes::step_other(all_nominal(), threshold = 0.01) %>%
recipes::step_nzv(all_nominal())
myPrep <- prep(rec)
spec <- linear_reg() %>%
set_mode("regression") %>%
set_engine("lm")
vipFit<-spec %>%
set_engine("lm") %>%
fit(sale_price ~ .,data = juice(myPrep))
impObj <- vipFit %>% vip::vi(scale=FALSE)
print(impObj)
`
Output:
A tibble: 193 x 3
Variable Importance Sign
1 second_flr_sf 11.5 POS
2 first_flr_sf 10.4 POS
3 misc_val 10.1 NEG
4 overall_qualVery_Excellent 9.04 POS
5 overall_qualExcellent 8.56 POS
6 neighborhoodStone_Brook 8.32 POS
7 neighborhoodNorthridge 7.51 POS
8 overall_condGood 6.84 POS
9 neighborhoodNorthridge_Heights 6.51 POS
10 overall_condVery_Good 6.44 POS
... with 183 more rows
Reproduceable example (custom case, with a simple linear model via pred_wrapper):
rec <- recipes::recipe(sale_price ~ ., data = training(ames_split)) %>%
recipes::step_other(all_nominal(), threshold = 0.01) %>%
recipes::step_nzv(all_nominal())
spec <- linear_reg() %>%
set_mode("regression") %>%
set_engine("lm")
final_model <- spec
final_wf <- workflow() %>%
add_model(final_model) %>%
add_recipe(rec)
final_fitted_wf <- final_wf %>% fit(data=training(ames_split))
myVip<-final_fitted_wf %>%
extract_fit_parsnip() %>%
vip(pred_wrapper = stats::predict, train = training(ames_split))
print(myVip)
Output