Regression Action Set

The genmod Action

Poisson Regression

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 following DATA step creates the data table mycas.getStarted in your CAS session. This data table consists of 100 observations on a count response variable (Y); and five categorical variables (C1–C5), each of which has four numerical levels. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

data mycas.getStarted;
  input C1-C5 Y @@;
  datalines;
0 3 1 1 3 2   2 3 0 3 1 2   1 3 2 2 2 1   1 2 0 0 3 2   0 2 1 0 1 1
0 2 1 0 2 1   1 2 1 0 1 0   0 2 1 1 2 1   1 2 0 0 1 0   0 1 1 3 3 2
2 2 2 2 1 1   0 3 2 0 3 2   2 2 2 0 0 1   0 2 0 2 0 1   1 3 0 0 0 0
1 2 1 2 3 2   0 1 2 3 1 1   1 1 0 0 1 0   1 3 2 2 2 0   2 1 3 1 1 2
1 3 0 1 2 1   2 0 2 3 0 5   2 2 2 1 0 1   3 1 3 1 0 1   3 1 3 1 2 3
3 1 1 2 3 7   3 1 1 1 0 1   3 1 0 0 0 2   3 1 2 3 0 1   2 2 0 1 2 1
3 3 2 2 3 1   2 0 0 2 3 2   1 0 0 2 3 4   0 0 2 3 0 6   0 3 1 0 0 0
3 0 1 0 1 1   2 0 0 0 3 2   1 0 1 0 3 2   1 1 3 1 1 1   2 1 3 0 3 7
1 3 2 1 1 0   2 1 0 0 1 0   0 0 1 1 2 3   0 1 0 1 0 2   1 2 2 2 3 1
0 3 2 3 1 1   3 3 0 3 3 1   2 0 0 2 1 2   2 2 3 0 3 3   0 2 0 3 0 1
3 0 1 2 2 2   2 1 2 3 1 0   3 2 0 3 1 0   3 0 3 0 0 2   1 3 2 2 1 3
2 3 2 0 3 1   1 0 1 2 1 1   2 1 2 2 2 5   3 0 1 1 2 5   0 1 1 3 2 1
2 2 1 3 1 4   1 1 0 0 1 1   3 3 1 2 1 2   2 2 0 2 3 3   0 1 0 0 2 2
0 1 2 0 1 2   3 2 2 2 0 1   2 2 3 0 0 1   1 2 2 3 2 1   3 3 2 2 1 2
0 0 3 1 3 6   3 2 3 1 2 3   0 3 0 0 0 1   3 3 3 0 3 2   2 3 2 0 2 0
1 3 0 2 0 1   1 3 1 0 2 0   3 2 3 3 0 1   3 1 0 2 0 4   0 1 2 2 1 1
0 2 3 2 2 1   0 3 2 0 1 3   2 1 1 2 0 1   1 0 3 0 0 0   0 0 3 2 0 1
0 2 3 1 0 0   2 2 2 1 3 2   3 3 3 0 0 0   2 2 1 3 3 2   0 2 3 2 3 2
1 3 1 2 1 0   3 0 1 1 1 4   2 1 1 1 3 6   0 2 0 3 2 1   2 0 1 1 2 2
2 2 2 2 3 2   1 0 2 2 1 3   1 3 3 1 1 1   3 1 2 1 3 5   0 3 2 1 2 0
;

The following PROC CAS statements use the genmod action in the regression action set to fit a log-linked Poisson model to these data. The table parameter names the input data table. The class parameter specifies that the variables C1, C2, C3, C4, and C5 are classification (nominal) variables; this parameter corresponds to the CLASS statement in the GENSELECT procedure. The model parameter specifies the model: the depvar subparameter specifies the variable y as the response (target) variable; the effects subparameter lists the five classification variables, C1–C5; the dist subparameter specifies that the response variable y has a Poisson distribution; and the link subparameter specifies a log link between the mean and the linear predictor. The itHist subparameter in the optimization parameter list produces a table that summarizes the steps of the optimization. Finally, the outputTables parameter outputs the fitted parameter estimates to the mycas.pe data table.

proc cas;
   regression.genmod
      table={name='getStarted'},
      class={'C1','C2','C3','C4','C5'},
      model={depvar='y',
             effects={'C1', 'C2', 'C3', 'C4', 'C5'},
             dist='poisson',
             link='log'},
      optimization={itHist='summary'},
      outputTables={names={parameterestimates='pe'}};
run;

