Super Learner Action Set

Fitting a Super Learner Model Using the slTrain 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.

The Sashelp.Baseball data table contains salary and performance information for Major League Baseball players (excluding pitchers) who played at least one game in both the 1986 and 1987 seasons (Time Inc. 1987).

You can load the Sashelp.Baseball data set 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 use the slTrain action in the superLearner action set to fit a super learner model to these data. The table parameter names the input data table to be analyzed. The target parameter names the target (response) variable, and the inputs parameter lists the predictor variables. The nominals parameter lists the variables that are treated as categorical variables for the analysis. The library parameter specifies the list of base learner models. In this example, you specify three base learner models. In each base learner model specification, the name parameter names the base learner model. The modelType parameter assigns a model type to the base learner model. The trainOptions parameter is optional and can be used to customize a base learner model. If you omit this parameter, default values of the training action that corresponds to the model type are used to train the base learner model. In the trainOptions parameter, you specify training parameters that are specific to the training action that corresponds to the model type. In this example, you specify the trainOptions parameter for the base learner 'bart'. You set the nTree and nMC parameters to values that are different from the default values of these parameters in the bartGauss action. The k parameter specifies the number of folds to create for cross-validation. The store parameter stores the fitted model in the modelFit data table.

proc cas;
superLearner.slTrain /
    inputs={"nAtBat", "nHits", "nHome", "nRuns", "nRBI", "nBB", "yrMajor",
            "crAtBat", "crHits", "crHome", "league", "division", "crRuns",
            "crRbi", "crBB", "nOuts", "nAssts", "nError"},
    k="6",
    library={{modelType="GLM", name="glm"},
             {modelType="DTREE", name="dtree"},
             {modelType="BARTGAUSS",
              name="bart",
              trainOptions={inputs={"nAtBat", "nHits", "nHome", "nRuns", "nRBI",
      "nBB", "yrMajor", "crAtBat", "crHits", "crHome", "league", "division",
      "crRuns", "crRbi", "crBB", "nOuts", "nAssts", "nError"},
                            nMC="100",
                            nTree="10",
                            nominals={"league", "division"},
                            table="baseball",
                            target="logSalary"}}},
    nominals={"league", "division"},
    seed="846203",
    store={name="modelFit", replace="true"},
    table="baseball",
    target="logSalary";
run;

The results from this analysis are presented in Output 27.1.1 and Output 27.1.2. The "Model Information" table in Output 27.1.1 summarizes important information about the input data and primary analysis options. Information includes the input data source, response (target) variable and its type, number of folds used in cross-validation, number of base learners, loss function, meta-learning method, and random number seed.

Output 27.1.1: Model Information

Results from superLearner.slTrain

Model Information
Data SourceBASEBALL
Target VariablelogSalary
Target Variable TypeINTERVAL
Number of Folds6
Number of Base Learners3
Loss FunctionQuadratic Loss
Meta-learning MethodConvex-constrained Least Squares
Random Number Seed846203


Output 27.1.2 shows the number of observations in the input data table. The slTrain action uses only complete cases in the input data for training, so an observation is excluded from training if any analysis variable contains a missing value.

Output 27.1.2: Number of Observations

Number of Observations Read322
Number of Observations Used263


Output 27.1.3 displays information about the super learner model coefficients, which describe the contribution of each base learner model in the super learner model ensemble. The cross-validated risk measures each base learner’s predictive performance in cross-validation. In this example, the 'bart' base learner model has the smallest cross-validated risk and therefore shows the best out-of-sample performance. It receives the largest super learner model coefficient.

Output 27.1.3: Super Learner Model Coefficients

Super Learner Model Coefficients
NameModel TypeCoefficientCross-Validated
Risk
glmOrdinary Linear Least Squares0.008500.35859
dtreeDecision Tree0.050100.27918
bartBayesian Additive Regression Trees0.941410.17768


Fitting a Super Learner Model Using the slTrain Action

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

The Sashelp.Baseball data table contains salary and performance information for Major League Baseball players (excluding pitchers) who played at least one game in both the 1986 and 1987 seasons (Time Inc. 1987).

To run this example code, you must make the Sashelp.Baseball data set accessible to the CAS server. One way to do this is to convert the Sashelp.Baseball data set to a comma-separated-value (CSV) file and then to load the CSV file into CAS.

