Explain Model Action Set

Compute Partial Dependence for Nominal Target Variable

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 shows how to use the partialDependence action to estimate the partial dependence (PD) function for a nominal target variable. It also shows how to generate an ICE table.

You can load the hmeq data set into your CAS session by using the following DATA step. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref. In this example, an observation ID variable, _ID_, is added to facilitate plotting of ICE curves for individuals.

data mycas.hmeq;
    set sampsio.hmeq;
    _ID_ =_n_;
run;

The following statements run the forestTrain action in the decisionTree action set to estimate the probability that mortgage loan applicants will default on repayment of their loan. The action also outputs an analytic store named forestStore_hmeq.

  proc cas;
  action decisionTree.forestTrain /
      table      = {name    = "hmeq"},
      inputs     = {{name   = "Job"},
                    {name   = "Reason"},
                    {name   = "DebtInc"},
                    {name   = "Value"},
                    {name   = "Loan"},
                    {name   = "Mortdue"}},
      nominals   = {"Bad"},
      target     = "Bad",
      savestate  = {name    = "forestStore_hmeq",
                    replace = true},
      seed       = 1234
      ;
  run;
  quit;


Note that the target variable Bad, which has two numeric levels (0 and 1), is defined as a nominal variable.

The following statements run the partialDependence action and output the PD function to an ODS table:

   proc cas;
   action explainModel.partialDependence  result = pd_res  /
        table            = {name = "hmeq"},
        modelTable       = "forestStore_hmeq",
        inputs           = {{name    = "Job"},
                            {name    = "Reason"},
                            {name    = "DebtInc"},
                            {name    = "Value"},
                            {name    = "Loan"},
                            {name    = "Mortdue"}},
        predictedTarget  = "P_Bad1",
        analysisVariable = "DebtInc",
        outputTables     = {includeAll = true, replace = true},
        output           = {casout   = {name    = "ICEOut",
                                        replace = true},
                            copyvars = {"_ID_"}},
        seed             = 1234
        ;
        print pd_res;
   run;
   quit;

Here, DebtInc is specified as the analysis variable. The partialDependence action reads the hmeq data table and the analytic store created by the forestTrain action and outputs a partial dependence table that displays the mean prediction for Bad by DebtInc. Because DebtInc is an interval variable, the values of the variable are binned.

The output parameter requests a CAS table ("ICEOut") that contains ICE curves for each sampled individual. The copyvars parameter specifies that the variable _ID_ be copied to the output data table.

Output 12.6.1: Partial Dependence Table

pd_res: Results from explainModel.partialDependence

Partial Dependence
BinDEBTINCMean Prediction (P_Bad1)Standard Error
10.52449921540.09183380620.0031907677
223.0564602680.08253230140.0032174871
345.5884213210.77683614270.002170502
468.1203823740.77683614270.002170502
590.6523434270.77683614270.002170502
6113.184304480.77683614270.002170502
7135.716265530.77683614270.002170502
8158.248226590.77683614270.002170502
9180.780187640.77683614270.002170502
10203.312148690.77683614270.002170502


Compute Partial Dependence for Nominal Target Variable

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 hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:

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

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

The following code calls the forestTrain and partialDependence actions.

The forestTrain action builds a model to estimate the probability that mortgage loan applicants will default on repayment of their loan. The action also outputs an analytic store named forestStore_hmeq. Note that the target variable Bad, which has two numeric levels (0 and 1), is defined as a nominal variable.

The partialDependence action outputs the partial dependence (PD) function to an ODS table.

s:loadactionset{actionset="decisionTree"}
s:loadactionset{actionset="explainModel"}

s:decisionTree_forestTrain{
     inputs    = {{name   = "Job"},
                  {name   = "Reason"},
                  {name   = "DebtInc"},
                  {name   = "Value"},
                  {name   = "Loan"},
                  {name   = "Mortdue"}},
     nominals  = {"Bad"},
     savestate = {name    = "forestStore_hmeq" ,
                  replace = true},
     table     = {name    = "hmeq"},
     target    = "Bad",
     seed      = 1234
}

s:explainModel_partialDependence{
     analysisVariable = "DebtInc",
     inputs           = {{name      = "Job"},
                         {name      = "Reason"},
                         {name      = "DebtInc"},
                         {name      = "Value"},
                         {name      = "Loan"},
                         {name      = "Mortdue"}},
     modelTable       = "forestStore_hmeq",
     output           = {casout     = {name    = "ICEOut",
                                       replace = true},
                         copyvars   = {"_ID_"}},
     outputTables     = {includeAll = true,
                         replace    = true},
     predictedTarget  = "P_Bad1",
     table            = {name       = "hmeq"},
     seed             = 1234
}