The output from this analysis is displayed by default, and is presented in Output 20.1.1 through Output 20.1.7.

Output 20.1.1 displays the "Model Information" table. The variable Y is an integer-valued variable that is modeled by using a Poisson probability distribution, and the mean of Y is modeled by using a log link function. The genmod action uses a Newton-Raphson algorithm to fit the model. The classification variables C1–C5 are parameterized by using GLM parameterization, which is the default.

Output 20.1.1: Model Information

Results from regression.genmod

Model Information
Data SourceGETSTARTED
Response VariableY
DistributionPoisson
Link FunctionLog
Optimization TechniqueNewton-Raphson with Ridging


Each of the classification variables C1–C5 has four unique formatted levels, which are displayed in the "Class Level Information" table in Output 20.1.2.

Output 20.1.2: Class Level Information

Class Level Information
ClassLevelsValues
C140 1 2 3
C240 1 2 3
C340 1 2 3
C440 1 2 3
C540 1 2 3


Output 20.1.3 displays the "Number of Observations" table. All 100 observations in the data set are used in the analysis.

Output 20.1.3: Number of Observations

Number of Observations Read100
Number of Observations Used100


Output 20.1.4 displays the "Dimensions" table for this model. This table summarizes some important sizes of various model components. For example, it shows that the design matrix bold upper X has 21 columns: 1 column for the intercept and 20 columns for the effects that are associated with the classification variables C1–C5. However, the rank of the crossproducts matrix is only 16. Because the classification variables C1–C5 use GLM parameterization and because the model contains an intercept, there is one singularity in the crossproducts matrix of the model for each classification variable. Consequently, only 16 parameters enter the optimization.

Output 20.1.4: Dimensions in Poisson Regression

Dimensions
Columns in Design21
Number of Effects6
Max Effect Columns4
Rank of Design16
Parameters in Optimization16


Output 20.1.5 displays the final convergence status of the Newton-Raphson algorithm. The FCONV convergence criterion is satisfied.

Output 20.1.5: Convergence Status

Convergence criterion (FCONV=1E-7) satisfied.


The "Fit Statistics" table is shown in Output 20.1.6. The –2 log likelihood at the converged estimates is 290.16169. You can use this value to compare the model to nested model alternatives by means of a likelihood ratio test. To compare models that are not nested, information criteria such as AIC (Akaike’s information criterion), AICC (Akaike’s bias-corrected information criterion), and BIC (Schwarz Bayesian information criterion) are used. These criteria penalize the –2 log likelihood for the number of parameters.

Output 20.1.6: Fit Statistics

Fit Statistics
-2 Log Likelihood290.16169
AIC (smaller is better)322.16169
AICC (smaller is better)328.71590
SBC (smaller is better)363.84441


The "Parameter Estimates" table in Output 20.1.7 shows that many parameters have fairly large p-values, indicating that one or more of the model effects might not be necessary.

Output 20.1.7: Parameter Estimates

Parameter Estimates
ParameterDFEstimateStandard
Error
Chi-SquarePr > ChiSq
Intercept10.8819040.3827305.30950.0212
C1 01-0.1960020.2114820.85900.3540
C1 11-0.6051610.2635085.27420.0216
C1 21-0.0684580.2107760.10550.7453
C1 300...
C2 010.9611170.25548514.15210.0002
C2 110.7081880.2467688.23600.0041
C2 210.1617410.2663650.36870.5437
C2 300...
C3 01-0.2270160.2525610.80790.3687
C3 11-0.0947750.2295190.17050.6797
C3 210.0448010.2381270.03540.8508
C3 300...
C4 01-0.2804760.2635891.13220.2873
C4 110.0281570.2496520.01270.9102
C4 210.0478030.2403780.03950.8424
C4 300...
C5 01-0.8179360.21990113.83510.0002
C5 11-0.7105950.20626511.86840.0006
C5 21-0.6020800.2177247.64710.0057
C5 300...


Finally, the procedure displays the table in Output 20.1.8, which shows the amount of time (in seconds) that the genmod action spent performing different tasks in the analysis.

Output 20.1.8: Procedure Timing

Task Timing
TaskSecondsPercent
Setup and Parsing0.0231.04%
Levelization0.0116.88%
Model Initialization0.006.13%
SSCP Computation0.019.40%
Model Fitting0.0235.15%
Cleanup0.000.01%
Total0.07100.00%


