Data Science Pilot Action Set

Detecting Interacting Variables in Binary Classification Data

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.

The following statements run the detectInteractions action to find interactions between predictor variables in a data table for a binary classification task.

These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.

The action produces a data table that contains a list of interacting variable pairs together with their probability score for interaction. The output table name is intdet1_out. The following DATA step creates the input data table, mycas.syntheticdata, in your CAS session. This data table contains 10 variables: Y is the response variable, and X1 through X9 are the predictor variables.

data mycas.syntheticdata;
   input Y X1 X2 X3 X4 X5 X6 X7 X8 X9;
   cards;
     1  1   1  -1   1  -1  -1  -1  -1  -1
     1  1   1   1  -1  -1   1   1  -1   1
     1  1   1   1  -1  -1   1  -1   1   1
     1  1   1   1  -1   1  -1   1  -1   1
     1  1   1  -1   1   1   1  -1  -1  -1
     1  1   1   1  -1  -1   1   1  -1   1
     1  1   1   1  -1  -1   1  -1   1  -1
     1  1   1   1  -1   1  -1  -1   1   1
     1  1   1  -1   1  -1  -1   1   1   1
     1  1   1  -1   1  -1  -1  -1  -1   1
    -1  1  -1  -1   1  -1  -1  -1  -1  -1
    -1  1  -1   1  -1  -1   1   1  -1   1
    -1  1  -1   1  -1  -1   1  -1   1   1
    -1  1  -1   1  -1   1  -1   1  -1   1
    -1  1  -1  -1   1   1   1  -1  -1  -1
    -1  1  -1   1  -1  -1   1   1  -1   1
    -1  1  -1   1  -1  -1   1  -1   1  -1
    -1  1  -1   1  -1   1  -1  -1   1   1
    -1  1  -1  -1   1  -1  -1   1   1   1
    -1  1  -1  -1   1  -1  -1  -1  -1   1
   ;
run;

The following PROC CAS statements run the detectInteractions action and display the results in two ODS tables:

proc cas;
   loadactionset "dataSciencePilot";
   action detectInteractions /
     table                     = {name="syntheticdata"},
     target                    = "Y",
     event                     = "1",
     seed                      = 123,
     sparseInputs              = false,
     inputs                    = {"X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9"},
     numPairs                  = 0,
     numProjections            = 100,
     maxNumInteractions        = 1000,
     maxDistance               = 1e-8,
     minInteractionProbability = 0.01,
     casOut                    = {name="intdet1_out", replace=True};
   run;
   fetch / table = "intdet1_out";
   run;
quit;

The table parameter names the input data table. The target parameter specifies the name of the variable from the input data table to treat as the response variable. The event parameter specifies the label for observations to treat as the positive class (plus 1). The seed parameter initializes the pseudorandom number generator for the action to use. The sparseInputs parameter specifies that the input data are not in sparse format. The inputs parameter specifies which set of numeric (interval) variables from the input data table the action is to consider for interaction analysis. The numPairs parameter specifies the number of background pairs (or the number of pairs of columns) to be used by the XYZ algorithm to estimate the average interaction strength between all input variables under consideration. The numProjections parameter specifies the number of random projections that the XYZ algorithm must use in order to obtain the final list of promising candidates. The maxNumInteractions parameter specifies the maximum number of potential candidates to consider per random projection. The maxDistance parameter specifies the threshold of the maximum distance between two variables for them to be considered as interacting or similar. The minInteractionProbability parameter specifies the threshold of the minimum interaction strength, or interaction probability, that is used to create the final list of interacting variables. The casOut parameter specifies the name of the table to be used to display the list of interacting variables detected by the detectInteractions action.

The CAS output table is shown in Figure 2.

Figure 2: Interaction Variables

Results from table.fetch

Selected Rows from Table INTDET1_OUT
_Index_FirstVarIDFirstVarNameSecondVarIDSecondVarNameGamma
11X12X21
22X25X50.4
32X29X90.4
42X23X30.2
52X24X40.2
62X27X70.2
72X28X80.2


Detecting Interacting Variables in Regression Data

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.

