Project: SHAP
Companion Exercises for Practical Machine Learning with R
The interactive R environment and the required packages must be loaded in your browser. This typically takes 1–2 minutes. You can monitor the progress in the bottom-right corner.
When everything is loaded, you get a “Ready to roll” message below. Thank you for your patience!
To generate a SHAP diagram for a continental U.S. county of your choice, you have to proceed in two steps:
In Step 1 you enter a search string for the county of your choice, and you will get a CountyID as a result. This CountyID is the row in the data frame
DataVaxrepresenting the observation of your county, and you need to enter it in Step 2.In Step 2 you enter the CountyID in the second code chunk and execute the code. R will run in your browser and generate the SHAP diagram for you. This can take a while. So, be a little patient.
Step 1:
The code chunk below helps you to find the \(CountyID\) for the county you are interested in. You need the \(CountyID\) for the next step below to create the SHAP diagram.
To find the \(CountyID\), substitute the \(/dots\) in the code block below with the name or part of the name of the county you are interested in (do not include the word “county”!):
Here is a hint: To find the CountyID for Los Angeles County, you could use:
CountySearchStr=“Los Angeles” or CountySearchStr=“Angeles”
Do not include the word “County” in your search string!
Here is the solution for the County of Los Angeles:
CountySearchStr="Los Angeles"
######## Do not change code below this line ###################
VecMatchingCountyNames=grep(CountySearchStr, DataCountyInfo$County,
ignore.case=TRUE)
VecMatchingFIPS=grep(CountySearchStr, as.character(DataCountyInfo$Fips),
ignore.case=TRUE)
VecMatching=sort(c(VecMatchingCountyNames, VecMatchingFIPS))
unique(DataCountyInfo[VecMatching,])
DataMatchingCounties=DataCountyInfo[VecMatching,]
print(DataMatchingCounties)
CountySearchStr="Los Angeles"
######## Do not change code below this line ###################
VecMatchingCountyNames=grep(CountySearchStr, DataCountyInfo$County,
ignore.case=TRUE)
VecMatchingFIPS=grep(CountySearchStr, as.character(DataCountyInfo$Fips),
ignore.case=TRUE)
VecMatching=sort(c(VecMatchingCountyNames, VecMatchingFIPS))
unique(DataCountyInfo[VecMatching,])
DataMatchingCounties=DataCountyInfo[VecMatching,]
print(DataMatchingCounties)Step 2:
Here you can create the SHAP diagram and the related table with some extra info. All you have to do is substitute the \(\dots\) in the code block below with the CountyID that you got in Step 1 (the number only; no quotes).
Executing the code takes a while (depending on your computer between 1 – 8 minutes). You can change the B= argument (the number of scenarios to be calculated) to a lower value to speed things up, but the trade-off is a lower precision.
After you get the results, take a moment to interpret the Shap values and decide which SHAP values should not be interpreted (hint: take the value of the SHAP value and the box plot into account.)
Here is a hint: To generate the SHAP Diagram and table for Los Angeles, use:
CountyID=133
Here is the solution for the County of Los Angeles:
CountyID= 133
set.seed(2021)
SHAPObject=predict_parts(
explainer=ExplainerRandForest,
new_observation=DataVax[CountyID,],
type="shap", B=25)
### Do not change code below this line ##########
CountyPred=round(attr(SHAPObject, "prediction"),3)
VacRateCoalNone=round(attr(SHAPObject, "intercept"),3)
TitleShap=paste(substr(DataCountyInfo$County[CountyID],1,15),
"(PredVac=", CountyPred,
"Coalition: 'none'=", VacRateCoalNone,")")
SHAPObject$label=TitleShap
plot(SHAPObject)
ShapTable=SHAPObject |>
as_tibble() |>
filter(B==0) |>
select(Variable=variable_name, SHAP=contribution,
County=variable_value) |>
mutate(Nation=c(0.063,0.136,0.115,0.191,0.173,
0.478,0.310)) |>
mutate(County= round(as.numeric(County),3))|>
mutate(SHAP= round(SHAP,3))
kable(ShapTable, caption = TitleShap) |>
kable_styling(bootstrap_options=c("striped","hover"),
position="center", full_width = T)
CountyID= 133
set.seed(2021)
SHAPObject=predict_parts(
explainer=ExplainerRandForest,
new_observation=DataVax[CountyID,],
type="shap", B=25)
### Do not change code below this line ##########
CountyPred=round(attr(SHAPObject, "prediction"),3)
VacRateCoalNone=round(attr(SHAPObject, "intercept"),3)
TitleShap=paste(substr(DataCountyInfo$County[CountyID],1,15),
"(PredVac=", CountyPred,
"Coalition: 'none'=", VacRateCoalNone,")")
SHAPObject$label=TitleShap
plot(SHAPObject)
ShapTable=SHAPObject |>
as_tibble() |>
filter(B==0) |>
select(Variable=variable_name, SHAP=contribution,
County=variable_value) |>
mutate(Nation=c(0.063,0.136,0.115,0.191,0.173,
0.478,0.310)) |>
mutate(County= round(as.numeric(County),3))|>
mutate(SHAP= round(SHAP,3))
kable(ShapTable, caption = TitleShap) |>
kable_styling(bootstrap_options=c("striped","hover"),
position="center", full_width = T)