Bayesian Additive Regression Trees Action Set

Computing Predictive Margins Using the bartScoreMargin 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. 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 uses the model that is fit in Example 4.1, which is stored in the mycas.modelFit data table, and the testData data table that is created in Example 4.3 to demonstrate how to compute predictive margins.

The following statements show how you can compute predictive margins by using the bartScoreMargin action. The saved model mycas.modelFit is specified by the restore parameter, and the data table of observations to score is specified by the table parameter. Five predictive margins are specified by the margins parameter. The first scenario intervenes on x2 and sets its value to 0.25, and the second scenario intervenes on both x2 and x3 and sets their values to 0.25 and 0.5, respectively. The remaining three predictive margins intervene on x1 and set its level to 0.25, 0.5, and 0.75, respectively. These three predictive margins are then used in the differences parameter to request differences between these predictive margins. The marginInfo parameter produces a table summarizing the interventions that define the predictive margins.

proc cas;
bart.bartScoreMargin /
    differences={{evtMargin="x1Evt1", label="x1:0.5 - 0.25", refMargin="x1Ref"},
                 {evtMargin="x1Evt2", label="x1:0.75 - 0.25",
                  refMargin="x1Ref"}},
    marginInfo="true",
    margins={{at={{value="0.25", var="x2"}}, name="Scenario1"},
             {at={{value="0.25", var="x2"}, {value="0.5", var="x3"}},
              name="Scenario2"},
             {at={{value="0.25", var="x1"}}, name="x1Ref"},
             {at={{value="0.5", var="x1"}}, name="x1Evt1"},
             {at={{value="0.75", var="x1"}}, name="x1Evt2"}},
    restore="modelFit",
    table="testData";
run;

The "Score Information" table in Output 4.4.1 displays information about the saved model, the data table that was used to train the model, the data table of scored observations, and the number of scored observations.

Output 4.4.1: Score Information

Results from bart.bartScoreMargin

Scoring Information
StoreMODELFIT
Scoring TableTESTDATA
Training TableTRAINDATA
Target Variabley
Missing RuleSeparate
MCMC Sample Size1000
Number of Observations1000


The "Margin Information" table in Output 4.4.2 displays information about the interventions that define the predictive margins that are defined in the margins parameter.

Output 4.4.2: Margin Information

Predictive Margin Information
DescriptionVariable 1Value 1Variable 2Value 2
Scenario1x20.25  
Scenario2x20.25x30.5
x1Refx10.25  
x1Evt1x10.5  
x1Evt2x10.75  


The "Predictive Margins" table in Output 4.4.3 displays the means for the predicted margin estimates and equal-tail credible intervals for the interventions that are specified by the margins parameter.

Output 4.4.3: Predictive Margins

Predictive Margins
DescriptionEstimate95% Equal-Tail Interval
Scenario112.0729111.8613012.27063
Scenario211.1818610.9704811.39765
x1Ref12.2977512.1324412.48558
x1Evt115.2060814.9955915.45563
x1Evt216.6546116.4447016.91105


The "Predictive Margin Differences" table in Output 4.4.4 displays the mean difference in predictive margins that is specified in the differences parameter, along with equal-tail credible intervals.

Output 4.4.4: Predictive Margin Differences

Predictive Margin Differences
DescriptionEstimate95% Equal-Tail Interval
x1:0.5 - 0.252.90832.56043.1865
x1:0.75 - 0.254.35694.12024.6081


Computing Predictive Margins Using the bartScoreMargin Action

This section contains Lua code that creates the same modelFit table and performs the same analysis as shown in the CASL version of this example. For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Lua client to create the simulated data in your CAS session.

This example uses the model that is fit in Example 4.1, which is stored in the mycas.modelFit data table, and the testData data table that is created in Example 4.3 to demonstrate how to compute predictive margins.

The following statements show how you can compute predictive margins by using the bartScoreMargin action. The saved model mycas.modelFit is specified by the restore parameter, and the data table of observations to score is specified by the table parameter. Five predictive margins are specified by the margins parameter. The first scenario intervenes on x2 and sets its value to 0.25, and the second scenario intervenes on both x2 and x3 and sets their values to 0.25 and 0.5, respectively. The remaining three predictive margins intervene on x1 and set its level to 0.25, 0.5, and 0.75, respectively. These three predictive margins are then used in the differences parameter to request differences between these predictive margins. The marginInfo parameter produces a table summarizing the interventions that define the predictive margins.

s:bart_bartScoreMargin{
   table ='testData',
   restore = 'modelFit',
   marginInfo =true,
   margins= {
     { name='Scenario1', at={{var='x2', value=0.25}}},
     { name='Scenario2', at={{var='x2', value=0.25}, {var='x3',
     value=0.5}} },
     { name='x1Ref',     at={{var='x1', value=0.25}} },
     { name='x1Evt1',    at={{var='x1', value=0.5}} },
     { name='x1Evt2',    at={{var='x1', value=0.75}} }
   },
   differences = {
     { label='x1:0.5 - 0.25',  refMargin='x1Ref', evtMargin='x1Evt1'},
     { label='x1:0.75 - 0.25', refMargin='x1Ref', evtMargin='x1Evt2'}
   }
}