Output 20.1.7 shows that the full model might have several nonsignificant effects. You can perform forward selection on these data by using the following statements. Effects that provide the best improvement for the default selection criterion, the Schwarz Bayesian information criterion (SBC), are added until no more effects can improve the selection criterion. The method subparameter in the selection parameter enables you to choose the selection method, and the details subparameter produces all tables that are related to model selection. The ODS TRACE command displays the full name paths of the output tables in the SAS log.

ods trace on;
proc cas;
  regression.genmod
  table={name='GETSTARTED'},
         class={'C1', 'C2', 'C3', 'C4', 'C5'},
         model={depvar='Y',
         effects={'C1', 'C2','C3', 'C4', 'C5'},
         dist='poisson',
         link='log'},
  selection={method='FORWARD', details='ALL'};
run;

The model selection tables are shown in Output 20.1.9 through Output 20.1.11. Results from the selected model are shown in Output 20.1.12 and Output 20.1.13.

The "Selection Information" table in Output 20.1.9 summarizes the settings for the model selection. At each step, an effect is added to the model only if it produces the most significant improvement in the SBC. The forward selection stops three steps after the smallest SBC is obtained, or when all effects have been added to the model.

Output 20.1.9: Selection Information

Results from regression.genmod

Selection Information
Selection MethodForward
Select CriterionSBC
Stop CriterionSBC
Effect Hierarchy EnforcedNone
Stop Horizon3


For each step of the selection process, the details=all subparameter displays the effects that are candidates for entering the model along with their selection criterion (SBC). Output 20.1.10 displays this table for the first step; the other steps are not shown here.

Output 20.1.10: Step1 Entry Candidates

Entry Candidates
RankEffectSBC
1C2342.8795
2C5347.5376
3C1355.7067
4C4361.7379
5C3365.7339


The details=all subparameter also displays the dimensions, fit statistics, and parameter estimates at each step of the selection process; these details are not shown here.

When the selection procedure is complete, the "Selection Summary" table in Output 20.1.11 shows the effects that were added to the model and the value of their selection criterion (and the choose and stop criteria, if they are specified). Step 0 refers to the model that contains only an intercept. In step 1, effect C2 made the most significant contribution to the model among the candidate effects, according to the SBC statistic. In step 2, C5 made the most significant contribution when an effect was added to a model that contains the intercept and C2. In the three subsequent steps, no effect could be added to the model that would reduce the SBC, so variable selection stopped because the stop horizon indicates that at most three steps beyond the minimum SBC value are to be taken.

In Output 20.1.11, the "Selection Summary" table is followed by three small tables that summarize why the process stopped and which model is selected.

Output 20.1.11: Selection Summary Information

Selection Summary
StepEffect
Entered
Number
Effects In
SBC
0Intercept1354.7980
1C22342.8795
2C53333.8182*
3C14341.7993
4C45351.8375
5C36363.8674
* Optimal Value Of Criterion

Selection stopped at a local minimum of the STOP criterion.

The model at step 2 is selected.

Selected Effects:Intercept C2 C5


Output 20.1.12 displays information about the selected model. Notice that the –2 log-likelihood value in the "Fit Statistics" table is larger than the value for the full model in Output 20.1.6. This is expected because the selected model contains only a subset of the parameters. Because the selected model is more parsimonious than the full model, the discrepancy between the –2 log likelihood and the information criteria is less severe than previously noted.

Output 20.1.12: Fit Statistics

Dimensions
Columns in Design9
Number of Effects3
Max Effect Columns4
Rank of Design7
Parameters in Optimization7

Fit Statistics
-2 Log Likelihood303.58036
AIC (smaller is better)317.58036
AICC (smaller is better)318.79775
SBC (smaller is better)335.81655


The parameter estimates of the selected model are shown in Output 20.1.13. Notice that the effects are listed in the "Parameter Estimates" table in the order in which they were specified in the model parameter and not in the order in which they were added to the model.

Output 20.1.13: Parameter Estimates

Parameter Estimates
ParameterDFEstimateStandard
Error
Chi-SquarePr > ChiSq
Intercept10.5280240.2152316.01860.0142
C2 010.9626360.23692116.5088<.0001
C2 110.8451320.23363413.08510.0003
C2 210.2132470.2488960.73410.3916
C2 300...
C5 01-0.7634910.20708213.59330.0002
C5 11-0.7859150.20127115.2471<.0001
C5 21-0.5412120.2086616.72750.0095
C5 300...


Poisson Regression

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 getStarted data to the comma-separated-value (CSV) file getStarted.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="getStarted.csv"}

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

