Bayesian Additive Regression Trees Action Set
Fitting a Probit Model to Binary Data Using the bartProbit Action
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 junk email data available in the Sashelp.JunkMail data set. The data set comes from a study that classifies whether an email is junk email (coded as 1) or not (coded as 0). The data were collected by Hewlett-Packard Labs and donated by George Forman. The data table contains 4,601 observations, with two binary variables and 57 continuous explanatory variables. The response variable, Class, is a binary indicator of whether an email is considered spam or not. The partitioning variable, Test, is a binary indicator that is used to divide the data into training and testing parts. The 57 explanatory variables are continuous variables that represent the frequencies of some common words and characters and the lengths of uninterrupted sequences of capital letters in emails.
You can load the Sashelp.JunkMail data set into your CAS session by specifying your CAS engine libref in the second statement in the following DATA step:
data mycas.JunkMail;
set Sashelp.JunkMail;
run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following statements use the bartProbit action in the bart action set to fit a Bayesian additive regression trees (BART) model to these data. The table parameter names the input data table to be analyzed. You can use one of two mutually exclusive modes to specify the variables in the model by using the bartProbit action: in one mode, you use the target, inputs, and nominals parameters; in the other mode, you use the class and model parameters. This example uses the first mode. The target parameter names the target (response) variable, and the inputs parameter lists the explanatory variables. You must also list the target variable in the nominals parameter along with any categorical predictors. The partByVar parameter divides the data into two parts: the training data have a Test value of 0 and contain about two-thirds of the data, and the rest of the data are used to evaluate the fit.
proc cas;
bart.bartProbit /
inputs={"Make", "Address", "All", "_3d", "Our", "Over", "Remove",
"Internet",
"Order", "Mail", "Receive", "Will", "People", "Report", "Addresses",
"Free", "Business", "Email", "You", "Credit", "Your", "Font",
"_000", "Money", "HP", "HPL", "George", "_650", "Lab", "Labs",
"Telnet", "_857", "Data", "_415", "_85", "Technology", "_1999",
"Parts", "PM", "Direct", "Conference", "Semicolon", "Paren",
"Bracket", "Exclamation", "Dollar", "Pound", "CapAvg", "CapLong",
"CapTotal", "CS", "Meeting", "Original", "Project", "RE", "Edu",
"Table"},
nominals="Class",
partByVar={name="Test", test="1", train="0"},
seed="210124",
table="junkmail",
target="Class";
run;
The "Number of Observations" and "Response Profile" tables in Output 4.2.1 are divided into training and testing columns.
Output 4.2.1: Partitioned Counts
The fit statistics for the selected model are displayed in the "Fit Statistics" table in Output 4.2.2. The misclassification rate and average square error are similar for the training and testing data, indicating that the fitted model generalizes well.
Output 4.2.2: Partitioned Fit Statistics
| Fit Statistics | ||
|---|---|---|
| Training | Testing | |
| Average Square Error | 0.04038 | 0.04709 |
| Misclassification Rate | 0.04829 | 0.06120 |
To enable you to further assess the performance of the model, the "Classification Matrix" table in Output 4.2.3 displays information about the observed and predicted values for both the training and test data.
Output 4.2.3: Partitioned Classification Matrix
| Classification Matrix | ||||||
|---|---|---|---|---|---|---|
| Observed | Training Prediction | Testing Prediction | ||||
| 0 | 1 | Total | 0 | 1 | Total | |
| 0 | 1789 | 58 | 1847 | 900 | 41 | 941 |
| 1 | 90 | 1128 | 1218 | 53 | 542 | 595 |
| Total | 1879 | 1186 | 3065 | 953 | 583 | 1536 |
Fitting a Probit Model to Binary Data Using the bartProbit Action
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
This example uses the junk email data available in the Sashelp.JunkMail data set. The data set comes from a study that classifies whether an email is junk email (coded as 1) or not (coded as 0). The data were collected by Hewlett-Packard Labs and donated by George Forman. The data table contains 4,601 observations, with two binary variables and 57 continuous explanatory variables. The response variable, Class, is a binary indicator of whether an email is considered spam or not. The partitioning variable, Test, is a binary indicator that is used to divide the data into training and testing parts. The 57 explanatory variables are continuous variables that represent the frequencies of some common words and characters and the lengths of uninterrupted sequences of capital letters in emails.
To run this example code, you must make the Sashelp.Junkmail data set accessible to the CAS server. One way to do this is to convert the Sashelp.Junkmail data set to a comma-separated-value (CSV) file and then to load the CSV file into CAS.
The following statements use the bartProbit action in the bart action set to fit a Bayesian additive regression trees (BART) model to these data. The table parameter names the input data table to be analyzed. You can use one of two mutually exclusive modes to specify the variables in the model by using the bartProbit action: in one mode, you use the target, inputs, and nominals parameters; in the other mode, you use the class and model parameters. This example uses the first mode. The target parameter names the target (response) variable, and the inputs parameter lists the explanatory variables. You must also list the target variable in the nominals parameter along with any categorical predictors. The partByVar parameter divides the data into two parts: the training data have a Test value of 0 and contain about two-thirds of the data, and the rest of the data are used to evaluate the fit.
s:loadactionset{actionset="bart"}
s:bart_bartProbit{
table='junkmail',
target='Class',
nominals='Class',
partByVar = { name='Test', test='1', train='0'},
inputs={'Make', 'Address', 'All','_3d', 'Our', 'Over',
'Remove', 'Internet', 'Order', 'Mail', 'Receive', 'Will',
'People', 'Report', 'Addresses', 'Free', 'Business', 'Email',
'You', 'Credit', 'Your', 'Font', '_000', 'Money', 'HP', 'HPL',
'George', '_650', 'Lab', 'Labs', 'Telnet', '_857', 'Data',
'_415', '_85', 'Technology', '_1999', 'Parts', 'PM', 'Direct',
'Conference', 'Semicolon', 'Paren', 'Bracket', 'Exclamation',
'Dollar', 'Pound', 'CapAvg', 'CapLong', 'CapTotal', 'CS',
'Meeting', 'Original', 'Project', 'RE', 'Edu', 'Table'
},
seed=210124
}
The "Number of Observations" and "Response Profile" tables in Output 4.2.4 are divided into training and testing columns.
Output 4.2.4: Partitioned Counts
| RowId | Description | Value |
|---|---|---|
| NREAD | Number of Observations Read | 4601 |
| NUSED | Number of Observations Used | 4601 |
| NTRAINUSED | Number of Observations Used for Training | 3065 |
| NTESTUSED | Number of Observations Used for Testing | 1536 |
| Outcome | Class | Training | Testing |
|---|---|---|---|
| 1 | 1 | 1218 | 595 |
| 0 | 0 | 1847 | 941 |
The fit statistics for the selected model are displayed in the "Fit Statistics" table in Output 4.2.5. The misclassification rate and average square error are similar for the training and testing data, indicating that the fitted model generalizes well.
Output 4.2.5: Partitioned Fit Statistics
| RowId | Description | Training | Testing |
|---|---|---|---|
| ASE | Average Square Error | 0.04038 | 0.04709 |
| MISCLASS | Misclassification Rate | 0.04829 | 0.06120 |
To enable you to further assess the performance of the model, the "Classification Matrix" table in Output 4.2.6 displays information about the observed and predicted values for both the training and test data.
Output 4.2.6: Partitioned Classification Matrix
| Observed | PredEventTrain | PredNonEventTrain | TotalTrain | PredEventTest | PredNonEventTest | TotalTest |
|---|---|---|---|---|---|---|
| 0 | 1789 | 58 | 1847 | 900 | 41 | 941 |
| 1 | 90 | 1128 | 1218 | 53 | 542 | 595 |
| Total | 1879 | 1186 | 3065 | 953 | 583 | 1536 |
Fitting a Probit Model to Binary Data Using the bartProbit Action
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
This example uses the junk email data available in the Sashelp.JunkMail data set. The data set comes from a study that classifies whether an email is junk email (coded as 1) or not (coded as 0). The data were collected by Hewlett-Packard Labs and donated by George Forman. The data table contains 4,601 observations, with two binary variables and 57 continuous explanatory variables. The response variable, Class, is a binary indicator of whether an email is considered spam or not. The partitioning variable, Test, is a binary indicator that is used to divide the data into training and testing parts. The 57 explanatory variables are continuous variables that represent the frequencies of some common words and characters and the lengths of uninterrupted sequences of capital letters in emails.
To run this example code, you must make the Sashelp.Junkmail data set accessible to the CAS server. One way to do this is to convert the Sashelp.Junkmail data set to a comma-separated-value (CSV) file and then to load the CSV file into CAS.
The following statements use the bartProbit action in the bart action set to fit a Bayesian additive regression trees (BART) model to these data. The table parameter names the input data table to be analyzed. You can use one of two mutually exclusive modes to specify the variables in the model by using the bartProbit action: in one mode, you use the target, inputs, and nominals parameters; in the other mode, you use the class and model parameters. This example uses the first mode. The target parameter names the target (response) variable, and the inputs parameter lists the explanatory variables. You must also list the target variable in the nominals parameter along with any categorical predictors. The partByVar parameter divides the data into two parts: the training data have a Test value of 0 and contain about two-thirds of the data, and the rest of the data are used to evaluate the fit.
s.loadactionset(actionset='bart')
s.bart.bartprobit(
inputs=['Make', 'Address', 'All', '_3d', 'Our', 'Over', 'Remove',
'Internet',
'Order', 'Mail', 'Receive', 'Will', 'People', 'Report', 'Addresses',
'Free', 'Business', 'Email', 'You', 'Credit', 'Your', 'Font',
'_000', 'Money', 'HP', 'HPL', 'George', '_650', 'Lab', 'Labs',
'Telnet', '_857', 'Data', '_415', '_85', 'Technology', '_1999',
'Parts', 'PM', 'Direct', 'Conference', 'Semicolon', 'Paren',
'Bracket', 'Exclamation', 'Dollar', 'Pound', 'CapAvg', 'CapLong',
'CapTotal', 'CS', 'Meeting', 'Original', 'Project', 'RE', 'Edu',
'Table'],
nominals='Class',
partByVar=dict(name='Test', test='1', train='0'),
seed='210124',
table='junkmail',
target='Class')
The "Number of Observations" and "Response Profile" tables in Output 4.2.7 are divided into training and testing columns.
Output 4.2.7: Partitioned Counts
| RowId | Description | Value |
|---|---|---|
| NREAD | Number of Observations Read | 4601 |
| NUSED | Number of Observations Used | 4601 |
| NTRAINUSED | Number of Observations Used for Training | 3065 |
| NTESTUSED | Number of Observations Used for Testing | 1536 |
| Outcome | Class | Training | Testing |
|---|---|---|---|
| 1 | 1 | 1218 | 595 |
| 0 | 0 | 1847 | 941 |
The fit statistics for the selected model are displayed in the "Fit Statistics" table in Output 4.2.8. The misclassification rate and average square error are similar for the training and testing data, indicating that the fitted model generalizes well.
Output 4.2.8: Partitioned Fit Statistics
| RowId | Description | Training | Testing |
|---|---|---|---|
| ASE | Average Square Error | 0.04038 | 0.04709 |
| MISCLASS | Misclassification Rate | 0.04829 | 0.06120 |
To enable you to further assess the performance of the model, the "Classification Matrix" table in Output 4.2.9 displays information about the observed and predicted values for both the training and test data.
Output 4.2.9: Partitioned Classification Matrix
| Observed | PredEventTrain | PredNonEventTrain | TotalTrain | PredEventTest | PredNonEventTest | TotalTest |
|---|---|---|---|---|---|---|
| 0 | 1789 | 58 | 1847 | 900 | 41 | 941 |
| 1 | 90 | 1128 | 1218 | 53 | 542 | 595 |
| Total | 1879 | 1186 | 3065 | 953 | 583 | 1536 |
Fitting a Probit Model to Binary Data Using the bartProbit Action
This section contains R code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
This example uses the junk email data available in the Sashelp.JunkMail data set. The data set comes from a study that classifies whether an email is junk email (coded as 1) or not (coded as 0). The data were collected by Hewlett-Packard Labs and donated by George Forman. The data table contains 4,601 observations, with two binary variables and 57 continuous explanatory variables. The response variable, Class, is a binary indicator of whether an email is considered spam or not. The partitioning variable, Test, is a binary indicator that is used to divide the data into training and testing parts. The 57 explanatory variables are continuous variables that represent the frequencies of some common words and characters and the lengths of uninterrupted sequences of capital letters in emails.
To run this example code, you must make the Sashelp.Junkmail data set accessible to the CAS server. One way to do this is to convert the Sashelp.Junkmail data set to a comma-separated-value (CSV) file and then to load the CSV file into CAS.
The following statements use the bartProbit action in the bart action set to fit a Bayesian additive regression trees (BART) model to these data. The table parameter names the input data table to be analyzed. You can use one of two mutually exclusive modes to specify the variables in the model by using the bartProbit action: in one mode, you use the target, inputs, and nominals parameters; in the other mode, you use the class and model parameters. This example uses the first mode. The target parameter names the target (response) variable, and the inputs parameter lists the explanatory variables. You must also list the target variable in the nominals parameter along with any categorical predictors. The partByVar parameter divides the data into two parts: the training data have a Test value of 0 and contain about two-thirds of the data, and the rest of the data are used to evaluate the fit.
m <- cas.builtins.loadActionSet(s, actionset='bart')
cas.bart.bartProbit(s,
inputs=c('Make', 'Address', 'All', '_3d', 'Our', 'Over', 'Remove',
'Internet', 'Order', 'Mail', 'Receive', 'Will', 'People', 'Report',
'Addresses', 'Free', 'Business', 'Email', 'You', 'Credit', 'Your',
'Font', '_000', 'Money', 'HP', 'HPL', 'George', '_650', 'Lab',
'Labs', 'Telnet', '_857', 'Data', '_415', '_85', 'Technology',
'_1999', 'Parts', 'PM', 'Direct', 'Conference', 'Semicolon',
'Paren', 'Bracket', 'Exclamation', 'Dollar', 'Pound', 'CapAvg',
'CapLong', 'CapTotal', 'CS', 'Meeting', 'Original', 'Project',
'RE', 'Edu', 'Table'),
nominals='Class',
partByVar=list(name='Test', test='1', train='0'),
seed='210124',
table='junkmail',
target='Class')
The "Number of Observations" and "Response Profile" tables in Output 4.2.10 are divided into training and testing columns.
Output 4.2.10: Partitioned Counts
| RowId | Description | Value |
|---|---|---|
| NREAD | Number of Observations Read | 4601 |
| NUSED | Number of Observations Used | 4601 |
| NTRAINUSED | Number of Observations Used for Training | 3065 |
| NTESTUSED | Number of Observations Used for Testing | 1536 |
| Outcome | Class | Training | Testing |
|---|---|---|---|
| 1 | 1 | 1218 | 595 |
| 0 | 0 | 1847 | 941 |
The fit statistics for the selected model are displayed in the "Fit Statistics" table in Output 4.2.11. The misclassification rate and average square error are similar for the training and testing data, indicating that the fitted model generalizes well.
Output 4.2.11: Partitioned Fit Statistics
| RowId | Description | Training | Testing |
|---|---|---|---|
| ASE | Average Square Error | 0.04038 | 0.04709 |
| MISCLASS | Misclassification Rate | 0.04829 | 0.06120 |
To enable you to further assess the performance of the model, the "Classification Matrix" table in Output 4.2.12 displays information about the observed and predicted values for both the training and test data.
Output 4.2.12: Partitioned Classification Matrix
| Observed | PredEventTrain | PredNonEventTrain | TotalTrain | PredEventTest | PredNonEventTest | TotalTest |
|---|---|---|---|---|---|---|
| 0 | 1789 | 58 | 1847 | 900 | 41 | 941 |
| 1 | 90 | 1128 | 1218 | 53 | 542 | 595 |
| Total | 1879 | 1186 | 3065 | 953 | 583 | 1536 |