The following statements use the slTrain action in the superLearner action set to fit a super learner model to these data. The table parameter names the input data table to be analyzed. The target parameter names the target (response) variable, and the inputs parameter lists the predictor variables. The nominals parameter lists the variables that are treated as categorical variables for the analysis. The library parameter specifies the list of base learner models. In this example, you specify three base learner models. In each base learner model specification, the name parameter names the base learner model. The modelType parameter assigns a model type to the base learner model. The trainOptions parameter is optional and can be used to customize a base learner model. If you omit this parameter, default values of the training action that corresponds to the model type are used to train the base learner model. In the trainOptions parameter, you specify training parameters that are specific to the training action that corresponds to the model type. In this example, you specify the trainOptions parameter for the base learner 'bart'. You set the nTree and nMC parameters to values that are different from the default values of these parameters in the bartGauss action. The k parameter specifies the number of folds to create for cross-validation. The store parameter stores the fitted model in the modelFit data table.

s:loadactionset{actionset="superLearner"}
s:superLearner_slTrain{
   table='baseball',
   target='logSalary',
   nominals={'league','division'},
   inputs={'nAtBat', 'nHits', 'nHome', 'nRuns', 'nRBI', 'nBB',
   'yrMajor', 'crAtBat', 'crHits', 'crHome', 'league', 'division',
   'crRuns', 'crRbi', 'crBB', 'nOuts', 'nAssts', 'nError'},
   library={
      {name='glm', modelType='GLM'},
      {name='dtree', modelType='DTREE'},
      {name='bart', modelType='BARTGAUSS',
       trainOptions={table='baseball',
                     target='logSalary',
                     nominals={'league','division'},
                     inputs={'nAtBat', 'nHits', 'nHome', 'nRuns', 'nRBI', 'nBB',
                             'yrMajor', 'crAtBat', 'crHits', 'crHome', 'league',
                             'division', 'crRuns', 'crRbi', 'crBB', 'nOuts',
                             'nAssts', 'nError'},
                     nTree=10,
                     nMC=100
                     }
      }
   },
   k=6,
   seed=846203,
   store={name='modelFit', replace='true'}
   }

The results from this analysis are presented in Output 27.1.4 and Output 27.1.5. The "Model Information" table in Output 27.1.4 summarizes important information about the input data and primary analysis options. Information includes the input data source, response (target) variable and its type, number of folds used in cross-validation, number of base learners, loss function, meta-learning method, and random number seed.

Output 27.1.4: Model Information

RowIdDescriptioncValueValue
TABLEData SourceBASEBALL.
TARGETTarget VariablelogSalary.
TARGETTYPETarget Variable TypeINTERVAL.
NFOLDSNumber of Folds66.000000
NLEARNERSNumber of Base Learners33.000000
LOSSLoss FunctionQuadratic Loss.
METHODMeta-learning MethodConvex-constrained Least Squares.
SEEDRandom Number Seed846203846203


Output 27.1.5 shows the number of observations in the input data table. The slTrain action uses only complete cases in the input data for training, so an observation is excluded from training if any analysis variable contains a missing value.

Output 27.1.5: Number of Observations

RowIdDescriptionValue
NREADNumber of Observations Read322
NUSEDNumber of Observations Used263


Output 27.1.6 displays information about the super learner model coefficients, which describe the contribution of each base learner model in the super learner model ensemble. The cross-validated risk measures each base learner’s predictive performance in cross-validation. In this example, the 'bart' base learner model has the smallest cross-validated risk and therefore shows the best out-of-sample performance. It receives the largest super learner model coefficient.

Output 27.1.6: Super Learner Model Coefficients

BaseLearnerNameBaseLearnerModelTypeCoefficientCrossValidatedRisk
glmOrdinary Linear Least Squares0.008500.35859
dtreeDecision Tree0.050100.27918
bartBayesian Additive Regression Trees0.941410.17768


Fitting a Super Learner Model Using the slTrain Action

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

The Sashelp.Baseball data table contains salary and performance information for Major League Baseball players (excluding pitchers) who played at least one game in both the 1986 and 1987 seasons (Time Inc. 1987).

