Explain Model Action Set

Compute the Shapley Values (Kernel SHAP Method) Using the linearExplainer Action

This section contains PROC CAS code.

Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts (SAS Viya: Machine Learning Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.

This example runs the linearExplainer action to explain a prediction made by a forest model by using the Kernel SHAP method.

The following DATA step creates the reference data table mycas.dmagecr in your CAS session, keeping only the variables age, amount, coapp, duration, foreign, job, and good_bad. 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;
    keep age amount coapp duration foreign job good_bad;
run;

The following DATA step creates the query data table mycas.query in your CAS session:

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

The following statements run 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;
   loadactionset "decisionTree";
   decisionTree.forestTrain   result    = r
                            / table     = '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 statements run the linearExplainer action and output the results to ODS tables:

proc cas;
   loadactionset "explainModel";
   explainModel.linearExplainer / table            = "DMAGECR"
                                  query            = "QUERY"
                                  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"}}
                                  ;
   run;
quit;

The table parameter names the input reference data table. The query parameter names the input query data table. 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 "Explainer Information" table, "Parameter Estimates" table, and "Explainer Fidelity" table that these statements produce are shown in Output 13.2.1, Output 13.2.2, and Output 13.2.3, respectively.

Output 13.2.1: Explainer Information

Results from explainModel.linearExplainer

Explainer Information
RowIdDescriptioncValueValue
DATAGENData Generation MethodReference Distribution.
DISTANCEDistance MethodSHAP Kernel.
EXPLAINERExplainer TypeRegression.
BINARYENCODINGBinary EncodingAll.
STANDARDIZEStandardize Parameter EstimatesNone.
INCLUDEMISSINGInclude Missing as a LevelNo.
SAMPLESIZENumber of Samples30003000
BINWIDTHBin Width0.10.1
SEEDSeed12341234


Output 13.2.2: Explainer Parameter Estimates

Parameter Estimates
VariableBinaryVariablecoappforeignjobEstimateQueryValueLowerBoundUpperBound
Intercept ...0.173327336...
age_bCode_AGE_...-0.0176158333129.86245314332.137546857
amount_bCode_AMOUNT_...-0.03527177834303147.72631243712.2736876
coapp_bCode_COAPP_1..-0.0572256461..
duration_bCode_DURATION_...-0.02677252422.79411855525.205881445
foreign_bCode_FOREIGN_.1.-0.0208757271..
job_bCode_JOB_..3-0.0155674231..


Output 13.2.3: Explainer Fidelity Information

Explainer Fidelity
ModelPredExplainerPredExplainerRMSE
0-1.57151E-60.1340876923


Compute the Shapley Values (Kernel SHAP Method) Using the linearExplainer Action

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the dmagecr data to the comma-separated-value (CSV) file dmagecr.csv, convert the query data to the CSV file query.csv, and then use the following code to load the CSV files into CAS:

s:loadtable{casLib="casuser", path="dmagecr.csv"}
s:loadtable{casLib="casuser", path="query.csv"}

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

This example runs the linearExplainer action to explain a prediction made by a forest model by using the Kernel SHAP method. 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.


                              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"}}

The following code runs the linearExplainer action:

                              table            = {name="DMAGECR"},
                              query            = {name="QUERY"},
                              modelTable       = {name="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"}}

The table parameter names the input reference data table. The query parameter names the input query data table. 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.

For example output from the action, see the SAS code examples.

Compute the Shapley Values (Kernel SHAP Method) Using the linearExplainer Action

This section contains R code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the dmagecr data to the comma-separated-value (CSV) file dmagecr.csv, convert the query data to the CSV file query.csv, and then use the following code to load the CSV files into CAS:

m <- cas.read.csv(s, "dmagecr.csv", casOut=list(name="dmagecr"))
m <- cas.read.csv(s, "query.csv", casOut=list(name="query"))

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

This example runs the linearExplainer action to explain a prediction made by a forest model by using the Kernel SHAP method. 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.


   s,
   table     = list(name ="DMAGECR"),
   seed      = 1234,
   saveState = list(name = 'FOREST_ASTORE',
                    replace = True),
   target    = 'good_bad',
   maxLevel  = 5,
   inputs    = list("age",
                    "amount",
                    "coapp",
                    "duration",
                    "foreign",
                    "job"),
   nominals  = list("coapp",
                    "foreign",
                    "job")

The following code runs the linearExplainer action:

   s,
   table            = list(name="DMAGECR"),
   query            = list(name="QUERY"),
   modelTable       = list(name="FOREST_ASTORE"),
   modelTableType   = "ASTORE",
   predictedTarget  = "P_good_badbad",
   seed             = 1234,
   preset           = "KERNELSHAP",
   inputs           = list("age",
                           "amount",
                           "coapp",
                           "duration",
                           "foreign",
                           "job"),
   nominals         = list("coapp",
                           "foreign",
                           "job")

The table parameter names the input reference data table. The query parameter names the input query data table. 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.

For example output from the action, see the SAS code examples.

Compute the Shapley Values (Kernel SHAP Method) Using the linearExplainer Action

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the dmagecr data to the comma-separated-value (CSV) file dmagecr.csv, convert the query data to the CSV file query.csv, and then use the following code to load the CSV files into CAS:

s.upload_file('dmagecr.csv')
s.upload_file('query.csv')

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

This example runs the linearExplainer action to explain a prediction made by a forest model by using the Kernel SHAP method. 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.

s.loadactionset(actionset="decisionTree")
s.forestTrain(
  table            = {"name" : "DMAGECR"},
  seed             = 123,
  inputs           = ["age", "amount", "coapp", "duration",
                     "foreign", "job"],
  target           = "good_bad",
  nominals         = ["coapp", "foreign", "job"],
  maxLevel         = 5,
  saveState        = {"name" : "FOREST_ASTORE", "replace" : True}
 )

The following code runs the linearExplainer action:

s.loadactionset(actionset="explainModel")
s.linearExplainer(
  table           = {"name" : "DMAGECR"},
  query           = {"name" : "QUERY"},
  modelTable      = {"name" : "FOREST_ASTORE"},
  modelTableType  = "ASTORE",
  predictedTarget = "P_good_badbad",
  seed            = 1234,
  preset          = "KERNELSHAP",
  inputs          = ["age", "amount", "coapp", "duration",
                     "foreign", "job"],
  nominals        = ["coapp", "foreign", "job"]
 )

The table parameter names the input reference data table. The query parameter names the input query data table. 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.

For example output from the action, see the SAS code examples.

Last updated: August 04, 2026