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 13.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
1.950 51 4.350 85 1.833 54 3.917 84 4.200 78 1.750 47 4.700 83 2.167 52
1.750 62 4.800 84 1.600 52 4.250 79 1.800 51 1.750 47 3.450 78 3.067 69
4.533 74 3.600 83 1.967 55 4.083 76 3.850 78 4.433 79 4.300 73 4.467 77
3.367 66 4.033 80 3.833 74 2.017 52 1.867 48 4.833 80 1.833 59 4.783 90
4.350 80 1.883 58 4.567 84 1.750 58 4.533 73 3.317 83 3.833 64 2.100 53
4.633 82 2.000 59 4.800 75 4.716 90 1.833 54 4.833 80 1.733 54 4.883 83
3.717 71 1.667 64 4.567 77 4.317 81 2.233 59 4.500 84 1.750 48 4.800 82
1.817 60 4.400 92 4.167 78 4.700 78 2.067 65 4.700 73 4.033 82 1.967 56
4.500 79 4.000 71 1.983 62 5.067 76 2.017 60 4.567 78 3.883 76 3.600 83
4.133 75 4.333 82 4.100 70 2.633 65 4.067 73 4.933 88 3.950 76 4.517 80
2.167 48 4.000 86 2.200 60 4.333 90 1.867 50 4.817 78 1.833 63 4.300 72
4.667 84 3.750 75 1.867 51 4.900 82 2.483 62 4.367 88 2.100 49 4.500 83
4.050 81 1.867 47 4.700 84 1.783 52 4.850 86 3.683 81 4.733 75 2.300 59
4.900 89 4.417 79 1.700 59 4.633 81 2.317 50 4.600 85 1.817 59 4.417 87
2.617 53 4.067 69 4.250 77 1.967 56 4.600 88 3.767 81 1.917 45 4.500 82
2.267 55 4.650 90 1.867 45 4.167 83 2.800 56 4.333 89 1.833 46 4.383 82
1.883 51 4.933 86 2.033 53 3.733 79 4.233 81 2.233 60 4.533 82 4.817 77
4.333 76 1.983 59 4.633 80 2.017 49 5.100 96 1.800 53 5.033 77 4.000 77
2.400 65 4.600 81 3.567 71 4.000 70 4.500 81 4.083 93 1.800 53 3.967 89
2.200 45 4.150 86 2.000 58 3.833 78 3.500 66 4.583 76 2.367 63 5.000 88
1.933 52 4.617 93 1.917 49 2.083 57 4.583 77 3.333 68 4.167 81 4.333 81
4.500 73 2.417 50 4.000 85 4.167 74 1.883 55 4.583 77 4.250 83 3.767 83
2.033 51 4.433 78 4.083 84 1.833 46 4.417 83 2.183 55 4.800 81 1.833 57
4.800 76 4.100 84 3.966 77 4.233 81 3.500 87 4.366 77 2.250 51 4.667 78
2.100 60 4.350 82 4.133 91 1.867 53 4.600 78 1.783 46 4.367 77 3.850 84
1.933 49 4.500 83 2.383 71 4.700 80 1.867 49 3.833 75 3.417 64 4.233 76
2.400 53 4.800 94 2.000 55 4.150 76 1.867 50 4.267 82 1.750 54 4.483 75
4.000 78 4.117 79 4.083 78 4.267 78 3.917 70 4.550 79 4.083 70 2.417 54
4.183 86 2.217 50 4.450 90 1.883 54 1.850 54 4.283 77 3.950 79 2.333 64
4.150 75 2.350 47 4.933 86 2.900 63 4.583 85 3.833 82 2.083 57 4.367 82
2.133 67 4.350 74 2.200 54 4.450 83 3.567 73 4.500 73 4.150 88 3.817 80
3.917 71 4.450 83 2.000 56 4.283 79 4.767 78 4.533 84 1.850 58 4.250 83
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 13.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 13.2.1 and Output 13.2.2.
Output 13.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 13.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')