FOREST Procedure
Getting Started: FOREST Procedure
Note: Input data must be in a CAS table that is accessible in your CAS session. You must refer to this table by using a two-level name. The first level must be a CAS engine libref, and the second level must be the table name. For more information, see the sections Using CAS Sessions and CAS Engine Librefs and Loading a SAS Data Set onto a CAS Server in Chapter 2, Shared Concepts.
A common use of forest models is to predict whether a mortgage applicant will default on a loan. The home equity data table Hmeq, which is in the Sampsio library that SAS provides, contains observations for 5,960 mortgage applicants. A variable named Bad indicates whether the applicant, after being approved for a loan, paid off or defaulted on the loan.
This example uses the Hmeq data table to build a forest model that is used to score the data and can be used to score data about new loan applicants. Table 1 describes the variables in Hmeq.
Table 1: Variables in the Home Equity (Hmeq) Data Table
| Variable | Role | Level | Description |
|---|---|---|---|
Bad | Response | Binary | 1 = applicant defaulted on the loan or is seriously delinquent |
| 0 = applicant paid off the loan | |||
CLAge | Predictor | Interval | Age of oldest credit line in months |
CLNo | Predictor | Interval | Number of credit lines |
DebtInc | Predictor | Interval | Debt-to-income ratio |
Delinq | Predictor | Interval | Number of delinquent credit lines |
Derog | Predictor | Interval | Number of major derogatory reports |
Job | Predictor | Nominal | Occupational category |
Loan | Predictor | Interval | Requested loan amount |
MortDue | Predictor | Interval | Amount due on mortgage |
nInq | Predictor | Interval | Number of recent credit inquiries |
Reason | Predictor | Binary | DebtCon = debt consolidation |
HomeImp = home improvement | |||
Value | Predictor | Interval | Value of property |
YoJ | Predictor | Interval | Years at present job |
The following statements load the mylib.hmeq data into your CAS session by specifying your CAS engine libref in the first statement. These statements assume that your CAS engine libref is named mylib, as in the section Using CAS Sessions and CAS Engine Librefs, but you can substitute any appropriately defined CAS engine libref.
/* Convert variable names to mixed case */
data mylib.hmeq;
length Bad Loan MortDue Value 8 Reason Job $7
YoJ Derog Delinq CLAge nInq CLNo DebtInc 8;
set sampsio.hmeq;
run;
proc print data=mylib.hmeq(obs=10); run;
Figure 1 shows the first 10 observations of mylib.hmeq.
Figure 1: Partial Listing of the mylib.hmeq Data
| Obs | Bad | Loan | MortDue | Value | Reason | Job | YoJ | Derog | Delinq | CLAge | nInq | CLNo | DebtInc |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 1100 | 25860 | 39025 | HomeImp | Other | 10.5 | 0 | 0 | 94.367 | 1 | 9 | . |
| 2 | 1 | 1500 | . | . | . | . | . | . | . | . | . | ||
| 3 | 1 | 1800 | 48649 | 57037 | HomeImp | Other | 5.0 | 3 | 2 | 77.100 | 1 | 17 | . |
| 4 | 1 | 2000 | . | 62250 | HomeImp | Sales | 16.0 | 0 | 0 | 115.800 | 0 | 13 | . |
| 5 | 1 | 2000 | 45000 | 55000 | HomeImp | Other | 3.0 | 0 | 0 | 86.067 | 2 | 25 | . |
| 6 | 1 | 2200 | 24280 | 34687 | HomeImp | Other | . | 0 | 1 | 300.867 | 0 | 8 | . |
| 7 | 1 | 2300 | 28192 | 40150 | HomeImp | Other | 4.5 | 0 | 0 | 54.600 | 1 | 16 | . |
| 8 | 1 | 2400 | 50000 | 73395 | HomeImp | ProfExe | 5.0 | 1 | 0 | . | 1 | 0 | . |
| 9 | 1 | 2400 | . | 17180 | HomeImp | Other | . | 0 | 0 | 14.567 | 3 | 4 | . |
| 10 | 1 | 2500 | 15000 | 20200 | HomeImp | 18.0 | 0 | 0 | 136.067 | 1 | 19 | . |
PROC FOREST treats numeric variables as interval inputs unless you specify otherwise. Character variables are always treated as nominal inputs. The following statements run PROC FOREST and save the model in a table named mylib.savedModel:
proc forest data=mylib.hmeq outmodel=mylib.savedModel seed=12345;
input Delinq Derog Job nInq Reason / level = nominal;
input CLAge CLNo DebtInc Loan Mortdue Value YoJ / level = interval;
target Bad / level = nominal;
ods output FitStatistics=fitstats;
run;
No parameters are specified in the PROC FOREST statement; therefore, the procedure uses all default values. For example, the number of trees in the forest is 100, the number of bins for interval input variables is 20, and the number of variables that are examined at each node for a split is the square root of the number of input variables.
The INPUT and TARGET statements are required in order to run PROC FOREST. The INPUT statement indicates which variables to use to build the model, and the TARGET statement indicates which variable the procedure predicts.
Figure 2 displays the "Model Information" table. This table shows the values of the training parameters in the first six rows, in addition to some basic information about the trees in the forest.
Figure 2: Model Information
| Model Information | |
|---|---|
| Number of Trees | 100 |
| Number of Variables Per Split | 4 |
| Seed | 12345 |
| Bootstrap Percentage | 60 |
| Number of Bins | 50 |
| Number of Input Variables | 12 |
| Maximum Number of Tree Nodes | 329 |
| Minimum Number of Tree Nodes | 143 |
| Maximum Number of Branches | 2 |
| Minimum Number of Branches | 2 |
| Maximum Depth | 20 |
| Minimum Depth | 20 |
| Maximum Number of Leaves | 165 |
| Minimum Number of Leaves | 72 |
| Maximum Leaf Size | 2282 |
| Minimum Leaf Size | 5 |
| OOB Misclassification Rate | 0.09765101 |
| Average Number of Leaves | 122.33 |
Figure 3 displays the "Number of Observations" table, which shows how many observations were read and used. If you specify a PARTITION statement, the "Number of Observations" table also displays the number of observations that were read and used per partition.
Figure 3: Number of Observations
| Training | |
|---|---|
| Number of Observations Read | 5960 |
| Number of Observations Used | 5960 |
Figure 4 displays the estimates of variable importance. The rows in this figure are sorted by the importance measure. A conclusion from fitting the forest model to these data is that DebtInc is the most important predictor of loan default.
Figure 4: Variable Importance
| Variable Importance | |||
|---|---|---|---|
| Variable | Importance | Std Dev Importance | Relative Importance |
| DebtInc | 384.10 | 63.0376 | 1.0000 |
| Value | 72.7549 | 25.4882 | 0.1894 |
| Delinq | 58.6063 | 18.1454 | 0.1526 |
| CLAge | 46.9144 | 12.0956 | 0.1221 |
| Derog | 39.7649 | 15.5401 | 0.1035 |
| CLNo | 31.2729 | 8.6660 | 0.0814 |
| Loan | 30.9741 | 12.1380 | 0.0806 |
| YoJ | 27.4476 | 10.7459 | 0.0715 |
| Job | 26.8259 | 9.2556 | 0.0698 |
| nInq | 26.3861 | 9.3798 | 0.0687 |
| MortDue | 26.1040 | 9.8348 | 0.0680 |
| Reason | 4.9037 | 4.3171 | 0.0128 |
Figure 5 shows the first 10 and last 10 observations of the fit statistics. When PROC FOREST runs, it computes fit statistics for a sequence of forests that have an increasing number of trees. As the number of trees increases, the fit statistics usually improve (decrease) at first and then level off and fluctuate within a small range. Forest models provide an alternative estimate of the average square error and misclassification rate. This alternative is called the out-of-bag (OOB) estimate. The OOB estimate is a convenient substitute for an estimate that is based on test data and is a less biased estimate of how the model will perform on future data. For more information, see the section Bagging the Data. The listing shows that the out-of-bag error estimate is worse (larger) than the estimate that evaluates all observations on all trees. This is common.
Figure 5: Fit Statistics
| Fit Statistics | ||||||
|---|---|---|---|---|---|---|
| Number of Trees | OOB Average Square Error | Training Average Square Error | OOB Misclassification Rate | Training Misclassification Rate | OOB Log Loss | Training Log Loss |
| 1 | 0.1059 | 0.0788 | 0.1347 | 0.1037 | 1.301 | 0.777 |
| 2 | 0.0979 | 0.0669 | 0.1266 | 0.0871 | 0.915 | 0.290 |
| 3 | 0.0942 | 0.0627 | 0.1234 | 0.0802 | 0.707 | 0.226 |
| 4 | 0.0909 | 0.0615 | 0.1197 | 0.0817 | 0.586 | 0.212 |
| 5 | 0.0867 | 0.0595 | 0.1175 | 0.0760 | 0.452 | 0.207 |
| 6 | 0.0840 | 0.0583 | 0.1133 | 0.0768 | 0.403 | 0.203 |
| 7 | 0.0825 | 0.0572 | 0.1120 | 0.0743 | 0.354 | 0.200 |
| 8 | 0.0808 | 0.0572 | 0.1098 | 0.0730 | 0.326 | 0.200 |
| 9 | 0.0801 | 0.0569 | 0.1081 | 0.0732 | 0.307 | 0.199 |
| 10 | 0.0793 | 0.0568 | 0.1079 | 0.0750 | 0.297 | 0.198 |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . |
| 91 | 0.0736 | 0.0550 | 0.0980 | 0.0742 | 0.249 | 0.193 |
| 92 | 0.0736 | 0.0549 | 0.0982 | 0.0740 | 0.249 | 0.193 |
| 93 | 0.0736 | 0.0549 | 0.0987 | 0.0740 | 0.249 | 0.193 |
| 94 | 0.0736 | 0.0550 | 0.0983 | 0.0738 | 0.249 | 0.193 |
| 95 | 0.0737 | 0.0550 | 0.0978 | 0.0747 | 0.249 | 0.193 |
| 96 | 0.0737 | 0.0550 | 0.0975 | 0.0745 | 0.249 | 0.193 |
| 97 | 0.0737 | 0.0550 | 0.0978 | 0.0743 | 0.249 | 0.194 |
| 98 | 0.0737 | 0.0550 | 0.0968 | 0.0738 | 0.249 | 0.194 |
| 99 | 0.0737 | 0.0550 | 0.0971 | 0.0737 | 0.249 | 0.193 |
| 100 | 0.0737 | 0.0550 | 0.0977 | 0.0737 | 0.249 | 0.193 |