Super Learner Action Set
Scoring and Computing Predictive Margins Using the slScore 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 27.1, which is stored in the mycas.modelFit data table.
If you have not loaded the Sashelp.Baseball data set, you can load it into your CAS session by naming your CAS engine libref in the first statement of the following DATA step:
data mycas.Baseball;
set Sashelp.Baseball;
run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following statements show how to score the original Sashelp.Baseball data set by using the slScore action. The action call involves the following parameters. You specify the saved model mycas.modelFit 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 contains the results.
proc cas;
superLearner.slScore
table='baseball',
restore='modelFit',
casOut={name='scoredData1', replace=true};
run;
quit;
In Output 27.2.1, the "Scoring Information" table displays the names of the saved model, the data table that is used for training, the data table of scored observations, and the target variable. The "Number of Observations" table displays the number of observations that are used for scoring.
Output 27.2.1: Scoring Information and Number of Observations
| Scoring Information | |
|---|---|
| Store | MODELFIT |
| Scoring Table | BASEBALL |
| Training Table | BASEBALL |
| Target Variable | logSalary |
| Number of Observations Read | 322 |
|---|---|
| Number of Observations Used for Scoring | 322 |
The following PROC PRINT statements print the first 10 observations of the predicted response values. The results are shown in Output 27.2.2.
proc print data=mycas.scoredData1(obs=10);
run;
Output 27.2.2: First 10 Observations in the Output Data Table
| Obs | P_logSalary |
|---|---|
| 1 | 5.71196 |
| 2 | 5.32676 |
| 3 | 6.50031 |
| 4 | 5.31661 |
| 5 | 5.80372 |
| 6 | 4.64077 |
| 7 | 6.48066 |
| 8 | 5.28444 |
| 9 | 4.83101 |
| 10 | 6.55891 |
You can also use the slScore action to compute predictive margins by using the following PROC CAS code. Two predictive margins are specified by the margins parameter. The first scenario sets the value of nHome to 1, and the second scenario intervenes on both nHome and nRuns and sets their values to 10 and 60, respectively. You set the marginPredOut parameter to True to include the predictions for each scenario in the output data table.
proc cas;
superLearner.slScore
table='baseball',
restore='modelFit',
margins={{at={{value='1', var='nHome'}}, name='Scenario1'},
{at={{value='10', var='nHome'}, {value='60', var='nRuns'}},
name='Scenario2'}},
marginPredOut=true,
casOut={name='scoredData2', replace=true};
run;
quit;
The "Predictive Margins" table in Output 27.2.3 displays the means of the predicted margin estimates for the scenarios that you specify in the margins parameter.
Output 27.2.3: Predictive Margins
| Predictive Margins | |
|---|---|
| Name | Estimate |
| Scenario1 | 5.85852 |
| Scenario2 | 5.93178 |
The following PROC PRINT statements print the first 10 observations of the scored data table that contain predicted responses without intervention and predicted responses for each intervening scenario. The results are shown in Output 27.2.4.
proc print data=mycas.scoredData2(obs=10);
run;
Output 27.2.4: First 10 Observations in the Output Data Table
| Obs | P_logSalary | Scenario1 | Scenario2 |
|---|---|---|---|
| 1 | 6.71732 | 6.60376 | 6.71632 |
| 2 | 6.69287 | 6.57910 | 6.69271 |
| 3 | 6.08750 | 6.08759 | 6.20067 |
| 4 | 5.17054 | 5.13303 | 5.17231 |
| 5 | 6.66656 | 6.66647 | 6.78072 |
| 6 | 6.30965 | 6.30965 | 6.42446 |
| 7 | 4.48284 | 4.48275 | 4.52972 |
| 8 | 5.96863 | 5.96666 | 6.20713 |
| 9 | 4.87299 | 4.83539 | 4.87246 |
| 10 | 5.65595 | 5.61674 | 5.65738 |
The P_logSalary column contains super learner predictions, and the other columns contain predictions for each intervening scenario.
Scoring and Computing Predictive Margins Using the slScore Action
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the Baseball data to the comma-separated-value (CSV) file Baseball.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="Baseball.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
This example uses the model that is fit in Example 27.1, which is stored in the modelFit data table.
The following commands show how to score the original Sashelp.Baseball data set by using the slScore action. The action call involves the following parameters. You specify the saved model modelFit 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 contains the results.
s:loadactionset{actionset='superLearner'}
s:superLearner_slScore{
table='baseball',
restore='modelFit',
casOut={name='scoredData1', replace=true}
}
The following commands print the first 10 observations of the predicted response values:
a=s:fetch{table={name='scoredData1'}, to=10}
print(a)
You can also use the slScore action to compute predictive margins, as in the following code. Two predictive margins are specified by the margins parameter. The first scenario sets the value of nHome to 1, and the second scenario intervenes on both nHome and nRuns and sets their values to 10 and 60, respectively. You set the marginPredOut parameter to True to include the predictions for each scenario in the output data table.
s:superLearner_slScore{
table='baseball',
restore='modelFit',
margins={{at={{value='1', var='nHome'}}, name='Scenario1'},
{at={{value='10', var='nHome'}, {value='60', var='nRuns'}},
name='Scenario2'}},
marginPredOut=true,
casOut={name='scoredData2', replace=true}
}
The following commands print the first 10 observations of the scored data table that contain predicted responses without intervention and predicted responses for each intervening scenario:
b=s:fetch{table={name='scoredData2'}, to=10}
print(b)
For details about the results of this analysis, see the CASL version of this example.
Scoring and Computing Predictive Margins Using the slScore Action
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the Baseball data to the comma-separated-value (CSV) file Baseball.csv and then use the following code to load the CSV file into CAS:
s.upload_file('Baseball.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
This example uses the model that is fit in Example 27.1, which is stored in the modelFit data table.
The following commands show how to score the original Sashelp.Baseball data set by using the slScore action. The action call involves the following parameters. You specify the saved model modelFit 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 contains the results.
s.loadactionset(actionset='superLearner')
s.superLearner.slScore(
table='baseball',
restore='modelFit',
casOut=dict(name='scoredData1', replace='true'))
The following commands print the first 10 observations of the predicted response values:
a=s.fetch(table={'name':'scoredData1'}, to='10')
print(a)
You can also use the slScore action to compute predictive margins, as in the following code. Two predictive margins are specified by the margins parameter. The first scenario sets the value of nHome to 1, and the second scenario intervenes on both nHome and nRuns and sets their values to 10 and 60, respectively. You set the marginPredOut parameter to True to include the predictions for each scenario in the output data table.
s.superLearner.slScore(
table='baseball',
restore='modelFit',
margins=[dict(at=[dict(value='1', var='nHome')],
name='Scenario1'),
dict(at=[dict(value='10', var='nHome'),
dict(value='60', var='nRuns')],
name='Scenario2')],
marginPredOut=true,
casOut=dict(name='scoredData2', replace='true'))
The following commands print the first 10 observations of the scored data table that contain predicted responses without intervention and predicted responses for each intervening scenario:
b=s.fetch(table={'name':'scoredData2'}, to='10')
print(b)
For details about the results of this analysis, see the CASL version of this example.
Scoring and Computing Predictive Margins Using the slScore Action
This section contains R code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the Baseball data to the comma-separated-value (CSV) file Baseball.csv and then use the following code to load the CSV file into CAS:
m <- cas.read.csv(s, "Baseball.csv", casOut=list(name="Baseball"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
This example uses the model that is fit in Example 27.1, which is stored in the modelFit data table.
The following commands show how to score the original Sashelp.Baseball data set by using the slScore action. The action call involves the following parameters. You specify the saved model modelFit 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 contains the results.
cas.builtins.loadactionset(s, actionset='superLearner')
cas.superLearner.slScore(s,
table='baseball',
restore='modelFit',
casOut=list(name='scoredData1', replace='true'))
The following command prints the first 10 observations of the predicted response values:
cas.table.fetch(s, table='scoredData1', to='10')
You can also use the slScore action to compute predictive margins, as in the following code. Two predictive margins are specified by the margins parameter. The first scenario sets the value of nHome to 1, and the second scenario intervenes on both nHome and nRuns and sets their values to 10 and 60, respectively. You set the marginPredOut parameter to True to include the predictions for each scenario in the output data table.
cas.superLearner.slScore(s,
table='baseball',
restore='modelFit',
margins=list(list(at=list(list(value='1', var='nHome')),
name='Scenario1'),
list(at=list(list(value='10', var='nHome'),
list(value='60', var='nRuns')),
name='Scenario2')],
marginPredOut=true,
casOut=list(name='scoredData2', replace='true'))
The following command prints the first 10 observations of the scored data table that contain predicted responses without intervention and predicted responses for each intervening scenario:
cas.table.fetch(s, table='scoredData2', to='10')
For details about the results of this analysis, see the CASL version of this example.