BART Procedure

Example 4.1 Storing and Scoring

(View the complete code for this example.)

When you use PROC BART to fit a model, you can use the STORE statement to save the model. For example, you can score new observations or define interventions on predictors and score marginal means for the interventions. This example uses the RESTORE= option in PROC BART to do subsequent scoring. Note that because scoring a new observation for a Bayesian additive regression trees (BART) model requires routing the observation through each tree for all the saved samples of the sum-of-trees ensemble, scoring large data sets can be time-consuming.

The following statements generate the data table inputData, which consists of 10,000 observations on a continuous response variable (y) and 40 continuous variables (x1x40), in your CAS session:


data mylib.inputData / single =yes;
   drop  j w1-w40;

  array x{40};
  array w{40};
  call streaminit(6524);
  pi=constant("pi");

   do i=1 to 10000;
      u = rand("Uniform");
      do j=1 to dim(x);
         w{j} = rand("Uniform");
         x{j} = (w{j} + u)/2;
      end;

      f1 = sin(pi * x1 * x2 );
      f2 = (x3-0.5)**2;
      f3 = x4;
      f4 = x5;
      fb = 10*f1 +20*f2+10*f3+5*f4;

      y = fb +  rand("Normal");
      output;
   end;

run;

For the simulation, only the variables x1x5 affect the outcome y, and the variables x1x40 are all positively correlated.

In the following program, because the input data are not large, the TRAININMEM and MAPINMEM options store the data and elements of the model in memory when the model is trained:

proc bart data=mylib.inputData seed=9181 trainInMem mapInMem;
   model y = x1-x40;
   store mylib.modelFit;
run;

The mylib.modelFit store contains a representation of the model that you can use to score new observations. The fit statistics for the fitted model are displayed in the "Fit Statistics" table in Output 4.1.1.

Output 4.1.1: Fit Statistics

The BART Procedure

Fit Statistics
Average Square Error0.97290


The following statements create a new data table of observations to score using the same data-generating process that generated the training data:


data mylib.toScoreData / single =yes;
   drop  j w1-w40;

  array x{40};
  array w{40};
  call streaminit(1972);
  pi=constant("pi");

   do i=1 to 1000;
      u = rand("Uniform");
      do j=1 to dim(x);
         w{j} = rand("Uniform");
         x{j} = (w{j} + u)/2;
      end;

      f1 = sin(pi * x1 * x2 );
      f2 = (x3-0.5)**2;
      f3 = x4;
      f4 = x5;
      fb = 10*f1 +20*f2+10*f3+5*f4;

      y = fb +  rand("Normal");
      output;
   end;

run;

The following statements show how to use PROC BART to score the new data by using the previously fitted model. The saved model mylib.modelFit is specified in the RESTORE= option. You use the DATA= option to specify the data table of observations to score, and you use the OUTPUT statement to designate the output data table that contains the results. The PRED= keyword names the variable in the output data table that contains the predicted response value. Note that because the toScoreData data table also contains the response variable y, residuals for the predicted values can also be computed. The RESID= keyword names the variable in the output data table that contains the residuals. The RESTORE= option and OUTPUT statement are both required when you use PROC BART to score data by using a previously fitted model.

proc bart data=mylib.toScoreData restore=mylib.modelFit;
   output out = mylib.scoredData pred = predResp resid = residual;
run;

The following statements compute the average square error (ASE) for the scored observations:

data fitCheck;
   set mylib.scoredData;
   SquareError = residual * residual;
run;

proc means data=fitCheck mean;
   var SquareError;
run;

The ASE for the scored observations in Output 4.1.2 is similar to the ASE for the training data in Output 4.1.1, indicating that the fitted model generalizes well.

Output 4.1.2: ASE for the Scored Data

The MEANS Procedure

Analysis Variable
: SquareError
Mean
1.0628698


You can also use the saved model mylib.modelFit to compute predictive margins. You can use the MARGIN statement to define predictive margins that intervene on one or more of the input variables. Observations in an input data table are scored according to the specified interventions, and means of the predicted values are reported along with equal-tail credible intervals. When you want to compare the difference between predicted margins, you can use the MARGINDIFF statement to specify a contrast of predictive margins that are defined in MARGIN statements.

The following statements show how you can compute predictive margins by using PROC BART and the previously fitted model that is saved in the analytic store mylib.modelFit. The saved model mylib.modelFit is specified in the RESTORE= option, and the data table of observations to score is specified in the DATA= option. Five predictive margins are specified in the MARGIN statement. 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 MARGINDIFF statements to request differences between these predictive margins.

proc bart restore = mylib.modelFit data=mylib.inputData;
   margin "Scenario1" x2 = 0.25;
   margin "Scenario2" x2 = 0.25 x3 = 0.5;
   margin "x1Ref"     x1 = 0.25;
   margin "x1Evt1"    x1 = 0.5;
   margin "x1Evt2"    x1 = 0.75;
   margindiff event = "x1Evt1" ref = "x1Ref" / label= "x1:0.5 - 0.25";
   margindiff event = "x1Evt2" ref = "x1Ref" / label= "x1:0.75 - 0.25";
run;

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

Output 4.1.3: Score Information

The BART Procedure

Scoring Information
StoreMODELFIT
Scoring TableINPUTDATA
Training TableINPUTDATA
Target Variabley
Missing RuleSeparate
MCMC Sample Size1000
Number of Observations10000


The "Predictive Margins" table in Output 4.1.4 displays the means for the predicted margin estimates and the equal-tail credible intervals for the interventions that are specified by a MARGIN statement.

Output 4.1.4: Predictive Margins

Predictive Margins
DescriptionEstimate95% Equal-Tail Interval
Scenario112.1418511.9798412.31817
Scenario211.3642311.2043111.55277
x1Ref12.2255212.0748812.42200
x1Evt115.1366315.0115515.25285
x1Evt216.5402816.3954416.66414


The "Predictive Margin Differences" table in Output 4.1.5 displays the mean difference between predictive margins that is specified in the MARGINDIFF statement along with equal-tail credible intervals.

Output 4.1.5: Predictive Margin Differences

Predictive Margin Differences
DescriptionEstimate95% Equal-Tail Interval
x1:0.5 - 0.252.91112.69983.1283
x1:0.75 - 0.254.31484.04244.5075


Last updated: June 22, 2026