Inherit or Copy ClassificationLinear

5 views (last 30 days)
Thomas Kirsh
Thomas Kirsh on 18 Apr 2023
Edited: Drew on 18 Apr 2023
I'm trying to use shapley to get the Shapley values for my logistic regression model. However, I got my model using glmfit and just have the corresponding coefficients b = [-7, 1,2,3,4,5]; while shapley only accepts objects for logistic regression made by the fitclinear function. These objects are ClassficationLinear. My idea is to convert my coefficients to this proper class type. I tried
mdl = ClassificationLinear;
mdl.Beta = b;
shapley(mdl, data);
which returns the error
Error using shapley
Blackbox model must be a classification model, regression model, or function
handle
How can I convert my logistic regression model to the proper class to be used in shapley? I'll eventually have to do this for a libsvm model too.

Answers (1)

Drew
Drew on 18 Apr 2023
Edited: Drew on 18 Apr 2023
When you do not have a classification model or regression model that is accepted by the shapley command, you can calculate Shapley values for an arbitrary model by specifying the model prediction function with a function handle, and passing that function handle to the MATLAB shapley function. See the example "Specify Blackbox Model Using Function Handle" on the Shapley doc page: https://www.mathworks.com/help/stats/shapley.html#mw_c8688099-08ba-41d5-8a6c-0785a609b341
In this case, since you have a model built with glmfit, it is probably easiest to first rebuild the model with fitglm, and then create a function handle to the predict function of the resulting GeneralizedLinearModel object, and pass that function handle to the shapley function. The "Alternative Functionality" section of the glmfit doc page https://www.mathworks.com/help/stats/glmfit.html#mw_156845ea-98b1-4232-a762-6aa05f8f0766 indicates: "glmfit is useful when you simply need the output arguments of the function or when you want to repeat fitting a model multiple times in a loop. If you need to investigate a fitted model further, create a generalized linear regression model object GeneralizedLinearModel by using fitglm or stepwiseglm. A GeneralizedLinearModel object provides more features than glmfit."
% Load data and build GLM. This model is like in the example at
% openExample('stats/FitaLogisticRegressionModelExample')
load hospital
fulldata = dataset2table(hospital);
% Only keep the variables being used
data=fulldata(:,[2 3 4 5]);
modelspec = 'Smoker ~ Age*Weight*Sex - Age:Weight:Sex';
mdl = fitglm(data,modelspec,'Distribution','binomial');
% Create a function handle to the predict function for this model
f = @(tbl) predict(mdl,tbl);
% Calculate shapley values using the function handle.
% This can be done all in one step (commented out)
% explainer=shapley(f,data(:,[1:3]),'CategoricalPredictors',[1],'q',data(1,:));
% Or, use separate calls to 'shapley' and 'fit'.
explainer=shapley(f,data(:,[1:3]),'CategoricalPredictors',[1]);
explainer=fit(explainer,data(1,:));
% View the ShapleyValues
explainer.ShapleyValues
ans = 3×2 table
Predictor ShapleyValue _________ ____________ "Sex" 0.043428 "Age" -0.001237 "Weight" 0.042887
% Plot the Shapley Values using the explainer plot method
plot(explainer)
If this answer helps you, please remember to accept the answer.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!