The "Score Information" table in Output 4.4.5 displays information about the saved model, the data table that was used to train the model, the data table of scored observations, and the number of scored observations.

Output 4.4.5: Score Information

Name1Label1cValue1nValue1
STOREStoreMODELFIT.
SCOREDATAScoring TableTESTDATA.
TRAINDATATraining TableTRAINDATA.
TARGETTarget Variabley.
MISSRULEMissing RuleSeparate.
NMCMCMCMC Sample Size10001000.000000
NOBSNumber of Observations10001000.000000


The "Margin Information" table in Output 4.4.6 displays information about the interventions that define the predictive margins that are defined in the margins parameter.

Output 4.4.6: Margin Information

DescriptionVariable1Value1Variable2Value2
Scenario1x20.25  
Scenario2x20.25x30.5
x1Refx10.25  
x1Evt1x10.5  
x1Evt2x10.75  


The "Predictive Margins" table in Output 4.4.7 displays the means for the predicted margin estimates and equal-tail credible intervals for the interventions that are specified by the margins parameter.

Output 4.4.7: Predictive Margins

DescriptionEstimateCredibleLowerCredibleUpper
Scenario112.0729111.8613012.27063
Scenario211.1818610.9704811.39765
x1Ref12.2977512.1324412.48558
x1Evt115.2060814.9955915.45563
x1Evt216.6546116.4447016.91105


The "Predictive Margin Differences" table in Output 4.4.8 displays the mean difference in predictive margins that is specified in the differences parameter, along with equal-tail credible intervals.

Output 4.4.8: Predictive Margin Differences

DescriptionRefMarginEvtMarginEstimateCredibleLowerCredibleUpper
x1:0.5 - 0.25x1Refx1Evt12.90832.56043.1865
x1:0.75 - 0.25x1Refx1Evt24.35694.12024.6081


Computing Predictive Margins Using the bartScoreMargin Action

This section contains Python code that creates the same modelFit table and performs the same analysis as shown in the CASL version of this example. For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Python client to create the simulated data in your CAS session.

This example uses the model that is fit in Example 4.1, which is stored in the mycas.modelFit data table, and the testData data table that is created in Example 4.3 to demonstrate how to compute predictive margins.

The following statements show how you can compute predictive margins by using the bartScoreMargin action. The saved model mycas.modelFit is specified by the restore parameter, and the data table of observations to score is specified by the table parameter. Five predictive margins are specified by the margins parameter. The first scenario intervenes on x2 and sets its value to 0.25, and the second scenario intervenes on both x2 and x3 and sets their values to 0.25 and 0.5, respectively. The remaining three predictive margins intervene on x1 and set its level to 0.25, 0.5, and 0.75, respectively. These three predictive margins are then used in the differences parameter to request differences between these predictive margins. The marginInfo parameter produces a table summarizing the interventions that define the predictive margins.

s.bart.bartscoremargin(
    differences=[dict(evtMargin='x1Evt1',
                      label='x1:0.5 - 0.25',
                      refMargin='x1Ref'),
                 dict(evtMargin='x1Evt2', label='x1:0.75 - 0.25',
                      refMargin='x1Ref')],
    marginInfo='true',
    margins=[dict(at=[dict(value='0.25', var='x2')],
                  name='Scenario1'),
             dict(at=[dict(value='0.25', var='x2'),
                      dict(value='0.5', var='x3')],
                  name='Scenario2'),
             dict(at=[dict(value='0.25', var='x1')],
                  name='x1Ref'),
             dict(at=[dict(value='0.5', var='x1')],
                  name='x1Evt1'),
             dict(at=[dict(value='0.75', var='x1')],
                  name='x1Evt2')],
    restore='modelFit',
    table='testData')

The "Score Information" table in Output 4.4.9 displays information about the saved model, the data table that was used to train the model, the data table of scored observations, and the number of scored observations.

Output 4.4.9: Score Information

Name1Label1cValue1nValue1
STOREStoreMODELFIT.
SCOREDATAScoring TableTESTDATA.
TRAINDATATraining TableTRAINDATA.
TARGETTarget Variabley.
MISSRULEMissing RuleSeparate.
NMCMCMCMC Sample Size10001000.000000
NOBSNumber of Observations10001000.000000


The "Margin Information" table in Output 4.4.10 displays information about the interventions that define the predictive margins that are defined in the margins parameter.

Output 4.4.10: Margin Information

