Model-Based Clustering Action Set
The mbcScore Action
Two-Component Gaussian Mixture
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 that is selected in Example 12.1 to demonstrate how to compute observationwise cluster weights and log likelihoods. 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 duration wait @@;
id=_n_;
datalines;
3.600 79 1.800 54 3.333 74 2.283 62 4.533 85 2.883 55 4.700 88 3.600 85
... more lines ...
1.983 43 2.250 60 4.750 75 4.117 81 2.150 46 4.417 90 1.817 46 4.467 74
;
The following PROC CAS statements use the mbcFit action to find the best-fitting model for these data from a list of several different models and to store that model. The table parameter names the input data table to analyze. The effects subparameter in the model parameter defines the variables to analyze. The nClusters parameter fits models that have two to four Gaussian components, the noise parameter fits models without a noise component, and the covStruct parameter fits models that have three covariance structures. The output parameter scores the observations in the mycas.getStarted table, writing the cluster association weights for each observation to the mycas.out1 data table. The variables that contain the cluster association weights have the "WT" prefix in the mycas.out1 table. The copyVars subparameter in the output parameter includes the analysis variables Duration and Wait and the identifying variable ID in the output data table. The store parameter stores the model in a special data table named mycas.myModel. Finally, the seed parameter specifies the random seed for initial clusterings. The model fit results are displayed in Example 12.1.
proc cas;
action mbc.mbcFit /
table={name='getStarted'}
model={effects={{vars={name='duration', name='wait'}}}}
nClusters={2, 3, 4}
noise='N'
covStruct={'VII', 'EII', 'VVV'}
output={casOut={name='out1'}, nextClus='WT', copyVars={'Duration','Wait','ID'}}
store={name='myModel'}
seed=9872;
run;
The following PROC CAS statements take the model that was previously fit and stored in the mycas.myModel data table and use the mbcScore action in the mbc action set to score the original data. The results are stored in the mycas.out2 data table.
proc cas;
action mbc.mbcScore /
table={name='getStarted'}
restore={name='myModel'}
casOut={name='out2'}
nextClus='WT'
copyVars={'Duration','Wait','ID'};
run;
The following PROC PRINT statements show the first five observations in the mycas.out1 and mycas.out2 data tables, respectively:
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 12.2.1 and Output 12.2.2.
Output 12.2.1: Output Data Table from the Fitted Model
| Obs | WT1 | WT2 | duration | wait | id |
|---|---|---|---|---|---|
| 1 | 1.00000 | 0.00000 | 3.600 | 79 | 1 |
| 2 | 0.00001 | 0.99999 | 2.283 | 62 | 4 |
| 3 | 0.00000 | 1.00000 | 1.800 | 54 | 2 |
| 4 | 1.00000 | 0.00000 | 4.533 | 85 | 5 |
| 5 | 0.99999 | 0.00001 | 3.333 | 74 | 3 |
Output 12.2.2: Output Data Table from the Restored Model
| Obs | WT1 | WT2 | duration | wait | id |
|---|---|---|---|---|---|
| 1 | 1.00000 | 0.00000 | 3.600 | 79 | 1 |
| 2 | 0.00001 | 0.99999 | 2.283 | 62 | 4 |
| 3 | 0.00000 | 1.00000 | 1.800 | 54 | 2 |
| 4 | 1.00000 | 0.00000 | 4.533 | 85 | 5 |
| 5 | 0.99999 | 0.00001 | 3.333 | 74 | 3 |
Two-Component Gaussian Mixture
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 mbc action set, uses the mbcFit action to find the best-fitting model for these data from a list of several different models, stores that model, and writes posterior weights for each observation to an output data table. The table parameter names the input data table to analyze. The effects subparameter in the model parameter defines the variables to analyze. The nClusters parameter fits models that have two to four Gaussian components, the noise parameter fits models without a noise component, and the covStruct parameter fits models that have three covariance structures. The output parameter scores the observations in the getStarted table, writing the cluster association weights for each observation to the Out1 data table. The variables that contain the cluster association weights have the "WT" prefix in the Out1 table. The copyVars subparameter in the output parameter includes the analysis variables Duration and Wait and the identifying variable ID in the output data table. The store parameter stores the model in a special data table named myModel. Finally, the seed parameter specifies the random seed for initial clusterings.
s:loadActionSet{actionSet='mbc'}
m = s:mbcFit{table={name='getStarted'},
model={effects={{vars={name='duration', name='wait'}}}},
nClusters={2, 3, 4},
noise='N',
covStruct={'VII', 'EII', 'VVV'},
output={casOut={name='out1'},
nextClus='WT',
copyVars={'Duration','Wait','ID'}},
store={name='myModel'},
seed=9872}
The following commands apply the stored model myModel to the same data by using the mbcScore action and write posterior weights for each observation to a separate output data table, Out2:
m = s:mbcScore{table={name='getStarted'},
restore={name='myModel'},
casOut={name='out2'},
nextClus='WT',
copyVars={'Duration','Wait','ID'}}
The following statements display the first five observations in the Out1 and Out2 data tables, respectively:
out1 = s:fetch{table={name='out1', orderby='ID' }, to=5}
out2 = s:fetch{table={name='out2', orderby='ID' }, to=5}
For details about the results of this analysis, see the CASL version of this example.
Two-Component Gaussian Mixture
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 mbc action set, uses the mbcFit action to find the best-fitting model for these data from a list of several different models, stores that model, and writes posterior weights for each observation to an output data table.
The table parameter names the input data table to analyze. The effects subparameter in the model parameter defines the variables to analyze. The nClusters parameter fits models that have two to four Gaussian components, the noise parameter fits models without a noise component, and the covStruct parameter fits models that have three covariance structures. The output parameter scores the observations in the getStarted table, writing the cluster association weights for each observation to the Out1 data table. The variables that contain the cluster association weights have the "WT" prefix in the Out1 table. The copyVars subparameter in the output parameter includes the analysis variables Duration and Wait and the identifying variable ID in the output data table. The store parameter stores the model in a special data table named myModel. Finally, the seed parameter specifies the random seed for initial clusterings.
s.loadActionSet(actionSet='mbc')
m = s.mbcFit(model={'effects':[{'vars':['duration']},{'vars':['wait']}]},
covstruct=('VII','EII','VVV'),
nclusters=(2,3,4),
noise='N',
table={'name':'getStarted'},
output={'casOut':{'name':'out1', 'replace':'true'},
'nextClus':'WT',
'copyVars':['duration','wait','ID']},
store={'name':'myModel','replace':'true'},
seed=9872)
The following commands apply the stored model myModel to the same data by using the mbcScore action and write posterior weights for each observation to a separate output data table, Out2:
m = s.mbcScore(table={'name':'getStarted'},
restore={'name':'myModel'},
casOut={'name':'out2','replace':'true'},
nextClus='WT',
copyVars=['Duration','Wait','ID'])
The following statements display the first five observations in the Out1 and Out2 data tables, respectively:
out1 = s.fetch(table={'name':'out1', 'orderby':'ID' }, to='5')
out2 = s.fetch(table={'name':'out2', 'orderby':'ID' }, to='5')
For details about the results of this analysis, see the CASL version of this example.
Two-Component Gaussian Mixture
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 mbc action set, uses the mbcFit action to find the best-fitting model for these data from a list of several different models, stores that model, and writes posterior weights for each observation to an output data table. The table parameter names the input data table to analyze. The effects subparameter in the model parameter defines the variables to analyze. The nClusters parameter fits models that have two to four Gaussian components, the noise parameter fits models without a noise component, and the covStruct parameter fits models that have three covariance structures. The output parameter scores the observations in the getStarted table, writing the cluster association weights for each observation to the Out1 data table. The variables that contain the cluster association weights have the "WT" prefix in the Out1 table. The copyVars subparameter in the output parameter includes the analysis variables Duration and Wait and the identifying variable ID in the output data table. The store parameter stores the model in a special data table named myModel. Finally, the seed parameter specifies the random seed for initial clusterings.
m <- cas.builtins.loadActionSet(s, actionset='mbc')
m <- cas.mbc.mbcFit(s,
table='getStarted',
model=list(effects=list('Duration','Wait')),
nClusters=list(2, 3, 4),
noise='N',
covStruct=list('VII', 'EII', 'VVV'),
output=list(casOut=list(name='out1'),
nextClus='WT',
copyVars=list('Duration','Wait','ID')),
store=list(name='myModel'),
seed=9872)
The following commands apply the stored model myModel to the same data by using the mbcScore action and write posterior weights for each observation to a separate output data table, Out2:
m <- cas.mbc.mbcScore(s,
table='getStarted',
restore=list(name='myModel'),
casOut=list(name='out2'),
nextClus='WT',
copyVars=list('Duration','Wait','ID'))
The following statements 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')