image goes here
Model specific
Model agnostic
library(tidymodels);library(rio);library(vip);library(kableExtra)
DataVaxFull=import("https://www.lange-analytics.com/AIBook/Data/DataVax.rds") %>%
mutate(RowNum=row.names(.))
DataVax= DataVaxFull%>%
select(PercVacFull, PercRep,
PercAsian, PercBlack, PercHisp,
PercYoung25, PercOld65,
PercFoodSt, Population) %>%
mutate(Population=frequency_weights(Population))
set.seed(2021)
Split85=DataVax %>% initial_split(prop = 0.85,
strata = PercVacFull,
breaks = 3)
DataTrain=training(Split85)
DataTest=testing(Split85)Lime, Shs, and the randomized variable approach (forgot name)
We use the same training and testing data as created in the prevha
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 0.9433753 | 0.0240164 | 39.2804433 | 0.0000000 |
| PercRep | -0.5515592 | 0.0164025 | -33.6264704 | 0.0000000 |
| PercAsian | 0.0407740 | 0.0435763 | 0.9356931 | 0.3495327 |
| PercBlack | -0.3771981 | 0.0207927 | -18.1408963 | 0.0000000 |
| PercHisp | 0.0645242 | 0.0136853 | 4.7148525 | 0.0000026 |
| PercYoung25 | -0.5999597 | 0.1060435 | -5.6576772 | 0.0000000 |
| PercOld65 | 0.1945055 | 0.0608728 | 3.1952800 | 0.0014165 |
| PercFoodSt | -0.1887113 | 0.0503212 | -3.7501339 | 0.0001813 |
ModelDesignRandFor=rand_forest(trees=1000, min_n=5, mtry=2) %>%
set_engine("ranger", num.threads = NumberOfCores, importance="impurity") %>%
set_mode("regression")
RecipeVax=recipe(PercVacFull~., data=DataTrain)
set.seed(2021)
WfModelVax=workflow() %>%
add_model(ModelDesignRandFor)%>%
add_recipe(RecipeVax) %>%
add_case_weights(Population) %>%
fit(DataTrain)
vip(extract_fit_parsnip(WfModelVax))
Carsten joins last:
Carsten joins second:
Carsten joins first:
Carsten’s average contribution (Shapley value):

Number of coalitions:
Number of joining scenarios:
SHAPley Values estimate contribution of players
SHAP Values estimate contribution of variables
Otherwise, they are the same!
library(DALEX)
ExplainerRandFor <-
DALEXtra::explain_tidymodels(
WfModelVax,
data = DataVax,
y = DataVax$PercVacFull,
label = "random forest",
verbose = FALSE
)
DataTrainPredictorVar=DataVax %>% select(-PercVacFull, -Population)
i=141
j=137
if (!DraftMode){
set.seed(777)
ShapValues1 = DALEX::predict_parts(
explainer = ExplainerRandFor,
new_observation = DataTrainPredictorVar[i,],
type = "shap",
B = 128)
ShapValues2 = DALEX::predict_parts(
explainer = ExplainerRandFor,
new_observation = DataTrainPredictorVar[j,],
type = "shap",
B = 128)}i=135
j=149
if (!DraftMode){
set.seed(777)
ShapValues1 = DALEX::predict_parts(
explainer = ExplainerRandFor,
new_observation = DataTrainPredictorVar[i,],
type = "shap",
B = 128)
ShapValues2 = DALEX::predict_parts(
explainer = ExplainerRandFor,
new_observation = DataTrainPredictorVar[j,],
type = "shap",
B = 128)}