DescriptionVariable1Value1Variable2Value2
Scenario1x20.25  
Scenario2x20.25x30.5
x1Refx10.25  
x1Evt1x10.5  
x1Evt2x10.75  


The "Predictive Margins" table in Output 4.4.11 displays the means for the predicted margin estimates and equal-tail credible intervals for the interventions that are specified by the margins parameter.

Output 4.4.11: Predictive Margins

DescriptionEstimateCredibleLowerCredibleUpper
Scenario112.0729111.8613012.27063
Scenario211.1818610.9704811.39765
x1Ref12.2977512.1324412.48558
x1Evt115.2060814.9955915.45563
x1Evt216.6546116.4447016.91105


The "Predictive Margin Differences" table in Output 4.4.12 displays the mean difference in predictive margins that is specified in the differences parameter, along with equal-tail credible intervals.

Output 4.4.12: Predictive Margin Differences

DescriptionRefMarginEvtMarginEstimateCredibleLowerCredibleUpper
x1:0.5 - 0.25x1Refx1Evt12.90832.56043.1865
x1:0.75 - 0.25x1Refx1Evt24.35694.12024.6081


Computing Predictive Margins Using the bartScoreMargin Action

This section contains R code that creates the same modelFit table and performs the same analysis as shown in the CASL version of this example. For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your R client to create the simulated data in your CAS session.

This example uses the model that is fit in Example 4.1, which is stored in the mycas.modelFit data table, and the testData data table that is created in Example 4.3 to demonstrate how to compute predictive margins.

The following statements show how you can compute predictive margins by using the bartScoreMargin action. The saved model mycas.modelFit is specified by the restore parameter, and the data table of observations to score is specified by the table parameter. Five predictive margins are specified by the margins parameter. The first scenario intervenes on x2 and sets its value to 0.25, and the second scenario intervenes on both x2 and x3 and sets their values to 0.25 and 0.5, respectively. The remaining three predictive margins intervene on x1 and set its level to 0.25, 0.5, and 0.75, respectively. These three predictive margins are then used in the differences parameter to request differences between these predictive margins. The marginInfo parameter produces a table summarizing the interventions that define the predictive margins.

cas.bart.bartScoreMargin(s,
    differences=list(list(evtMargin='x1Evt1',
                          label='x1:0.5 - 0.25',
                          refMargin='x1Ref'),
                     list(evtMargin='x1Evt2', label='x1:0.75 - 0.25',
                          refMargin='x1Ref')),
    marginInfo='true',
    margins=list(list(at=list(list(value='0.25', var='x2')),
                      name='Scenario1'),
                 list(at=list(list(value='0.25', var='x2'),
                              list(value='0.5', var='x3')),
                      name='Scenario2'),
                 list(at=list(list(value='0.25', var='x1')),
                      name='x1Ref'),
                 list(at=list(list(value='0.5', var='x1')),
                      name='x1Evt1'),
                 list(at=list(list(value='0.75', var='x1')),
                      name='x1Evt2')),
    restore='modelFit',
    table='testData')

The "Score Information" table in Output 4.4.13 displays information about the saved model, the data table that was used to train the model, the data table of scored observations, and the number of scored observations.

Output 4.4.13: Score Information

Name1Label1cValue1nValue1
STOREStoreMODELFIT.
SCOREDATAScoring TableTESTDATA.
TRAINDATATraining TableTRAINDATA.
TARGETTarget Variabley.
MISSRULEMissing RuleSeparate.
NMCMCMCMC Sample Size10001000.000000
NOBSNumber of Observations10001000.000000


The "Margin Information" table in Output 4.4.14 displays information about the interventions that define the predictive margins that are defined in the margins parameter.

Output 4.4.14: Margin Information

DescriptionVariable1Value1Variable2Value2
Scenario1x20.25  
Scenario2x20.25x30.5
x1Refx10.25  
x1Evt1x10.5  
x1Evt2x10.75  


The "Predictive Margins" table in Output 4.4.15 displays the means for the predicted margin estimates and equal-tail credible intervals for the interventions that are specified by the margins parameter.

Output 4.4.15: Predictive Margins

DescriptionEstimateCredibleLowerCredibleUpper
Scenario112.0729111.8613012.27063
Scenario211.1818610.9704811.39765
x1Ref12.2977512.1324412.48558
x1Evt115.2060814.9955915.45563
x1Evt216.6546116.4447016.91105


The "Predictive Margin Differences" table in Output 4.4.16 displays the mean difference in predictive margins that is specified in the differences parameter, along with equal-tail credible intervals.

Output 4.4.16: Predictive Margin Differences

DescriptionRefMarginEvtMarginEstimateCredibleLowerCredibleUpper
x1:0.5 - 0.25x1Refx1Evt12.90832.56043.1865
x1:0.75 - 0.25x1Refx1Evt24.35694.12024.6081


Last updated: May 21, 2026