The following code loads the regression action set and then uses the genmod action in the regression action set to fit a log-linked Poisson model to the getStarted data table. The table parameter names the input data table. The class parameter specifies that the variables C1, C2, C3, C4, and C5 are classification (nominal) variables; this parameter corresponds to the CLASS statement in the GENSELECT procedure. The model parameter specifies the model: the depvar subparameter specifies the variable y as the response (target) variable; the effects subparameter lists the five classification variables, C1–C5; the dist subparameter specifies that the response variable y has a Poisson distribution; and the link subparameter specifies a log link between the mean and the linear predictor. The itHist subparameter in the optimization parameter list produces a table that summarizes the steps of the optimization. Finally, the outputTables parameter outputs the fitted parameter estimates to the pe data table.

s:loadactionset{actionset='regression'}
m=s:genmod{table={name='getStarted'},
          class={'C1','C2','C3','C4','C5'},
      model={depvar='y',
             effects={'C1', 'C2', 'C3', 'C4', 'C5'},
             dist='poisson',
             link='log'},
      optimization={itHist='summary'},
      outputTables={names={parameterestimates='pe'}} }

The following commands display the tables that are produced by this action call:

print(m.ModelInfo)
print(m.NObs)
print(m.ClassInfo)
print(m.IterHistory)
print(m.ConvergenceStatus)
print(m.Dimensions)
print(m.FitStatistics)
print(m.ParameterEstimates)
print(m.Timing)

The full model has several nonsignificant effects; for details about the results of this analysis, see the CASL version of this example. You can perform forward selection on these data by using the following statements. Effects that provide the best improvement to the default selection criterion, the Schwarz Bayesian information criterion (SBC), are added until no more effects can improve the selection criterion. In the following program, the method subparameter in the selection parameter enables you to choose the selection method, and the details subparameter produces all tables that are related to model selection.

m=s:genmod{table={name='getStarted'},
           class={'C1','C2','C3','C4','C5'},
           model={depvar='y',
                  effects={'C1', 'C2', 'C3', 'C4', 'C5'},
                  dist='poisson',
                  link='log'},
           optimization={itHist='summary'},
           selection={method='forward', details='all'}}

The following commands display some of the tables that are produced by this action call:

print(m.SelectionInfo)
print(m["StepDetails.Step1.EntryCandidates"])
print(m["Summary.SelectionSummary"])
print(m["Summary.StopReason"])
print(m["Summary.SelectionReason"])
print(m["Summary.SelectedEffects"])
print(m["SelectedModel.Dimensions"])
print(m["SelectedModel.FitStatistics"])
print(m["SelectedModel.ParameterEstimates"])

The following command displays all the tables that are produced by this action call along with their full name paths:

for k,t in pairs(m) do print(k) print(t) end

For details about the results of this analysis, see the CASL version of this example.

Poisson Regression

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 getStarted data to the comma-separated-value (CSV) file getStarted.csv and then use the following code to load the CSV file into CAS:

s.upload_file('getStarted.csv')

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

The following code loads the regression action set and then uses the genmod action in the regression action set to fit a log-linked Poisson model to the getStarted data table. The table parameter names the input data table. The class parameter specifies that the variables C1, C2, C3, C4, and C5 are classification (nominal) variables; this parameter corresponds to the CLASS statement in the GENSELECT procedure. The model parameter specifies the model: the depvar subparameter specifies the variable y as the response (target) variable; the effects subparameter lists the five classification variables, C1–C5; the dist subparameter specifies that the response variable y has a Poisson distribution; and the link subparameter specifies a log link between the mean and the linear predictor. The itHist subparameter in the optimization parameter list produces a table that summarizes the steps of the optimization. Finally, the outputTables parameter outputs the fitted parameter estimates to the pe data table.

s.loadactionset(actionset='regression')
m=s.genmod(
   table='getStarted',
   classVars={'C1','C2','C3','C4','C5'},
   model={'depvar':'y',
          'effects':['C1', 'C2', 'C3', 'C4', 'C5'],
          'dist':'poisson',
          'link':'log'},
   optimization={'itHist':'summary'},
   outputTables={'names':{'parameterestimates':'pe'}} )

The following commands display the tables that are produced by this call:

print(m.ModelInfo)
print(m.NObs)
print(m.ClassInfo)
print(m.IterHistory)
print(m.ConvergenceStatus)
print(m.Dimensions)
print(m.FitStatistics)
print(m.ParameterEstimates)
print(m.Timing)

