?> bartScoreMargin - WeAreCAS
bart

bartScoreMargin

Description

The bartScoreMargin action computes predictive margins by using a fitted Bayesian additive regression trees (BART) model. Predictive margins are predictions from a model at fixed values of some predictors, averaged over the distribution of the other predictors. This technique is useful for understanding the effect of a specific predictor on the outcome, while accounting for the influence of other variables in the model.

bart.bartScoreMargin { alpha=double, casOut={casouttable}, differences={{bartScoreMargin_scoreDiff-1} <, {bartScoreMargin_scoreDiff-2}, ...>}, display={displayTables}, marginInfo=TRUE | FALSE, margins={{bartScoreMargin_evaluate-1} <, {bartScoreMargin_evaluate-2}, ...>}, model={castable}, outputTables={outputTables}, seed=64-bit-integer, table={castable} };
Settings
ParameterDescription
alphaSpecifies the significance level for constructing equal-tail credible limits. The default is 0.05.
casOutSpecifies the output data table to store the computed predictive margins.
differencesSpecifies a list of differences between predictive margins to compute. Each difference is defined by a reference margin (refMargin) and an event margin (evtMargin).
displaySpecifies which result tables to display. By default, all tables are displayed.
marginInfoWhen set to TRUE, requests a summary table of the variables and their values that define each predictive margin.
marginsSpecifies one or more predictive margins to compute. Each margin is defined by a name and a set of variable values ('at' subparameter).
modelSpecifies the CAS table that contains the fitted BART model information, saved from a previous call to the bartGauss or bartProbit action.
outputTablesSpecifies which result tables to save as CAS tables.
seedSpecifies the seed for the pseudorandom number generator to ensure reproducibility.
tableSpecifies the input data table used for computing the predictive margins. This is typically the same data used to train the model.
Data Preparation
Data Creation

This example first creates a sample dataset 'getStarted' with a binary outcome 'y' and several predictor variables. Then, it fits a Bayesian additive regression trees model for a binary outcome using the `bartProbit` action and saves the model to a CAS table named 'my_bart_model'. This saved model is required for the `bartScoreMargin` action.

data mycas.getStarted;
  call streaminit(123);
  do i = 1 to 100;
    x1 = rand('UNIFORM');
    x2 = rand('UNIFORM');
    x3 = rand('UNIFORM');
    if (i <= 50) then x4 = 'A'; else x4 = 'B';
    p = 1 / (1 + exp(-(x1 - 0.5*x2 + 0.2*x3)));
    y = rand('BERNOULLI', p);
    output;
  end;
run;

proc cas;
  bart.bartProbit table={name='getStarted'},
    model={depVars={{name='y', levelType='BINARY'}},
           effects={{vars={'x1', 'x2', 'x3', 'x4'}}}},
    store={name='my_bart_model', replace=true};
quit;

Examples

This example computes a single predictive margin named 'margin_x1_high'. It evaluates the model's prediction when the predictor 'x1' is fixed at a high value (0.9), while averaging over the observed values of all other predictors in the 'getStarted' table.

SAS® / CAS Code
Copied!
1PROC CAS;
2 bart.bartScoreMargin
3 TABLE='getStarted',
4 model='my_bart_model',
5 margins={{
6 name='margin_x1_high',
7 at={{var='x1', value=0.9}}
8 }};
9QUIT;
Result :
The action produces a 'Margins' table showing the posterior mean, standard deviation, and credible interval for the predicted probability when x1 is 0.9.

This example calculates and compares predictive margins for the two levels of the categorical variable 'x4'. It defines two margins, 'margin_x4_A' and 'margin_x4_B', and then computes the difference between them, named 'diff_A_vs_B'. This allows for quantifying the effect of changing 'x4' from 'A' to 'B' on the predicted outcome.

SAS® / CAS Code
Copied!
1PROC CAS;
2 bart.bartScoreMargin
3 TABLE='getStarted',
4 model='my_bart_model',
5 seed=1234,
6 alpha=0.1,
7 margins={{
8 name='margin_x4_A',
9 label='Margin for x4=A',
10 at={{var='x4', value='A'}}
11 },
12 {
13 name='margin_x4_B',
14 label='Margin for x4=B',
15 at={{var='x4', value='B'}}
16 }},
17 differences={{
18 name='diff_A_vs_B',
19 label='Difference (A vs B)',
20 refMargin='margin_x4_B',
21 evtMargin='margin_x4_A'
22 }},
23 marginInfo=true,
24 casOut={name='scored_margins', replace=true};
25QUIT;
Result :
The results will include three tables: 'MarginInfo' describing the defined margins, 'Margins' with the posterior summaries for each margin, and 'MarginDifferences' with the posterior summary for the difference 'diff_A_vs_B'. An output CAS table named 'scored_margins' will also be created containing the detailed posterior samples for each margin.

FAQ

What is the purpose of the bart.bartScoreMargin action in SAS Viya?
What is the 'model' parameter used for in the bartScoreMargin action?
How do I specify the input data for the bartScoreMargin action?
What is a predictive margin and how is it defined in this action?
How can I compute the difference between two predictive margins?
What does the 'alpha' parameter control?