Regression Action Set
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.
This example uses the mycas.getStarted data table and the model selected in Example 19.1 to demonstrate how to store and restore your model. These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.getStarted;
input C1-C5 Y @@;
id = _n_;
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
... more lines ...
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 generalized linear model to these data and store the model. The store parameter stores the model in a special data table named mycas.myModel. The output parameter scores the observations in the mycas.getStarted data table and writes to the mycas.out1 data table the predicted probabilities, the Pearson chi-square residuals, and the predicted response level, as well as the response and the covariates from the original data table. The model fit results are displayed in Example 19.1.
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'},
store='myModel',
output={casOut={name='out1',replace=true},
copyVars={'y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5'},
pred='pred',resChi='reschi',into='into'};
run;
The following PROC CAS statements take the model that was previously fit and stored in mycas.myModel and use the genmodScore action in the regression action set to score the original data set. The restore parameter names the stored model to be used for scoring, the table parameter names the table to be scored, the fitData parameter indicates that you are scoring the data that were used to fit the model, the casOut parameter names the output data table, and the copyVars parameter lists several variables to copy from the getStarted data table into the out2 output table. The pred parameter includes the predicted probabilities, the reschi parameter includes the Pearson chi-square residuals, and the into parameter includes the response level into which the observations are classified. The subsequent PROC PRINT steps display the first five observations in the mycas.out1 and mycas.out2 data tables, respectively.
proc cas;
regression.genmodScore
restore='myModel',
table='getStarted',
fitData='true',
casOut={name='out2',replace=true},
copyVars={'y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5'},
pred='pred',resChi='reschi',into='into';
run;
proc print data=mycas.out1(where=(id<=5));
run;
proc print data=mycas.out2(where=(id<=5));
run;
The output from this analysis is displayed by default but is not displayed here. The PROC PRINT results are shown in Output 19.2.1 and Output 19.2.2.
Output 19.2.1: Output Data Table from the Fitted Model
| Obs | pred | reschi | Y | id | C1 | C2 | C3 | C4 | C5 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1.85760 | 0.10448 | 2 | 1 | 0 | 3 | 1 | 1 | 3 |
| 2 | 0.93332 | 1.10413 | 2 | 4 | 1 | 2 | 0 | 0 | 3 |
| 3 | 0.88323 | 1.18830 | 2 | 2 | 2 | 3 | 0 | 3 | 1 |
| 4 | 0.78804 | 0.23877 | 1 | 5 | 0 | 2 | 1 | 0 | 1 |
| 5 | 0.79237 | 0.23326 | 1 | 3 | 1 | 3 | 2 | 2 | 2 |
Output 19.2.2: Output Data Table from the Restored Model
| Obs | pred | reschi | Y | id | C1 | C2 | C3 | C4 | C5 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1.85760 | 0.10448 | 2 | 1 | 0 | 3 | 1 | 1 | 3 |
| 2 | 0.93332 | 1.10413 | 2 | 4 | 1 | 2 | 0 | 0 | 3 |
| 3 | 0.88323 | 1.18830 | 2 | 2 | 2 | 3 | 0 | 3 | 1 |
| 4 | 0.78804 | 0.23877 | 1 | 5 | 0 | 2 | 1 | 0 | 1 |
| 5 | 0.79237 | 0.23326 | 1 | 3 | 1 | 3 | 2 | 2 | 2 |
Note: If you score data that were not used to fit or train the model, the following regression diagnostic statistics no longer carry their original meaning and are set to missing: CBAR, DIFCHISQ, DIFDEV, H, RESCHI, RESDEV, RESLIK, STDRESCHI, and STDRESDEV. Specifying the fitData parameter indicates that you are scoring the original data table, and these statistics are computed. If you stored a model that contains a partition variable, then the values of that variable are also used to determine whether the diagnostic statistics should be computed.
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, uses the genmod action to fit the generalized linear model selected in Example 19.1 to the getStarted data table, and demonstrates how to store and restore your model.
The store parameter stores the model in the myModel data table. The output parameter scores the observations in the getStarted data table and writes to the out1 data table the predicted probabilities, the Pearson chi-square residuals, and the predicted response level, as well as the response and the covariates from the original data table.
s:loadactionset{actionset="regression"}
m=s:genmod{
table='getStarted',
class={'C1','C2','C3','C4','C5'},
model={depvar='y',
effects={'C1', 'C2', 'C3', 'C4', 'C5'},
dist='poisson',
link='log'},
store='myModel',
output={casOut={name='out1',replace=true},
pred='pred',resChi='reschi',into='into',
copyVars={'y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5'}} }
The following commands use the genmodScore action in the regression action set to score the original data set by using the model that was previously fit. The table parameter names the table to be scored, the restore parameter names the stored model to be used for scoring, and the output parameter names the output data table and lists a number of statistics and variables to store in the table.
s:loadactionset{actionset="regression"}
m=s:genmodScore{
table='getStarted',
restore='myModel',
fitData='true',
casOut={name='out2',replace=true},
pred='pred',resChi='reschi',into='into',
copyVars={'y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5'} }
The following commands display the first five observations in the out1 and out2 data tables, respectively:
a=s:fetch{table={name='out1', orderby='id'}, to=5}
b=s:fetch{table={name='out2', orderby='id'}, to=5}
print(a)
print(b)
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, uses the genmod action to fit the generalized linear model selected in Example 19.1 to the getStarted data table, and demonstrates how to store and restore your model.
The store parameter stores the model in the myModel data table. The output parameter scores the observations in the getStarted data table and writes to the out1 data table the predicted probabilities, the Pearson chi-square residuals, and the predicted response level, as well as the response and the covariates from the original 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']},
store={'name':'myModel','replace':'true'},
output={'casOut':{'name':'out1','replace':'true'},
'pred':'pred','resChi':'reschi','into':'into',
'copyVars':['y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5']} )
The following commands use the genmodScore action in the regression action set to score the original data set by using the model that was previously fit. The table parameter names the table to be scored, the restore parameter names the stored model to be used for scoring, and the output parameter names the output data table and lists a number of statistics and variables to store in the table.
m=s.genmodScore(
table='getStarted',
restore='myModel',
fitData='true',
casOut={'name':'out2','replace':'true'},
copyVars=['y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5'],
pred='pred',resChi='reschi',into='into' )
The following commands display the first five observations in the out1 and out2 data tables, respectively:
a=s.fetch(table={'name':'out1','orderby':'id'}, to='5')
b=s.fetch(table={'name':'out2','orderby':'id'}, to='5')
print(a)
print(b)
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, uses the genmod action to fit the generalized linear model selected in Example 19.1 to the getStarted data table, and demonstrates how to store and restore your model.
The store parameter stores the model in the myModel data table. The output parameter scores the observations in the getStarted data table and writes to the out1 data table the predicted probabilities, the Pearson chi-square residuals, and the predicted response level, as well as the response and the covariates from the original 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'),
store=list(name='myModel', replace=TRUE),
output=list(casOut=list(name='out1',replace=TRUE),
pred='pred',resChi='reschi',into='into',
copyVars=list('y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5'))
)
The following commands use the genmodScore action in the regression action set to score the original data set by using the model that was previously fit. The table parameter names the table to be scored, the restore parameter names the stored model to be used for scoring, and the output parameter names the output data table and lists a number of statistics and variables to store in the table.
m <- cas.regression.genmodScore(s,
table='getStarted',
restore='myModel',
fitData=TRUE,
casOut=list(name='out2',replace=TRUE),
pred='pred',resChi='reschi',into='into',
copyVars=list('y', 'id', 'C1', 'C2', 'C3', 'C4', 'C5')
)
The following commands display the first five observations in the out1 and out2 data tables, respectively:
cas.table.fetch(s, table='out1', orderby='id', to='5')
cas.table.fetch(s, table='out2', orderby='id', to='5')
For details about the results of this analysis, see the CASL version of this example.