To run this example code, you must make the Sashelp.Baseball data set accessible to the CAS server. One way to do this is to convert the Sashelp.Baseball data set to a comma-separated-value (CSV) file and then to load the CSV file into CAS.

The following statements use the slTrain action in the superLearner action set to fit a super learner model to these data. The table parameter names the input data table to be analyzed. The target parameter names the target (response) variable, and the inputs parameter lists the predictor variables. The nominals parameter lists the variables that are treated as categorical variables for the analysis. The library parameter specifies the list of base learner models. In this example, you specify three base learner models. In each base learner model specification, the name parameter names the base learner model. The modelType parameter assigns a model type to the base learner model. The trainOptions parameter is optional and can be used to customize a base learner model. If you omit this parameter, default values of the training action that corresponds to the model type are used to train the base learner model. In the trainOptions parameter, you specify training parameters that are specific to the training action that corresponds to the model type. In this example, you specify the trainOptions parameter for the base learner 'bart'. You set the nTree and nMC parameters to values that are different from the default values of these parameters in the bartGauss action. The k parameter specifies the number of folds to create for cross-validation. The store parameter stores the fitted model in the modelFit data table.

s.loadactionset(actionset='superLearner')
s.superlearner.sltrain(
    inputs=['nAtBat', 'nHits', 'nHome', 'nRuns', 'nRBI', 'nBB', 'yrMajor',
            'crAtBat', 'crHits', 'crHome', 'league', 'division', 'crRuns',
            'crRbi', 'crBB', 'nOuts', 'nAssts', 'nError'],
    k='6',
    library=[dict(modelType='GLM', name='glm'),
             dict(modelType='DTREE', name='dtree'),
             dict(modelType='BARTGAUSS',
                  name='bart',
                  trainOptions=dict(inputs=['nAtBat', 'nHits', 'nHome', 'nRuns',
      'nRBI', 'nBB', 'yrMajor', 'crAtBat', 'crHits', 'crHome', 'league',
      'division', 'crRuns', 'crRbi', 'crBB', 'nOuts', 'nAssts', 'nError'],
                                    nMC='100',
                                    nTree='10',
                                    nominals=['league', 'division'],
                                    table='baseball',
                                    target='logSalary'))],
    nominals=['league', 'division'],
    seed='846203',
    store=dict(name='modelFit', replace='true'),
    table='baseball',
    target='logSalary')

The results from this analysis are presented in Output 27.1.7 and Output 27.1.8. The "Model Information" table in Output 27.1.7 summarizes important information about the input data and primary analysis options. Information includes the input data source, response (target) variable and its type, number of folds used in cross-validation, number of base learners, loss function, meta-learning method, and random number seed.

Output 27.1.7: Model Information

RowIdDescriptioncValueValue
TABLEData SourceBASEBALL.
TARGETTarget VariablelogSalary.
TARGETTYPETarget Variable TypeINTERVAL.
NFOLDSNumber of Folds66.000000
NLEARNERSNumber of Base Learners33.000000
LOSSLoss FunctionQuadratic Loss.
METHODMeta-learning MethodConvex-constrained Least Squares.
SEEDRandom Number Seed846203846203


Output 27.1.8 shows the number of observations in the input data table. The slTrain action uses only complete cases in the input data for training, so an observation is excluded from training if any analysis variable contains a missing value.

Output 27.1.8: Number of Observations

RowIdDescriptionValue
NREADNumber of Observations Read322
NUSEDNumber of Observations Used263


Output 27.1.9 displays information about the super learner model coefficients, which describe the contribution of each base learner model in the super learner model ensemble. The cross-validated risk measures each base learner’s predictive performance in cross-validation. In this example, the 'bart' base learner model has the smallest cross-validated risk and therefore shows the best out-of-sample performance. It receives the largest super learner model coefficient.

Output 27.1.9: Super Learner Model Coefficients

BaseLearnerNameBaseLearnerModelTypeCoefficientCrossValidatedRisk
glmOrdinary Linear Least Squares0.008500.35859
dtreeDecision Tree0.050100.27918
bartBayesian Additive Regression Trees0.941410.17768


Fitting a Super Learner Model Using the slTrain Action

This section contains R code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

The Sashelp.Baseball data table contains salary and performance information for Major League Baseball players (excluding pitchers) who played at least one game in both the 1986 and 1987 seasons (Time Inc. 1987).