Like the preceding example, this example shows how to use the detectInteractions action to find interactions between predictor variables in a data table for a regression task. In order for this action to handle data for regression tasks, the event parameter should not be specified.

These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.

The following DATA step creates the input data table, mycas.syntheticdata, in your CAS session. This data table contains 11 variables: Y is the response variable, and X1 through X10 are the predictor variables.

Remark  In practice, and for some regression problems, it might be helpful to standardize the response to have zero mean and unit variance before going through the interaction analysis.

data mycas.syntheticdata;
   input Y X1 X2 X3 X4 X5 X6 X7 X8 X9 X10;
   cards;
0.92656   -1  1  1  1 -1  -1  -1   1  1  -1
7.06509    1  1  1 -1  1  -1  -1  -1  1   1
3.96648   -1 -1  1 -1 -1  -1  -1  -1  1   1
1.28011    1 -1 -1  1  1  -1   1   1  1  -1
0.37873   -1  1  1  1  1  -1   1   1  1   1
4.91928    1  1 -1  1  1  -1  -1  -1 -1  -1
5.61293   -1 -1  1 -1 -1  -1   1  -1 -1  -1
3.66434    1  1  1  1  1   1   1   1 -1   1
4.71154    1  1  1 -1  1  -1   1  -1 -1   1
4.01789   -1 -1  1  1  1  -1  -1   1 -1  -1
1.62021    1 -1  1 -1 -1   1   1  -1 -1   1
6.01789    1  1 -1  1 -1  -1  -1  -1  1   1
4.91928    1  1  1  1 -1   1  -1   1  1  -1
3.66434    1  1 -1  1 -1  -1   1   1 -1  -1
-0.7199   -1  1 -1  1  1  -1   1   1  1  -1
1.62021    1 -1 -1 -1  1  -1   1  -1 -1   1
5.27284   -1 -1  1  1 -1   1   1  -1  1   1
2.02517   -1  1  1  1  1  -1  -1  -1  1   1
7.06509    1  1 -1 -1 -1   1  -1  -1  1  -1
5.66434   -1 -1  1  1  1   1   1  -1 -1   1
0.32731   -1  1  1 -1 -1   1   1  -1  1   1
1.62021    1 -1  1 -1 -1   1   1  -1 -1  -1
3.66434    1  1 -1  1 -1  -1   1  -1 -1  -1
5.27284   -1 -1 -1  1  1  -1   1  -1  1  -1
1.97376   -1  1 -1 -1  1   1  -1   1  1  -1
6.01789    1  1 -1  1 -1  -1  -1   1 -1  -1
2.02517   -1  1  1  1  1   1  -1  -1 -1   1
6.01789    1  1 -1  1 -1   1  -1   1  1  -1
4.37145    1  1 -1  1 -1  -1   1   1  1  -1
0.92656   -1  1 -1  1  1   1  -1  -1 -1   1
;
run;

The following statements run the detectInteractions action with default parameters and display the results in two ODS tables. Note that the event parameter is not specified, so the action treat this as a regression task.

proc cas;
   loadactionset "dataSciencePilot";
   action detectInteractions /
     table                     = {name="syntheticdata"},
     target                    = "Y",
     numProjections            = 100,
     maxNumInteractions        = 1000,
     casOut                    = {name="intdet2_out", replace=true};
   run;
   fetch / table = "intdet2_out";
   run;
quit;

The CAS output table is shown in Figure 3.

Figure 3: Interaction Variables

Results from table.fetch

Selected Rows from Table INTDET2_OUT
_Index_FirstVarIDFirstVarNameSecondVarIDSecondVarNameGamma
11X12X22.5967213333
23X310X101.8195593333
31X13X31.460066
48X810X101.3838106667
55X510X101.2670806667
67X79X91.25765
74X48X81.0128486667
81X15X50.905882
93X35X50.894416
103X34X40.893436
111X110X100.7447706667
121X17X70.7251053333
134X410X100.7138593333
142X24X40.6869406667
153X38X80.6374606667
162X28X80.5939653333
173X37X70.5903926667
186X68X80.566408
193X36X60.5511486667
201X18X80.538268


Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format

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.

The following statements run the detectInteractions action to find interactions between predictor variables in sparse format in a data table for a classification task.