The full model has several nonsignificant effects; for details about the results of this analysis, see the CASL version of this example. You can perform forward selection on these data by using the following statements. Effects that provide the best improvement for the default selection criterion, the Schwarz Bayesian information criterion (SBC), are added until no more effects can improve the selection criterion. The method subparameter in the selection parameter enables you to choose the selection method, and the details subparameter produces all tables that are related to model selection.

m=s.genmod(
   table='getStarted',
   classVars={'C1','C2','C3','C4','C5'},
   model={'depvar':'y',
            'effects':['C1', 'C2', 'C3', 'C4', 'C5'],
            'dist':'poisson',
            'link':'log'},
   optimization={'itHist':'summary'},
   selection={'method':'forward', 'details':'all'})

The following commands display some of the tables that are produced by this action call:

print(m.SelectionInfo)
print(m["StepDetails.Step1.EntryCandidates"])
print(m["Summary.SelectionSummary"])
print(m["Summary.StopReason"])
print(m["Summary.SelectionReason"])
print(m["Summary.SelectedEffects"])
print(m["SelectedModel.Dimensions"])
print(m["SelectedModel.FitStatistics"])
print(m["SelectedModel.ParameterEstimates"])

The following command displays all the tables that are produced by this action call along with their full name paths:

print(m)

For details about the results of this analysis, see the CASL version of this example.

Poisson Regression

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 getStarted data to the comma-separated-value (CSV) file getStarted.csv and then use the following code to load the CSV file into CAS:

m <- cas.read.csv(s, "getStarted.csv", casOut=list(name="getStarted"))

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

The following code loads the regression action set and then uses the genmod action in the regression action set to fit a log-linked Poisson model to the getStarted data table. The table parameter names the input data table. The class parameter specifies that the variables C1, C2, C3, C4, and C5 are classification (nominal) variables; this parameter corresponds to the CLASS statement in the GENSELECT procedure. The model parameter specifies the model: the depvar subparameter specifies the variable y as the response (target) variable; the effects subparameter lists the five classification variables, C1–C5; the dist subparameter specifies that the response variable y has a Poisson distribution; and the link subparameter specifies a log link between the mean and the linear predictor. The itHist subparameter in the optimization parameter list produces a table that summarizes the steps of the optimization. Finally, the outputTables parameter outputs the fitted parameter estimates to the pe data table.

m <- cas.builtins.loadActionSet(s, actionset='regression')
m <- cas.regression.genmod(s,
   table='getStarted',
   class=list('C1','C2','C3','C4','C5'),
   model=list(depvar='y',
              effects=list('C1', 'C2', 'C3', 'C4', 'C5'),
              dist='poisson',
              link='log'),
   optimization=list(itHist='summary'),
   outputTables=list(names=list(parameterestimates='pe'))
)

The following commands display the tables that are produced by this action call:

print(m$ModelInfo)
print(m$NObs)
print(m$ClassInfo)
print(m$IterHistory)
print(m$ConvergenceStatus)
print(m$Dimensions)
print(m$FitStatistics)
print(m$ParameterEstimates)
print(m$Timing)

The full model has several nonsignificant effects; for details about the results of this analysis, see the CASL version of this example. You can perform forward selection on these data by using the following statements. Effects that provide the best improvement to the default selection criterion, the Schwarz Bayesian information criterion (SBC), are added until no more effects can improve the selection criterion. In the following program, the method subparameter in the selection parameter enables you to choose the selection method, and the details subparameter produces all tables that are related to model selection.

m <- cas.regression.genmod(s,
   table='getStarted',
   class=list('C1','C2','C3','C4','C5'),
   model=list(depvar='y',
              effects=list('C1', 'C2', 'C3', 'C4', 'C5'),
              dist='poisson',
              link='log'),
   optimization=list(itHist='summary'),
   selection=list(method='forward', details='all')
)

The following commands display some of the tables that are produced by this action call:

print(m$SelectionInfo)
print(m$StepDetails.Step1.EntryCandidates)
print(m$Summary.SelectionSummary)
print(m$Summary.StopReason)
print(m$Summary.SelectionReason)
print(m$Summary.SelectedEffects)
print(m$SelectedModel.Dimensions)
print(m$SelectedModel.FitStatistics)
print(m$SelectedModel.ParameterEstimates)

The following command displays all the tables that are produced by this action call along with their full name paths:

print(m)

For details about the results of this analysis, see the CASL version of this example.

Last updated: September 13, 2022