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
| Scoring Information | |
|---|---|
| Store | MODELFIT |
| Scoring Table | TESTDATA |
| Training Table | TRAINDATA |
| Target Variable | y |
| Missing Rule | Separate |
| MCMC Sample Size | 1000 |
| Number of Observations | 1000 |
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 | ||||
|---|---|---|---|---|
| Description | Variable 1 | Value 1 | Variable 2 | Value 2 |
| Scenario1 | x2 | 0.25 | ||
| Scenario2 | x2 | 0.25 | x3 | 0.5 |
| x1Ref | x1 | 0.25 | ||
| x1Evt1 | x1 | 0.5 | ||
| x1Evt2 | x1 | 0.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 | |||
|---|---|---|---|
| Description | Estimate | 95% Equal-Tail Interval | |
| Scenario1 | 12.07291 | 11.86130 | 12.27063 |
| Scenario2 | 11.18186 | 10.97048 | 11.39765 |
| x1Ref | 12.29775 | 12.13244 | 12.48558 |
| x1Evt1 | 15.20608 | 14.99559 | 15.45563 |
| x1Evt2 | 16.65461 | 16.44470 | 16.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 | |||
|---|---|---|---|
| Description | Estimate | 95% Equal-Tail Interval | |
| x1:0.5 - 0.25 | 2.9083 | 2.5604 | 3.1865 |
| x1:0.75 - 0.25 | 4.3569 | 4.1202 | 4.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
| Name1 | Label1 | cValue1 | nValue1 |
|---|---|---|---|
| STORE | Store | MODELFIT | . |
| SCOREDATA | Scoring Table | TESTDATA | . |
| TRAINDATA | Training Table | TRAINDATA | . |
| TARGET | Target Variable | y | . |
| MISSRULE | Missing Rule | Separate | . |
| NMCMC | MCMC Sample Size | 1000 | 1000.000000 |
| NOBS | Number of Observations | 1000 | 1000.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
| Description | Variable1 | Value1 | Variable2 | Value2 |
|---|---|---|---|---|
| Scenario1 | x2 | 0.25 | ||
| Scenario2 | x2 | 0.25 | x3 | 0.5 |
| x1Ref | x1 | 0.25 | ||
| x1Evt1 | x1 | 0.5 | ||
| x1Evt2 | x1 | 0.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
| Description | Estimate | CredibleLower | CredibleUpper |
|---|---|---|---|
| Scenario1 | 12.07291 | 11.86130 | 12.27063 |
| Scenario2 | 11.18186 | 10.97048 | 11.39765 |
| x1Ref | 12.29775 | 12.13244 | 12.48558 |
| x1Evt1 | 15.20608 | 14.99559 | 15.45563 |
| x1Evt2 | 16.65461 | 16.44470 | 16.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
| Description | RefMargin | EvtMargin | Estimate | CredibleLower | CredibleUpper |
|---|---|---|---|---|---|
| x1:0.5 - 0.25 | x1Ref | x1Evt1 | 2.9083 | 2.5604 | 3.1865 |
| x1:0.75 - 0.25 | x1Ref | x1Evt2 | 4.3569 | 4.1202 | 4.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
| Name1 | Label1 | cValue1 | nValue1 |
|---|---|---|---|
| STORE | Store | MODELFIT | . |
| SCOREDATA | Scoring Table | TESTDATA | . |
| TRAINDATA | Training Table | TRAINDATA | . |
| TARGET | Target Variable | y | . |
| MISSRULE | Missing Rule | Separate | . |
| NMCMC | MCMC Sample Size | 1000 | 1000.000000 |
| NOBS | Number of Observations | 1000 | 1000.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
| Description | Variable1 | Value1 | Variable2 | Value2 |
|---|---|---|---|---|
| Scenario1 | x2 | 0.25 | ||
| Scenario2 | x2 | 0.25 | x3 | 0.5 |
| x1Ref | x1 | 0.25 | ||
| x1Evt1 | x1 | 0.5 | ||
| x1Evt2 | x1 | 0.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
| Description | Estimate | CredibleLower | CredibleUpper |
|---|---|---|---|
| Scenario1 | 12.07291 | 11.86130 | 12.27063 |
| Scenario2 | 11.18186 | 10.97048 | 11.39765 |
| x1Ref | 12.29775 | 12.13244 | 12.48558 |
| x1Evt1 | 15.20608 | 14.99559 | 15.45563 |
| x1Evt2 | 16.65461 | 16.44470 | 16.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
| Description | RefMargin | EvtMargin | Estimate | CredibleLower | CredibleUpper |
|---|---|---|---|---|---|
| x1:0.5 - 0.25 | x1Ref | x1Evt1 | 2.9083 | 2.5604 | 3.1865 |
| x1:0.75 - 0.25 | x1Ref | x1Evt2 | 4.3569 | 4.1202 | 4.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
| Name1 | Label1 | cValue1 | nValue1 |
|---|---|---|---|
| STORE | Store | MODELFIT | . |
| SCOREDATA | Scoring Table | TESTDATA | . |
| TRAINDATA | Training Table | TRAINDATA | . |
| TARGET | Target Variable | y | . |
| MISSRULE | Missing Rule | Separate | . |
| NMCMC | MCMC Sample Size | 1000 | 1000.000000 |
| NOBS | Number of Observations | 1000 | 1000.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
| Description | Variable1 | Value1 | Variable2 | Value2 |
|---|---|---|---|---|
| Scenario1 | x2 | 0.25 | ||
| Scenario2 | x2 | 0.25 | x3 | 0.5 |
| x1Ref | x1 | 0.25 | ||
| x1Evt1 | x1 | 0.5 | ||
| x1Evt2 | x1 | 0.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
| Description | Estimate | CredibleLower | CredibleUpper |
|---|---|---|---|
| Scenario1 | 12.07291 | 11.86130 | 12.27063 |
| Scenario2 | 11.18186 | 10.97048 | 11.39765 |
| x1Ref | 12.29775 | 12.13244 | 12.48558 |
| x1Evt1 | 15.20608 | 14.99559 | 15.45563 |
| x1Evt2 | 16.65461 | 16.44470 | 16.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
| Description | RefMargin | EvtMargin | Estimate | CredibleLower | CredibleUpper |
|---|---|---|---|---|---|
| x1:0.5 - 0.25 | x1Ref | x1Evt1 | 2.9083 | 2.5604 | 3.1865 |
| x1:0.75 - 0.25 | x1Ref | x1Evt2 | 4.3569 | 4.1202 | 4.6081 |