s:fetch{table={name="ICEOut"}}

Here, DebtInc is specified as the analysis variable. The partialDependence action reads the hmeq data table and the analytic store created by the forestTrain action and outputs a partial dependence table that displays the mean prediction for Bad by value of DebtInc. Because DebtInc is an interval variable, the values of the variable are binned.

The output parameter requests a CAS table ("ICEOut") that contains ICE curves for each sampled individual. The copyvars parameter specifies that the variable _ID_ be copied to the output data table.

Compute Partial Dependence for Nominal Target Variable

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 hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:

s.upload_file('hmeq.csv')

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

The following code calls the forestTrain and partialDependence actions.

The forestTrain action builds a model to estimate the probability that mortgage loan applicants will default on repayment of their loan. The action also outputs an analytic store named forestStore_hmeq. Note that the target variable Bad, which has two numeric levels (0 and 1), is defined as a nominal variable.

The partialDependence action outputs the partial dependence (PD) function to an ODS table.

s.loadactionset("decisionTree")

s.decisionTree.forestTrain (
      table     = {"name":"hmeq"},
      inputs    = ["Job", "Reason", "DebtInc", "Value",
                   "Loan", "Mortdue"],
      nominals  = ["Bad"],
      target    = "Bad",
      savestate = {"name":"forestStore_hmeq", "replace":True},
      seed      = 1234
 )

s.loadactionset("explainModel")

s.explainModel.partialDependence (
        table            = "hmeq",
        modelTable       = {"name": "forestStore_hmeq"},
        inputs           = ["Job", "Reason", "DebtInc", "Value",
                            "Loan", "Mortdue"],
        predictedTarget  = "P_Bad1",
        analysisVariable = "DebtInc",
        outputTables     = {"includeAll": True, "replace": True},
        output           = {"casout":{"name": "ICEOut", "replace": True},
                            "copyVars":["_ID_"] },
        seed             = 1234
 )

Here, DebtInc is specified as the analysis variable. The partialDependence action reads the hmeq data table and the analytic store created by the forestTrain action and outputs a partial dependence table that displays the mean prediction for Bad by value of DebtInc. Because DebtInc is an interval variable, the values of the variable are binned.

The output parameter requests a CAS table ("ICEOut") that contains the ICE curves for each sampled individual. The copyvars parameter specifies that the variable _ID_ be copied to the output data table.

Compute Partial Dependence for Nominal Target Variable

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 hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:

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

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

The following code calls the forestTrain and partialDependence actions.

The forestTrain action builds a model to estimate the probability that mortgage loan applicants will default on repayment of their loan. The action also outputs an analytic store named forestStore_hmeq. Note that the target variable Bad, which has two numeric levels (0 and 1), is defined as a nominal variable.

The partialDependence action outputs the partial dependence (PD) function to an ODS table.

loadActionSet(s,'decisionTree')
loadActionSet(s,'explainModel')

rs <- cas.decisionTree.forestTrain(
     s,
     inputs    = list('Job',
                      'Reason',
                      'DebtInc',
                      'Value',
                      'Loan',
                      'Mortdue'),
     nominals  = list('Bad'),
     savestate = list(name    = 'forestStore_hmeq',
                      replace = true),
     table     = list(name    = 'hmeq'),
     target    = 'Bad',
     seed      = 1234
 )

rs <- cas.explainModel.partialDependence(
     s,
     analysisVariable = 'DebtInc',
     inputs           = list('Job',
                             'Reason',
                             'DebtInc',
                             'Value',
                             'Loan',
                             'Mortdue'),
     modelTable       = 'forestStore_hmeq',
     output           = list(casout     = list(name    = 'ICEOut',
                                               replace = true),
                             copyvars   = list('_ID_')),
     outputTables     = list(includeAll = true,
                             replace    = true),
     predictedTarget  = 'P_Bad1',
     table            = list(name='hmeq'
     seed             = 1234
 )


Here, DebtInc is specified as the analysis variable. The partialDependence action reads the hmeq data table and the analytic store created by the forestTrain action and outputs a partial dependence table that displays the mean prediction for Bad by value of DebtInc. Because DebtInc is an interval variable, the values of the variable are binned.

The output parameter requests a CAS table ("ICEOut") that contains ICE curves for each sampled individual. The copyvars parameter specifies that the variable _ID_ be copied to the output data table.

Last updated: June 10, 2021