These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.

The following DATA step loads the home equity data table (hmeq), which has 5,906 observations, 12 input variables, and 1 binary target variable (column) named bad.

data mycas.hmeq;
   set sampsio.hmeq;
run;

The next steps prepare the sparse input table for the detectInteractions action, determine the number of input levels for each nominal variable, and exclude observations that have missing values.

The following code calls the detectInteractions action by using the sparse parameter. Note that the inputs and inputLevels parameters must be specified when the sparse parameter is set to True.

proc cas;
   session mysess;
   transform /
      table = 'hmeq'
      pipelines = {
                   {
                     inputs     = ${loan, mortdue}
                     discretize = {method = 'quantile', args={nbins=5}}
                   },

                   {
                     inputs   = ${job, reason}
                     cattrans = {method = 'label'}
                   }
                 }
      copyvars                = ${bad}
      outVarsNameGlobalPrefix = ''
      casout                  = {name='transformed_hmeq' replace=True};
   run;

   dataSciencePilot.detectInteractions /
      table       = 'transformed_hmeq'
      target      = 'bad'
      event       = '1'
      sparse      = True
      inputs      = ${loan, mortdue, job, reason}
      inputLevels = {5, 5, 6, 2}
      casout      = {name = 'intdet3_out', replace=True};
   run;
   fetch / table = "intdet3_out";
   run;
quit;

The CAS output table is shown in Figure 4.

Figure 4: Interacting Variables

Results from table.fetch

Selected Rows from Table INTDET3_OUT
_Index_FirstVarIDFirstVarNameSecondVarIDSecondVarNameGamma
11LOAN_115JOB_50.457276629
21LOAN_116JOB_60.4346468982
32LOAN_215JOB_50.339055794
43LOAN_315JOB_50.3347639485
51LOAN_111JOB_10.3312524385
61LOAN_17MORTDUE_20.321888412
74LOAN_415JOB_50.3211080765
85LOAN_516JOB_60.3195474054
94LOAN_416JOB_60.3179867343
102LOAN_216JOB_60.3164260632
113LOAN_316JOB_60.3144752243
125LOAN_515JOB_50.3094030433
131LOAN_18MORTDUE_30.3019898556
145LOAN_510MORTDUE_50.2961373391
151LOAN_112JOB_20.2902848225
161LOAN_19MORTDUE_40.2750682794
174LOAN_411JOB_10.2707764339
185LOAN_56MORTDUE_10.2684354272
191LOAN_117REASON_10.2567303941
201LOAN_118REASON_20.2567303941


Detecting Interacting Variables in Binary Classification Data

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

s.upload_file('syntheticdata.csv')

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

s.loadactionset('dataSciencePilot')
s.detectInteractions(
        table                     = casDataTableName,
        target                    = 'Y',
        event                     = '1',
        sparseInputs              = False,
        numPairs                  = 0,
        maxNumInteractions        = 1000,
        numProjections            = 100,
        maxDistance               = 1.0e-8,
        minInteractionProbability = 0.01,
        casOut                    = {"name" : "detint1_out", "replace" : True}
                    )
s.fetch(table = {"name": "detint1_out"})

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

Detecting Interacting Variables in Regression Data

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

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


s.loadactionset('dataSciencePilot')
s.detectInteractions(
        table                     = casDataTableName,
        target                    = 'Y',
        sparseInputs              = False,
        numPairs                  = 0,
        maxNumInteractions        = 1000,
        numProjections            = 100,
        maxDistance               = 1.0e-8,
        minInteractionProbability = 0.01,
        casOut                    = {"name" : "detint2_out", "replace" : True}
                    )
s.fetch(table = {"name": "detint2_out"})

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

Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format

This example is not available for the Python programming language.

Detecting Interacting Variables in Binary Classification Data

This example is not available for the Lua programming language.

Detecting Interacting Variables in Regression Data

This example is not available for the Lua programming language.

Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format

This example is not available for the Lua programming language.

Detecting Interacting Variables in Binary Classification Data

This example is not available for the R programming language.

Detecting Interacting Variables in Regression Data

This example is not available for the R programming language.

Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format

This example is not available for the R programming language.

Last updated: November 04, 2020