Hello there,
Hope everyone is doing well. I have developed and validated a logistic regression model predicting hypertension for my thesis. For a given observation, I now want to visualize the contribution of predictors with Shapley additive explanation (Shap) plots using the iml R package. I try to run the following code, but run into the error in the photo at the bottom. I believe the error has something to do with using rcs but I still want to use my model with splines, interactions, replicate weights. There’s no missing data by the way.
Would appreciate any feedback!
Thanks,
Raf
# Set final models
male_final_model <- survey::svyglm(highbp14090_adj ~ rcs(clc_age, 4) + married + edudr04 + working + gendmhi + gen_025 + gen_045 + fmh_15 + rcs(hwmdbmi, 3) + rcs(whr, 3) + low_drink_score1 + rcs(minperweek, 3) + smoke + slp_11 + totalfv + diabx + ckd + rcs(clc_age, 4)*gen_045 + rcs(clc_age, 4)*rcs(hwmdbmi, 3) + rcs(clc_age, 4)*rcs(whr, 3) + rcs(clc_age, 4)*rcs(minperweek, 3) + rcs(clc_age, 4)*smoke + rcs(clc_age, 4)*slp_11 + rcs(clc_age, 4)*diabx + rcs(clc_age, 4)*ckd + rcs(hwmdbmi, 3)*rcs(whr, 3), design = weighted_male, family = quasibinomial())
# Create the Predictor object with 'response' to get probabilities
male_explainer <- iml::Predictor$new(
model = male_final_model, # Pass the model
data = male_data, # Pass the data
y = male_data[["highbp14090_adj"]], # Indicate outcome
type = "response" # Return predicted probabilities
)
# Create observation as a data frame
observation <- data.frame(
clc_age = 62,
married = 1,
edudr04 = 1,
working = 1,
gendmhi = 1,
gen_025 = 1,
gen_045 = 1,
fmh_15 = 1,
hwmdbmi = 20,
whr = 0.7,
low_drink_score1 = 2,
minperweek = 100,
smoke = 1,
slp_11 = 8,
totalfv = 5,
diabx = 1,
ckd = 0
)
# Compute SHAP values for observation
shap_values <- iml::Shapley$new(male_explainer, x.interest = observation)
# Visualize SHAP values for observation
shap_values$plot()