How to get predictor contribution to R^2 ?
8 次查看(过去 30 天)
显示 更早的评论
As a sensitivity analysis I calculated the mean absolute Shapley values of each predictor for each observation. But this way the values don't directly represent the (contribution) percentage of each predictor (i.e. the values from all predictors sum to more than 100%) at any observation. I'm trying to compare my results to an article where the relaimpo R-package was used to determined the R^2 % for each predictor. Using that package, the contributions of deifferent independent variables sum to the total R^2 of the model. I'd like to get that same results in MATLAB.
Thanks.
load CP
load w
rng('default') % For reproducibility
hyperopts = struct('AcquisitionFunctionName','expected-improvement-plus');
[Mdl_CP] = fitrlinear(w,CP,'OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',hyperopts);
%%
% The Shapley function is not available in Matlab 2019a. Therefore it was executed
% in MATLAB Online using the latest software version.
for i = 1:size(w,1)
explainer_CP(i) = shapley(Mdl_CP,w,'QueryPoint',w(i,:)); % Determine predictor contribution for each observation in the dataset
ShapVal_CP(i,:) = (explainer_CP(i).ShapleyValues.ShapleyValue); % Isolate the Shapley values from the Shapley objects
end
normShapVal_CP = normalize(abs(ShapVal_CP),2,"range"); % Normalize the range of Shapley values to [0 1] for each prediction (the observations are the same as the predictions in this case because the observartions were used as QueryPoints)
meanShapVal_CP = mean(normShapVal_CP,1).*100; % Mean shapley value for individual predictors (weights)
bar(meanShapVal_CP)
title("Individual weight contribution to CP")
ylabel("Contribution(%)")
xlabel("weights")
%%
sum(meanShapVal_CP)
ans =
690.1947
1 个评论
Walter Roberson
2024-2-26
load CP
load w
rng('default') % For reproducibility
hyperopts = struct('AcquisitionFunctionName','expected-improvement-plus');
[Mdl_CP] = fitrlinear(w,CP,'OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',hyperopts);
%%
% The Shapley function is not available in Matlab 2019a. Therefore it was executed
% in MATLAB Online using the latest software version.
for i = 1:size(w,1)
explainer_CP{i} = shapley(Mdl_CP,w,'QueryPoint',w(i,:)); % Determine predictor contribution for each observation in the dataset
ShapVal_CP(i,:) = (explainer_CP{i}.ShapleyValues.ShapleyValue); % Isolate the Shapley values from the Shapley objects
end
normShapVal_CP = normalize(abs(ShapVal_CP),2,"range"); % Normalize the range of Shapley values to [0 1] for each prediction (the observations are the same as the predictions in this case because the observartions were used as QueryPoints)
meanShapVal_CP = mean(normShapVal_CP,1).*100; % Mean shapley value for individual predictors (weights)
bar(meanShapVal_CP)
title("Individual weight contribution to CP")
ylabel("Contribution(%)")
xlabel("weights")
%%
sum(meanShapVal_CP)
回答(1 个)
Vatsal
2024-2-26
Hi,
It appears that you are attempting to determine the relative importance of predictors in a model, similar to the 'relaimpo' package in R. To calculate the relative importance of each predictor, you can normalize the Shapley values so that they collectively sum to 100%.
Below is a modification to the code to achieve this:
for i = 1:size(w,1)
explainer_CP(i) = shapley(Mdl_CP,w,'QueryPoint',w(i,:)); % Determine predictor contribution for each observation in the dataset
ShapVal_CP(i,:) = (explainer_CP(i).ShapleyValues.ShapleyValue); % Isolate the Shapley values from the Shapley objects
end
absShapVal_CP = abs(ShapVal_CP); % Take the absolute value of the Shapley values
sumShapVal_CP = sum(absShapVal_CP,2); % Sum the absolute Shapley values for each observation
normShapVal_CP = absShapVal_CP ./ sumShapVal_CP; % Normalize the absolute Shapley values so they sum to 1 for each observation
meanShapVal_CP = mean(normShapVal_CP,1).*100; % Mean shapley value for individual predictors (weights)
bar(meanShapVal_CP)
title("Individual weight contribution to CP")
ylabel("Contribution(%)")
xlabel("weights")
I hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!