Regression Action Set
The logisticCode Action
Binary Logistic 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 from Example 20.5 to demonstrate how to generate SAS DATA step code that can be used to score new data tables. It also demonstrates how to store and restore your model.
The following PROC CAS statements use the logistic action in the regression action set to fit a logistic model to these data and store the model. The store parameter stores the model in a special data table named mycas.myModel. The model fit results are displayed in Example 20.5.
proc cas;
regression.logistic
table='getStarted',
class={'C'},
model={depvar='y',
effects={'C', 'x2', 'x8'}},
store={name='myModel', replace=true};
run;
The following PROC CAS statements use the logisticCode action in the regression action set to produce SAS DATA step code, which the runCodeTable action in the dataStep action set then uses to score the getStarted data without refitting the model. The restore parameter names the stored model to be used for scoring, the pcatall parameter adds code to generate probabilities for every response level, and the casOut parameter names the output data table in which to store the program.
The runCodeTable action in the dataStep action set applies this program to the getStarted data table and writes the results to the mycas.scored data table. The subsequent PROC PRINT step displays the first five scored observations from the mycas.scored data table.
proc cas;
regression.logisticCode
restore='myModel',
pcatall=true,
casOut={name='code1', replace=true};
dataStep.runCodeTable / codeTable="code1" table="getStarted" casOut="scored";
quit;
proc print data=mycas.scored(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 20.9.1.
Output 20.9.1: First Five Scored Observations Using the Fitted Model
| Obs | id | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | C | y | I_y | P_y0 | P_y1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 13.0 | 1.00000 | 2.5 | 26 | 93 | 1.9 | 70 | 0.1 | 1.2 | 2.1 | A | 1 | 1 | 0.40467 | 0.59533 |
| 2 | 2 | 7.9 | 0.28571 | 3.4 | 16 | 29 | 1.7 | 80 | 0.9 | 7.8 | 3.6 | B | 1 | 1 | 0.43332 | 0.56668 |
| 3 | 3 | 4.9 | 0.42857 | 2.6 | 15 | 94 | 1.1 | 40 | 0.8 | 2.2 | 0.7 | I | 1 | 1 | 0.37863 | 0.62137 |
| 4 | 4 | 8.6 | 0.28571 | 3.2 | 37 | 83 | 1.2 | 80 | 0.4 | 3.1 | 7.5 | J | 0 | 1 | 0.49592 | 0.50408 |
| 5 | 5 | 11.0 | 0.28571 | 1.7 | 20 | 19 | 1.6 | 0 | 0.8 | 6.3 | 1.4 | I | 0 | 1 | 0.41615 | 0.58385 |
Binary Logistic 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 logistic action to fit a logistic model 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.
s:loadactionset{actionset="regression"}
m=s:logistic{
table='getStarted',
class={'C'},
model={depvar='y',
effects={'C', 'x2', 'x8'}},
store={name='myModel', replace=true}}
The following commands use the logisticCode action in the regression action set to produce SAS DATA step code, which is then used by the runCodeTable action in the dataStep action set to score the getStarted data without refitting the model. The restore parameter names the stored model to be used for scoring, the pcatall parameter adds code to generate probabilities for every response level, and the casOut parameter names the output data table in which to store the program.
The runCodeTable action in the dataStep action set applies this program to the getStarted data table and writes the results to the scored data table.
m=s:logisticCode{
restore='myModel',
pCatAll='true',
casOut={name='code1', replace=true} }
s:loadactionset{actionset="dataStep"}
n=s:runCodeTable{
codetable='code1',
table='getStarted',
casOut='scored'}
The following commands display the first five observations in the scored data table:
a=s:fetch{table={name='scored', orderby='id'}, to=5}
print(a)
Binary Logistic 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 logistic action to fit a logistic model 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.
s.loadactionset(actionset='regression')
m=s.logistic(
table='getStarted',
classvars='C',
model={'depvar':'y',
'effects':['C', 'x2', 'x8']},
store={'name':'myModel', 'replace':'true'} )
The following commands use the logisticCode action in the regression action set to produce SAS DATA step code, which is then used by the runCodeTable action in the dataStep action set to score the getStarted data without refitting the model. The restore parameter names the stored model to be used for scoring, the pcatall parameter adds code to generate probabilities for every response level, and the casOut parameter names the output data table in which to store the program.
The runCodeTable action in the dataStep action set applies this program to the getStarted data table and writes the results to the scored data table.
m=s.logisticCode(
restore='myModel',
pCatAll='true',
casOut={'name':'code1', 'replace':'true'} )
s.loadactionset(actionset='datastep')
n=s.runCodeTable(
codeTable='code1',
table='getStarted',
casOut='scored')
The following commands display the first five observations in the scored data table:
a=s.fetch(table={'name':'scored','orderby':'id'}, to='5')
print(a)
Binary Logistic 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 logistic action to fit a logistic model 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.
m <- cas.builtins.loadActionSet(s, actionset='regression')
m <- cas.regression.logistic(s,
table='getStarted',
class='C',
model=list(depvar='y',
effects=list('C', 'x2', 'x8')),
store=list(name='myModel', replace=TRUE)
)
The following commands use the logisticCode action in the regression action set to produce SAS DATA step code, which is then used by the runCodeTable action in the dataStep action set to score the getStarted data without refitting the model. The restore parameter names the stored model to be used for scoring, the pcatall parameter adds code to generate probabilities for every response level, and the casOut parameter names the output data table in which to store the program.
The runCodeTable action in the dataStep action set applies this program to the getStarted data table and writes the results to the scored data table.
m <- cas.regression.logisticCode(s,
restore='myModel',
pCatAll=TRUE,
casOut=list(name='code1', replace=TRUE)
)
m <- cas.builtins.loadActionSet(s, actionset='dataStep')
m <- cas.dataStep.runCodeTable(s,
codeTable='code1',
table='getStarted',
casOut='scored'
)
The following command displays the first five observations in the scored data table:
cas.table.fetch(s, table='scored', orderby='id', to='5')
For details about the results of this analysis, see the CASL version of this example.