Explain Model Action Set

Modify DS2 code that has a DROP or KEEP statement

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 Visual Data Mining and 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 by using an analytic store and DS2 code. The example shows how to include DS2 code alongside an analytic store for model specification, as well as how to avoid a possible error.

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 describe action to generate DS2 code for the analytic store. Then the statements run the linearExplainer action and include the DS2 code in the action call. The DS2 code that is created from the describe action contains a KEEP statement that does not keep all the model inputs, thus causing the analysis in the linearExplainer action to fail.

%*;proc printto log=dummy; run;
proc cas;
   /* Create DS2 code */
   astore.describe result = r   / rstore           = "FOREST_ASTORE"
                                  epcode           = TRUE
                                  ;
   ds2_code = r['epcode'];
   run;

   /* Run linearExplainer action using the astore and DS2 code */
   loadactionset "explainModel";
   explainModel.linearExplainer / table            = "DMAGECR"
                                  query            = "QUERY"
                                  modelTable       = "FOREST_ASTORE"
                                  code             = ds2_code
                                  modelTableType   = "ASTORE"
                                  predictedTarget  = "P_good_badbad"
                                  seed             = 1234
                                  preset           = "LIME"
                                  inputs           = {{name = "age"},
                                                      {name = "amount"},
                                                      {name = "coapp"},
                                                      {name = "duration"},
                                                      {name = "foreign"},
                                                      {name = "job"}}
                                  nominals         = {{name = "coapp"},
                                                      {name = "foreign"},
                                                      {name = "job"}}
                                  ;
   run;
quit;
%*;proc printto; run;

The KEEP statement must include all input variables, and likewise a DROP statement cannot include any input variables. The full set of input variables is needed in the scored table as part of the analysis that the linearExplainer action performs. If you have a KEEP or DROP statement in your DS2 code, the linearExplainer action issues a warning. An error can occur if the necessary variables do not exist after the action uses the model for scoring.

There are several ways to ensure that your DS2 code is compatible with the linearExplainer action. You can manually save the DS2 code and edit any KEEP or DROP statements that it includes. Alternatively, you can use regular expressions to remove the KEEP or DROP statements.

The following statements run the linearExplainer action, while using PROC CAS to modify the DS2 code to remove the KEEP statement, and output the results to ODS tables. The regular expressions that are used here are examples, so they might not always work for the DS2 code that you have.

proc cas;
   /* Create DS2 code */
   astore.describe result = r   / rstore           = "FOREST_ASTORE"
                                  epcode           = TRUE
                                  ;
   ds2_code = r['epcode'];
   run;

   /* Remove KEEP statement */
   ds2_code = prxchange('s/Keep[^;]*;//s',-1,ds2_code);

   /* Run linearExplainer action using the astore and DS2 code */
   loadactionset "explainModel";
   explainModel.linearExplainer / table            = "DMAGECR"
                                  query            = "QUERY"
                                  modelTable       = "FOREST_ASTORE"
                                  code             = ds2_code
                                  modelTableType   = "ASTORE"
                                  predictedTarget  = "P_good_badbad"
                                  seed             = 1234
                                  preset           = "LIME"
                                  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 code parameter contains the string of DS2 code that is associated with the analytic store 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 global regression 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," "Explainer Parameter Estimates," and "Explainer Fidelity Information" tables that these statements produce are shown in Output 11.4.1, Output 11.4.2, and Output 11.4.3, respectively.

Output 11.4.1: Explainer Information

Results from explainModel.linearExplainer

Explainer Information
RowIdDescriptioncValueValue
DATAGENData Generation MethodQuery Centered.
DISTANCEDistance MethodNormalized Euclidean with Exponential Kernel.
EXPLAINERExplainer TypeRegression with LASSO.
BINARYENCODINGBinary EncodingNone.
STANDARDIZEStandardize Parameter EstimatesNone.
INCLUDEMISSINGInclude Missing as a LevelNo.
SAMPLESIZENumber of Samples30003000
EXPONENTIALKERNELExponential Kernel Divisor1.83711730711.8371173071
MIXEDWEIGHTMixed Distance Weight11
SEEDSeed12341234


Output 11.4.2: Explainer Parameter Estimates

Parameter Estimates
VariablecoappforeignjobEstimateQueryValue
Intercept...0.3283147386.
age...-0.00361468931
amount...7.4930917E-63430
coapp2..0.06443340380
coapp3..-0.0300385840
coapp1..01
duration...0.002472347424
foreign.2.-0.0798707910
foreign.1.01
job..40.00488124160
job..301


Output 11.4.3: Explainer Fidelity Information

Explainer Fidelity
ModelPredExplainerPredExplainerRMSE
0.29455725280.30129701510.0392614861


Modify DS2 code that has a DROP or KEEP statement

This example is not available for the Lua programming language.

Modify DS2 code that has a DROP or KEEP statement

This example is not available for the R programming language.

Modify DS2 code that has a DROP or KEEP statement

This example is not available for the Python programming language.

Last updated: November 04, 2020