Explain Model Action Set

Compute the Shapley Values (Kernel SHAP Method) of Multiple Query Observations

This example runs the linearExplainer action to explain multiple predictions that are made by the same forest model by using the Kernel SHAP method. Because the data generation for Kernel SHAP is query-invariant, you can use the linearExplainer action to generate data for one of the query observations and reuse the generated data for other query observations. This saves computation time by not generating new data for each query observation. Furthermore, because the linearExplainer action allows Shapley value calculation without a model when you use prescored query and reference data tables, you can save scoring time by using prescored query data when you reuse the prescored generated data.

The following DATA step creates the reference data table mycas.dmagecr in your CAS session; adds the ID variable id; and keeps only the variables age, amount, coapp, duration, foreign, good_bad, id, and job. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

data mycas.dmagecr;
    set sampsio.dmagecr;
    id = _N_;
    keep age amount coapp duration foreign job good_bad id;
run;

The following DATA step creates the query data table mycas.queries, which consists of the first five rows of the data table mycas.dmagecr, in your CAS session:

data mycas.queries;
    set mycas.dmagecr;
    where id <= 5;
run;

The following code runs the forestTrain action in the decisionTree action set to build a forest model in order to predict whether the credit rating of each individual in the dmagecr data table is good or bad. The action also outputs an analytic store named forest_astore.

proc cas;
     decisionTree.forestTrain /
         table     = {name    ="dmagecr"},
         seed      = 1234,
         saveState = {name    = 'forest_astore',
                   replace = True},
         target    = 'good_bad',
         maxLevel  = 5,
         inputs    = {{name   = "age"},
                      {name   = "amount"},
                      {name   = "coapp"},
                      {name   = "duration"},
                      {name   = "foreign"},
                      {name   = "job"}},
         nominals  = {{name   = "coapp"},
                      {name   = "foreign"},
                      {name   = "job"}};
     run;
quit;

The following code runs the score action from the astore action set to score the forest model on the queries data table in order to produce the prescored query table queries_scored, while keeping all the input and ID variables:

proc cas;
    astore.score /
       table     = 'queries',
       rstore    = 'forest_astore',
       casout    = {name = 'queries_scored',
                    replace = True},
       copyVars  = {'age',
                    'amount',
                    'coapp',
                    'duration',
                    'foreign',
                    'job',
                    'id'};
   run;
quit;

The following code runs the linearExplainer action to explain the observation in which the value of id is 1 in the prescored query data table. The action also saves the scored generated data table named scored_generated_data in your CAS session and the parameter estimates table (Kernel SHAP values) named kernel_shap_1 in your SAS library.

proc cas;
     explainModel.linearExplainer result = r /
         table            = {name = "dmagecr"},
         query            = {name = "queries_scored",
                             where = "id = 1"},
         modelTable       = "FOREST_ASTORE",
         modelTableType   = "ASTORE",
         predictedTarget  = "P_good_badbad",
         seed             = 1234,
         preset           = "KERNELSHAP",
         inputs           = {{name = "age"},
                             {name = "amount"},
                             {name = "coapp"},
                             {name = "duration"},
                             {name = "foreign"},
                             {name = "job"}},
         nominals         = {{name = "coapp"},
                             {name = "foreign"},
                             {name = "job"}},
         generatedOut     = {name = "scored_generated_data",
                             replace = True};
     saveResult r["ParameterEstimates"] dataset = kernel_shap_1;
     run;
quit;
}

The table parameter names the training data table dmagecr as the input reference data table. The query parameter names the input query data table. The where subparameter specifies that the action use only the row in which the value of id is 1. The modelTable parameter names the model table. The modelTableType parameter specifies that the model table is an analytic store model table. The predictedTarget parameter specifies that the variable P_good_badbad be used as the predicted target variable. The seed parameter specifies the seed to use for pseudorandom number generation. The preset parameter specifies that the preset explanation method is the Kernel SHAP method. The inputs parameter specifies that the variables age, amount, coapp, duration, foreign, and job be used as inputs. The nominals parameter specifies that the variables coapp, foreign, and job be used as nominal variables. The generatedOut parameter specifies that the action save the scored generated data to a table named scored_generated_data in your CAS session. The SAVERESULT statement saves the parameter estimates table to a data table named kernel_shap_1 in your SAS library.

When explaining subsequent query observations that are scored by the same machine learning model, you can reuse the generated data table scored_generated_data and run the linearExplainer action without specifying a model. Note: In addition to generated data, the scored generated data table also includes a copy of the query observation. You can identify this observation by checking a generated variable named _queryID_ in the scored generated data table. For the copied query observation, the value of _queryID_ is 1. For generated data, the value of _queryID_ is –1. When reusing this table as a reference data table, you can remove or exclude the copied query observation.

The following code defines and runs a macro function named explainQueries that calls the linearExplainer action in a loop to explain the other observations in the prescored query data table:

%macro explainQueries(start, end, increment=1);
  %do index=&start. %to &end. %by &increment.;
    proc cas;
      explainModel.linearExplainer result = r /
        table            = {name = "scored_generated_data",
                            where = "_queryID_ ne 1"},
        query            = {name = "queries_scored",
                            where = "id=&index."},
        predictedTarget  = "P_good_badbad",
        seed             = 1234,
        preset           = "KERNELSHAP",
        inputs           = {{name = "age"},
                            {name = "amount"},
                            {name = "coapp"},
                            {name = "duration"},
                            {name = "foreign"},
                            {name = "job"}},
        nominals         = {{name = "coapp"},
                            {name = "foreign"},
                            {name = "job"}};
      saveResult r["ParameterEstimates"] dataset = kernel_shap_&index.;
    run;
    quit;
  %end;
%mend explainQueries;

%explainQueries(start=2, end=5);

In the macro function definition, the DO statement creates an iterative process in which it creates a macro variable named index that increments its value with each iteration. In each iteration, the following occurs:

  1. The linearExplainer action calculates the Shapley values for one of the query observations. The specific observation is chosen if the value of the id variable equals the value of the index variable.

    • The table parameter names the prescored generated data table scored_generated_data as the input reference data table. The where subparameter excludes the copied query observation.

    • The query parameter names the prescored query data table queries_scored as the input query data table. The where subparameter specifies that the action use only the row in which the value of id equals the value of index.

    • Note: Because the reference and query data tables are prescored and both contain the predicted target variable, you can run the linearExplainer action without specifying a model.

  2. The SAVERESULT statement saves the parameter estimates table to a data table. The name of the data table is based on the value of the index variable.

Compute the Shapley Values (Kernel SHAP Method) of Multiple Query Observations

This example is not available for the Lua programming language.

Compute the Shapley Values (Kernel SHAP Method) of Multiple Query Observations

This example is not available for the R programming language.

Compute the Shapley Values (Kernel SHAP Method) of Multiple Query Observations

This example is not available for the Python programming language.

Last updated: August 04, 2026