Regression Action Set
The glmScore Action
- 22.4 Linear Regression
Linear 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 demonstrates how you train and store a linear regression model and then restore this model to score new data.
The following DATA step runs on the CAS server and creates the data table mycas.analysisData, which contains 1,000 observations and six variables. This DATA step assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The DATA step forms the response y by adding an error term to a true model that depends on a classification variable c1 and a continuous variable x1:
data mycas.analysisData;
call streamInit(1);
do id=1 to 100;
x1 = rand('UNIFORM');
x2 = rand('NORMAL');
c1 = 1 + mod(id,3);
c2 = rand('BERNOULLI',0.6);
y = 1 + x1 + c1 + 0.01*rand('NORMAL');
output;
end;
run;
The following PROC CAS statements fit a linear regression model to these data by using forward selection and store the model:
proc cas;
regression.glm
table='analysisData',
class={'c1','c2'},
model={depvar='y',
effects={'c1', 'c2', 'x1', 'x2'}},
store={name='myModel', replace=true},
selection={method='forward'},
output={casOut={name='out1', replace=true},
copyVars='ALL',
pred='pred', resid='resid', rstudent='rstudent'};
run;quit;
The store parameter stores the model in a special data table named mycas.myModel. The output parameter scores the observations in the mycas.analysisData data table and writes to the mycas.out1 data table the predicted values, residuals, studentized residuals with the current observation deleted, and all the variables from the original data table.
Output 22.4.1 displays the "Selected Effects" table, which shows that the selected model contains only the variables x1 and c1.
Output 22.4.1: Selected Effects
| Selected Effects: | Intercept c1 x1 |
|---|
Output 22.4.2 displays information about the two output tables you requested, namely the mycas.out1 table and the mycas.myModel table.
Output 22.4.2: Output CAS Tables
| Output CAS Tables | |||
|---|---|---|---|
| CAS Library | Name | Number of Rows | Number of Columns |
| CASUSERHDFS(sasred) | out1 | 100 | 9 |
| CASUSERHDFS(sasred) | myModel | 1 | 2 |
The following PROC CAS statements take the model that was previously fit and stored in mycas.myModel and use the glmScore action in the regression action set to score the original data set:
proc cas;
regression.glmScore
restore='myModel',
table='analysisData',
fitData='true',
casOut={name='out2', replace=true},
copyVars='ALL',
pred='pred', resid='resid', rstudent='rstudent';
run;
The restore parameter names the stored model to be used for scoring, the table parameter names the table to be scored, the casOut parameter names the output data table, and the copyVars='ALL' parameter requests that all variables in the analysisData data table be copied to the out2 output table. The pred parameter includes the predicted response, the resid parameter includes the residual, and the rstudent parameter includes the studentized residual with the current observation deleted. Because the RSTUDENT statistic is meaningful only for observations that were used in fitting the model, this statistic is produced only when you also specify the fitData parameter to indicate that you are scoring the data that were used to fit the model.
The glmScore action produces a single displayed table, which is shown in Output 22.4.3. This table displays information about the output table that contains the statistics that you requested.
Output 22.4.3: Output CAS Tables
| Output CAS Tables | |||
|---|---|---|---|
| CAS Library | Name | Number of Rows | Number of Columns |
| CASUSERHDFS(sasred) | out2 | 100 | 9 |
The following PROC PRINT steps display the first five observations in the mycas.out1 table, which is produced by the glm action, and the first five observations in the mycas.out2 table, which is produced by the glmScore action. You see that because both these actions were used to score the same input data table, the scored output is identical.
proc print data=mycas.out1(where=(id<=5));
run;
proc print data=mycas.out2(where=(id<=5));
run;
Output 22.4.4: Output Data Table from the Fitted Model
| Obs | pred | resid | rstudent | id | x1 | x2 | c1 | c2 | y |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 3.88546 | -0.001518 | -0.13776 | 1 | 0.88387 | 0.46319 | 2 | 0 | 3.88394 |
| 2 | 4.68936 | 0.015206 | 1.38519 | 2 | 0.68936 | -0.06703 | 3 | 0 | 4.70457 |
| 3 | 2.56903 | 0.007859 | 0.70719 | 3 | 0.56683 | 0.51495 | 1 | 0 | 2.57689 |
| 4 | 3.79147 | -0.018953 | -1.73863 | 4 | 0.79010 | 0.36049 | 2 | 0 | 3.77252 |
| 5 | 4.61389 | 0.003476 | 0.31294 | 5 | 0.61406 | 1.02322 | 3 | 1 | 4.61737 |
Output 22.4.5: Output Data Table from the Restored Model
| Obs | pred | resid | rstudent | id | x1 | x2 | c1 | c2 | y |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 3.88546 | -0.001518 | -0.13776 | 1 | 0.88387 | 0.46319 | 2 | 0 | 3.88394 |
| 2 | 4.68936 | 0.015206 | 1.38519 | 2 | 0.68936 | -0.06703 | 3 | 0 | 4.70457 |
| 3 | 2.56903 | 0.007859 | 0.70719 | 3 | 0.56683 | 0.51495 | 1 | 0 | 2.57689 |
| 4 | 3.79147 | -0.018953 | -1.73863 | 4 | 0.79010 | 0.36049 | 2 | 0 | 3.77252 |
| 5 | 4.61389 | 0.003476 | 0.31294 | 5 | 0.61406 | 1.02322 | 3 | 1 | 4.61737 |
The following DATA steps produce two new CAS tables that contain data to be scored with the stored model that the glm action produced:
data mycas.scoreData1;
call streamInit(3);
do id=1 to 5;
x1 = rand('UNIFORM');
c1 = 1 + mod(id,3);
y = 1 + x1 + c1 + 0.01*rannor(1);
output;
end;
run;
data mycas.scoreData2;
call streamInit(3);
do id=1 to 5;
x1 = rand('UNIFORM');
c1 = 1 + mod(id,4);
output;
end;
run;
Neither of these CAS tables contain the variables x2 and c2, which were used in fitting the stored model. However, because both these CAS tables contain all the selected variables that are shown in Output 22.4.1, you can score the data in these tables by using the glmScore action.
The following PROC CAS statements score the mycas.scoreData1 table:
proc cas;
regression.glmScore
restore='myModel',
table='scoreData1',
casOut={name='out3', replace=true},
copyVars='ALL',
pred='pred', resid='resid', rstudent='rstudent';
run;
Even though you requested that the RSTUDENT statistic be produced, it is not included in the output because this statistic is not meaningful for observations that were not used in training the model. Instead, the following warning is displayed in the SAS log:
WARNING: The cooksD, covratio, dffits, likeDist, press, and rStudent statistics
are not available when you score new data or when the fitData
parameter is not set to True.
The following PROC PRINT steps display the mycas.out3 table, which contains the requested statistics along with the variables that are copied from the scoreData1 table:
proc print data=mycas.out3;
run;
Output 22.4.6: Scored Data Table from the Restored Model
| Obs | pred | resid | id | x1 | c1 | y |
|---|---|---|---|---|---|---|
| 1 | 3.61063 | 0.017099 | 1 | 0.60969 | 2 | 3.62773 |
| 2 | 3.54811 | -0.011636 | 4 | 0.54731 | 2 | 3.53648 |
| 3 | 4.58940 | -0.000566 | 2 | 0.58964 | 3 | 4.58884 |
| 4 | 4.96578 | 0.021734 | 5 | 0.96513 | 3 | 4.98751 |
| 5 | 2.93953 | 0.000900 | 3 | 0.93646 | 1 | 2.94042 |
The following PROC CAS statements score the mycas.scoreData2 table, and the subsequent PROC PRINT step displays the scored results in Output 22.4.7. Because this table does not contain values for the response variable y, you cannot obtain any residual diagnostics.
proc cas;
regression.glmScore
restore='myModel',
table='scoreData2',
casOut={name='out4', replace=true},
copyVars='ALL',
pred='pred';
run;
proc print data=mycas.out4;
run;
Output 22.4.7: Scored Data Table from the Restored Model
| Obs | pred | id | x1 | c1 |
|---|---|---|---|---|
| 1 | 3.61063 | 1 | 0.60969 | 2 |
| 2 | 4.58940 | 2 | 0.58964 | 3 |
| 3 | . | 3 | 0.93646 | 4 |
| 4 | 2.54946 | 4 | 0.54731 | 1 |
| 5 | 3.96691 | 5 | 0.96513 | 2 |
You can see that the value of pred for observation 3 is missing. This is because the level of the classification variable c1 for this observation is 4, which was not an observed level of the variable c1 in the CAS table mycas.analysisData on which the restored model was trained.
Linear Regression
This section contains Lua code that creates the same analysisData table that is shown in the CASL example. It highlights sections of the example where the Lua syntax differs significantly from the CASL syntax. See the CASL version of this example for discussions of the data, syntax features, and the output from running these examples. For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Lua client as follows to create the simulated data in your CAS session:
s:runCode{single='YES',code=[[
data analysisData;
call streamInit(1);
do id=1 to 100;
x1 = rand('UNIFORM');
x2 = rand('NORMAL');
c1 = 1 + mod(id,3);
c2 = rand('BERNOULLI',0.6);
y = 1 + x1 + c1 + 0.01*rand('NORMAL');
output;
end;
run;
]]
}
The following code loads the regression action set and uses the glm action to fit a linear regression model to these data by using forward selection and to store the model:
s:loadactionset{actionset="regression"}
mfit=s:glm{
table='analysisData',
class={'c1','c2'},
model={depvar='y',
effects={'c1', 'c2', 'x1', 'x2'}},
store={name='myModel', replace=true},
selection={method='forward'},
output={casOut={name='out1', replace=true},
copyVars='ALL',
pred='pred', resid='resid', rstudent='rstudent'} }
The store parameter stores the model in a special data table named myModel. The output parameter scores the observations in the analysisData data table and writes to the out1 data table the predicted values, residuals, studentized residuals with the current observation deleted, and all the variables from the original data table.
The following command displays information about the out1 table:
print(mfit.OutputCasTables)
The following commands use the glmScore action in the regression action set to score the original data set, using the model that was previously fit:
mscore=s:glmScore{
table='analysisData',
restore='myModel',
casOut={name='out2', replace=true},
fitData='true',
copyVars='ALL',
pred='pred',
resid='resid',
rstudent='rstudent' }
The table parameter names the table to be scored, the restore parameter names the stored model to be used for scoring, the casOut parameter names the output data table, the copyVars='ALL' parameter requests that all variables in the analysisData data table be copied to the out2 output data table, and the remaining parameters name the statistics to store in the output data table.
The following command displays information about the out2 table:
print(mscore.OutputCasTables)
For details about the results of this analysis, see the CASL version of this example.
Linear Regression
This section contains Python code that creates the same analysisData table that is shown in the CASL example. It highlights sections of the example where the Python syntax differs significantly from the CASL syntax. See the CASL version of this example for discussions of the data, syntax features, and the output from running these examples. For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Python client as follows to create the simulated data in your CAS session:
s.runCode(single='YES',code="""
data analysisData;
call streamInit(1);
do id=1 to 100;
x1 = rand('UNIFORM');
x2 = rand('NORMAL');
c1 = 1 + mod(id,3);
c2 = rand('BERNOULLI',0.6);
y = 1 + x1 + c1 + 0.01*rand('NORMAL');
output;
end;
run;"""
)
The following code loads the regression action set and uses the glm action to fit a linear regression model to these data by using forward selection and to store the model:
s.loadactionset(actionset="regression")
mfit=s.glm(
table='analysisData',
classvars={'c1','c2'},
model={'depvar':'y',
'effects':['c1', 'c2', 'x1', 'x2']},
store={'name':'myModel', 'replace':'True'},
selection={'method':'forward'},
output={'casOut':{'name':'out1', 'replace':'True'},
'copyVars':'ALL',
'pred':'pred', 'resid':'resid', 'rstudent':'rstudent'}
)
The store parameter stores the model in a special data table named myModel. The output parameter scores the observations in the analysisData data table and writes to the out1 data table the predicted values, residuals, studentized residuals with the current observation deleted, and all the variables from the original data table.
The following command displays information about the out1 table:
print(mfit.OutputCasTables)
The following code uses the glmScore action in the regression action set to score the original data set, using the model that was previously fit:
mscore=s.glmScore(
table='analysisData',
restore='myModel',
casOut={'name':'out2', 'replace':'True'},
fitData='true',
copyVars='ALL',
pred='pred',
resid='resid',
rstudent='rstudent'
)
The table parameter names the table to be scored, the restore parameter names the stored model to be used for scoring, the casOut parameter names the output data table, the copyVars='ALL' parameter requests that all variables in the analysisData data table be copied to the out2 output data table, and the remaining parameters name the statistics to store in the output data table.
The following command displays information about the out2 table:
print(mscore.OutputCasTables)
For details about the results of this analysis, see the CASL version of this example.
Linear Regression
This section contains R code that creates the same analysisData table that is shown in the CASL example. It highlights sections of the example where the R syntax differs significantly from the CASL syntax. See the CASL version of this example for discussions of the data, syntax features, and the output from running these examples. For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your R client as follows to create the simulated data in your CAS session:
m <- cas.builtins.loadActionSet(s, actionset='dataStep')
m <- cas.dataStep.runCode(s,single='YES',code="
data analysisData;
call streamInit(1);
do id=1 to 100;
x1 = rand('UNIFORM');
x2 = rand('NORMAL');
c1 = 1 + mod(id,3);
c2 = rand('BERNOULLI',0.6);
y = 1 + x1 + c1 + 0.01*rand('NORMAL');
output;
end;
run;"
)
The following code loads the regression action set and uses the glm action to fit a linear regression model to these data by using forward selection and to store the model:
m <- cas.builtins.loadActionSet(s, actionset='regression')
mfit <- cas.regression.glm(s,
table='analysisData',
class=list('c1','c2'),
model=list(depvar='y',
effects=list('c1', 'c2', 'x1', 'x2')),
store=list(name='myModel', replace=TRUE),
selection=list(method='forward'),
output=list(casOut=list(name='out1', replace=TRUE),
copyVars='ALL',
pred='pred', resid='resid', rstudent='rstudent')
)
The store parameter stores the model in a special data table named myModel. The output parameter scores the observations in the analysisData data table and writes to the out1 data table the predicted values, residuals, studentized residuals with the current observation deleted, and all the variables from the original data table.
The following command displays information about the out1 table:
print(mfit$OutputCasTables)
The following code uses the glmScore action in the regression action set to score the original data set, using the model that was previously fit:
mscore <- cas.regression.glmScore(s,
table='analysisData',
restore='myModel',
casOut=list(name='out2', replace=TRUE),
fitData=TRUE,
copyVars='ALL',
pred='pred',
resid='resid',
rstudent='rstudent'
)
The table parameter names the table to be scored, the restore parameter names the stored model to be used for scoring, the casOut parameter names the output data table, the copyVars='ALL' parameter requests that all variables in the analysisData data table be copied to the out2 output data table, and the remaining parameters name the statistics to store in the output data table.
The following command displays information about the out2 table:
print(mscore$OutputCasTables)
For details about the results of this analysis, see the CASL version of this example.