To run this example code, you must make the Sashelp.Baseball data set accessible to the CAS server. One way to do this is to convert the Sashelp.Baseball data set to a comma-separated-value (CSV) file and then to load the CSV file into CAS.

The following statements use the slTrain action in the superLearner action set to fit a super learner model to these data. The table parameter names the input data table to be analyzed. The target parameter names the target (response) variable, and the inputs parameter lists the predictor variables. The nominals parameter lists the variables that are treated as categorical variables for the analysis. The library parameter specifies the list of base learner models. In this example, you specify three base learner models. In each base learner model specification, the name parameter names the base learner model. The modelType parameter assigns a model type to the base learner model. The trainOptions parameter is optional and can be used to customize a base learner model. If you omit this parameter, default values of the training action that corresponds to the model type are used to train the base learner model. In the trainOptions parameter, you specify training parameters that are specific to the training action that corresponds to the model type. In this example, you specify the trainOptions parameter for the base learner 'bart'. You set the nTree and nMC parameters to values that are different from the default values of these parameters in the bartGauss action. The k parameter specifies the number of folds to create for cross-validation. The store parameter stores the fitted model in the modelFit data table.

m <- cas.builtins.loadActionSet(s, actionset='superLearner')
cas.superLearner.slTrain(s,
    inputs=c('nAtBat', 'nHits', 'nHome', 'nRuns', 'nRBI', 'nBB', 'yrMajor',
             'crAtBat', 'crHits', 'crHome', 'league', 'division', 'crRuns',
             'crRbi', 'crBB', 'nOuts', 'nAssts', 'nError'),
    k='6',
    library=list(list(modelType='GLM', name='glm'),
                 list(modelType='DTREE', name='dtree'),
                 list(modelType='BARTGAUSS',
                      name='bart',
                      trainOptions=list(inputs=c('nAtBat', 'nHits', 'nHome',
      'nRuns', 'nRBI', 'nBB', 'yrMajor', 'crAtBat', 'crHits', 'crHome',
      'league', 'division', 'crRuns', 'crRbi', 'crBB', 'nOuts', 'nAssts',
      'nError'),
                                        nMC='100',
                                        nTree='10',
                                        nominals=c('league', 'division'),
                                        table='baseball',
                                        target='logSalary'))),
    nominals=c('league', 'division'),
    seed='846203',
    store=list(name='modelFit', replace='true'),
    table='baseball',
    target='logSalary')

The results from this analysis are presented in Output 27.1.10 and Output 27.1.11. The "Model Information" table in Output 27.1.10 summarizes important information about the input data and primary analysis options. Information includes the input data source, response (target) variable and its type, number of folds used in cross-validation, number of base learners, loss function, meta-learning method, and random number seed.

Output 27.1.10: Model Information

RowIdDescriptioncValueValue
TABLEData SourceBASEBALL.
TARGETTarget VariablelogSalary.
TARGETTYPETarget Variable TypeINTERVAL.
NFOLDSNumber of Folds66.000000
NLEARNERSNumber of Base Learners33.000000
LOSSLoss FunctionQuadratic Loss.
METHODMeta-learning MethodConvex-constrained Least Squares.
SEEDRandom Number Seed846203846203


Output 27.1.11 shows the number of observations in the input data table. The slTrain action uses only complete cases in the input data for training, so an observation is excluded from training if any analysis variable contains a missing value.

Output 27.1.11: Number of Observations

RowIdDescriptionValue
NREADNumber of Observations Read322
NUSEDNumber of Observations Used263


Output 27.1.12 displays information about the super learner model coefficients, which describe the contribution of each base learner model in the super learner model ensemble. The cross-validated risk measures each base learner’s predictive performance in cross-validation. In this example, the 'bart' base learner model has the smallest cross-validated risk and therefore shows the best out-of-sample performance. It receives the largest super learner model coefficient.

Output 27.1.12: Super Learner Model Coefficients

BaseLearnerNameBaseLearnerModelTypeCoefficientCrossValidatedRisk
glmOrdinary Linear Least Squares0.008500.35859
dtreeDecision Tree0.050100.27918
bartBayesian Additive Regression Trees0.941410.17768


Last updated: September 17, 2026