Bayesian Additive Regression Trees Action Set
Scoring a Model Using the bartScore 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.
The following statements create a new data table of observations to score by using the same data-generating process that generated the training data in Example 4.1. By default, the CAS server executes the DATA step in parallel in every thread on every node in your session. In this example, the single parameter restricts the execution to a single thread to create a data table that contains 1,000 observations.
proc cas;
dataStep.runCode /
code="
data testData;
drop i j pi w1-w40 u f1 f2 f3 f4 nTotalObs;
array x{40};
array w{40};
call streaminit(1234);
nTotalObs=1000;
pi=constant('pi');
do i=1 to nTotalObs;
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;
",
single="yes";
run;
The following statements show how to score the new data by using the bartScore action. The action call involves the following parameters. The saved model mycas.modelFit is specified in the restore parameter. You use the table parameter to specify the data table of observations to score, and you use the casOut parameter to designate the output data table that has the results. The pred parameter 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 parameter names the variable in the output data table that contains the residuals. The table, restore, and casOut parameters are all required when you use the bartScore action.
proc cas;
bart.bartScore /
casOut={name="scoredData", replace="true"},
pred="predResp",
resid="residual",
restore="modelFit",
table="testData";
run;
The following DATA step computes the square error for the scored observations:
proc cas;
dataStep.runCode /
code="
data scoredData;
set scoredData;
sqErr = residual * residual;
run;
";
run;
The following statements use the summary action in the simple action set to compute the average square error (ASE) for the scored data:
proc cas;
simple.summary /
inputs="sqErr",
subSet={"mean"},
table="scoredData";
run;
The ASE for the scored observations in Output 4.3.1 is similar to the ASE for the training data in Output 4.1.6, indicating that the fitted model generalizes well.
Output 4.3.1: ASE for the Scored Data
| Descriptive Statistics for SCOREDDATA | |
|---|---|
| Column | Mean |
| sqErr | 1.0527 |
Scoring a Model Using the bartScore 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.
The following statements create a new data table of observations to score by using the same data-generating process that generated the training data in Example 4.1. By default, the CAS server executes the DATA step in parallel in every thread on every node in your session. In this example, the single parameter restricts the execution to a single thread to create a data table that contains 1,000 observations.
s:dataStep_runCode{single='yes', code=[[
data testData;
drop i j pi w1-w40 u f1 f2 f3 f4 nTotalObs;
array x{40};
array w{40};
call streaminit(1234);
nTotalObs=1000;
pi=constant('pi');
do i=1 to nTotalObs;
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 score the new data by using the bartScore action. The action call involves the following parameters. The saved model mycas.modelFit is specified in the restore parameter. You use the table parameter to specify the data table of observations to score, and you use the casOut parameter to designate the output data table that has the results. The pred parameter 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 parameter names the variable in the output data table that contains the residuals. The table, restore, and casOut parameters are all required when you use the bartScore action.
s:loadactionset{actionset="bart"}
s:bart_bartScore{
table='testData',
restore='modelFit',
casOut={name='scoredData', replace=true},
pred ='predResp',
resid = 'residual'
}
The following DATA step computes the square error for the scored observations:
s:dataStep_runCode{ code=[[
data scoredData;
set scoredData;
sqErr = residual * residual;
run;
]] }
The following statements use the summary action in the simple action set to compute the average square error (ASE) for the scored data:
s:simple_summary{
table='scoredData',
inputs='sqErr',
subSet={'mean'}
}
The ASE for the scored observations in Output 4.3.2 is similar to the ASE for the training data in Output 4.1.13, indicating that the fitted model generalizes well.
Output 4.3.2: ASE for the Scored Data
| Column | Mean |
|---|---|
| sqErr | 1.0527 |
Scoring a Model Using the bartScore 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.
The following statements create a new data table of observations to score by using the same data-generating process that generated the training data in Example 4.1. By default, the CAS server executes the DATA step in parallel in every thread on every node in your session. In this example, the single parameter restricts the execution to a single thread to create a data table that contains 1,000 observations.
s.datastep.runcode(
code='''
data testData;
drop i j pi w1-w40 u f1 f2 f3 f4 nTotalObs;
array x{40};
array w{40};
call streaminit(1234);
nTotalObs=1000;
pi=constant('pi');
do i=1 to nTotalObs;
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;
''',
single='yes')
The following statements show how to score the new data by using the bartScore action. The action call involves the following parameters. The saved model mycas.modelFit is specified in the restore parameter. You use the table parameter to specify the data table of observations to score, and you use the casOut parameter to designate the output data table that has the results. The pred parameter 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 parameter names the variable in the output data table that contains the residuals. The table, restore, and casOut parameters are all required when you use the bartScore action.
s.loadactionset(actionset='bart')
s.bart.bartscore(
casOut=dict(name='scoredData', replace='true'),
pred='predResp',
resid='residual',
restore='modelFit',
table='testData')
The following DATA step computes the square error for the scored observations:
s.datastep.runcode(
code='''
data scoredData;
set scoredData;
sqErr = residual * residual;
run;
''')
The following statements use the summary action in the simple action set to compute the average square error (ASE) for the scored data:
s.simple.summary(
inputs='sqErr',
subSet=['mean'],
table='scoredData')
The ASE for the scored observations in Output 4.3.3 is similar to the ASE for the training data in Output 4.1.20, indicating that the fitted model generalizes well.
Output 4.3.3: ASE for the Scored Data
| Column | Mean |
|---|---|
| sqErr | 1.0527 |
Scoring a Model Using the bartScore 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.
The following statements create a new data table of observations to score by using the same data-generating process that generated the training data in Example 4.1. By default, the CAS server executes the DATA step in parallel in every thread on every node in your session. In this example, the single parameter restricts the execution to a single thread to create a data table that contains 1,000 observations.
cas.dataStep.runCode(s,
code='
data testData;
drop i j pi w1-w40 u f1 f2 f3 f4 nTotalObs;
array x{40};
array w{40};
call streaminit(1234);
nTotalObs=1000;
pi=constant(\'pi\');
do i=1 to nTotalObs;
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;
',
single='yes')
The following statements show how to score the new data by using the bartScore action. The action call involves the following parameters. The saved model mycas.modelFit is specified in the restore parameter. You use the table parameter to specify the data table of observations to score, and you use the casOut parameter to designate the output data table that has the results. The pred parameter 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 parameter names the variable in the output data table that contains the residuals. The table, restore, and casOut parameters are all required when you use the bartScore action.
m <- cas.builtins.loadActionSet(s, actionset='bart')
cas.bart.bartScore(s,
casOut=list(name='scoredData', replace='true'),
pred='predResp',
resid='residual',
restore='modelFit',
table='testData')
The following DATA step computes the square error for the scored observations:
cas.dataStep.runCode(s,
code='
data scoredData;
set scoredData;
sqErr = residual * residual;
run;
')
The following statements use the summary action in the simple action set to compute the average square error (ASE) for the scored data:
cas.simple.summary(s,
inputs='sqErr',
subSet=c('mean'),
table='scoredData')
The ASE for the scored observations in Output 4.3.4 is similar to the ASE for the training data in Output 4.1.27, indicating that the fitted model generalizes well.
Output 4.3.4: ASE for the Scored Data
| Column | Mean |
|---|---|
| sqErr | 1.0527 |