Quantile Regression Modeling Action Set
Salary Data for Baseball Players
This section contains PROC CAS code.
Note: Input data must be in a CAS table that is accessible in your CAS session. This 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 PROC CAS, see SAS Cloud Analytic Services: CAS Procedure Programming Guide and Reference. For more information about two-level names, see Chapter 2: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.1: Statistical Procedures.
The following example is modeled on the example in the section "Getting Started: QUANTSELECT Procedure" in the
SAS/STAT User's Guide. The Sashelp.baseball data set contains salary and performance information for Major League Baseball (MLB) players, excluding pitchers, who played in at least one game in both the 1986 and 1987 seasons. The salaries (Time Inc. 1987) are for the 1987 season, and the performance measures are for the 1986 season (Reichler 1987).
The following statements display the variables in the data set. Output 1.1 shows the results.
proc contents varnum data=sashelp.baseball; ods select position; run;
Output 1.1: Sashelp.Baseball Data Set
| Variables in Creation Order | ||||
|---|---|---|---|---|
| # | Variable | Type | Len | Label |
| 1 | Name | Char | 18 | Player's Name |
| 2 | Team | Char | 14 | Team at the End of 1986 |
| 3 | nAtBat | Num | 8 | Times at Bat in 1986 |
| 4 | nHits | Num | 8 | Hits in 1986 |
| 5 | nHome | Num | 8 | Home Runs in 1986 |
| 6 | nRuns | Num | 8 | Runs in 1986 |
| 7 | nRBI | Num | 8 | RBIs in 1986 |
| 8 | nBB | Num | 8 | Walks in 1986 |
| 9 | YrMajor | Num | 8 | Years in the Major Leagues |
| 10 | CrAtBat | Num | 8 | Career Times at Bat |
| 11 | CrHits | Num | 8 | Career Hits |
| 12 | CrHome | Num | 8 | Career Home Runs |
| 13 | CrRuns | Num | 8 | Career Runs |
| 14 | CrRbi | Num | 8 | Career RBIs |
| 15 | CrBB | Num | 8 | Career Walks |
| 16 | League | Char | 8 | League at the End of 1986 |
| 17 | Division | Char | 8 | Division at the End of 1986 |
| 18 | Position | Char | 8 | Position(s) in 1986 |
| 19 | nOuts | Num | 8 | Put Outs in 1986 |
| 20 | nAssts | Num | 8 | Assists in 1986 |
| 21 | nError | Num | 8 | Errors in 1986 |
| 22 | Salary | Num | 8 | 1987 Salary in $ Thousands |
| 23 | Div | Char | 16 | League and Division |
| 24 | logSalary | Num | 8 | Log Salary |
You can load the Sashelp.Baseball data set into your CAS session by naming your CAS engine libref in the first statement of the following DATA step:
data mycas.baseball; set sashelp.baseball; run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
Suppose you want to investigate how the MLB players’ salaries for the 1987 season depend on performance measures for the players’ previous season and MLB career. You might worry that some players who are outliers could dominate your least squares analysis. To address this concern, you can use the following statements to obtain a median regression model, which is equivalent to the 50th conditional percentile (the quantile regression model at quantile level 0.5):
proc cas;
action quantreg.quantreg/
table={name='baseball'},
class={'league','division'},
model={depvars='Salary',
effects={'nAtBat', 'nHits', 'nHome', 'nRuns',
'nRBI', 'nBB', 'yrMajor', 'crAtBat',
'crHits', 'crHome', 'crRuns', 'crRbi',
'crBB', 'league', 'division', 'nOuts',
'nAssts', 'nError'}};
run;
If you do not use the selection parameter, the quantreg action fits the full model that is specified by the model parameter without any effect selection.
Output 1.2 displays the data source and the response variable.
Output 1.2: Model Information
Output 1.3 displays information about the number of observations, class levels, and dimensions.
Output 1.3: Number of Observations, Class Level Information, and Dimensions Tables
The "Number of Observations" table in Output 1.3 shows that, of the 322 observations, the quantreg action uses only 263 observations for model fitting (and ignores 59 incomplete observations).
The "Class Level Information" table in Output 1.3 shows level information for two classification effects that the class parameter identifies: League and Division. League has two levels: American and National. Division also has two levels: East and West.
The "Dimensions" table in Output 1.3 shows that the model parameter identifies 19 effects for model fitting besides the intercept effect. Because the 19 effects include two classification effects and each level of a classification effect corresponds to a parameter, the 19 effects contain a total of 21 parameters.
The "Fit Statistics" table in Output 1.4 shows the values of model fitting criteria for the fitted median model. For more information about model fitting criteria for quantile regression, see the section Details.
Output 1.4: Fit Statistics
The "Parameter Estimates" table in Output 1.5 shows the parameter estimates of the fitted median model. Among the 19 effective parameters whose degrees of freedom are not zero, the fitted model contains 13 insignificant parameters whose 95% confidence intervals cover zeros. Because more than half of the 19 effective parameters are insignificant, you might worry that the model is overfitted.
Output 1.5: Parameter Estimates
| Parameter Estimates | |||||
|---|---|---|---|---|---|
| Parameter | DF | Estimate | Standard Error | t Value | Pr > |t| |
| Intercept | 1 | -67.75322 | 39.95908 | -1.70 | 0.0912 |
| nAtBat | 1 | -1.57112 | 0.44700 | -3.51 | 0.0005 |
| nHits | 1 | 8.82192 | 1.94990 | 4.52 | <.0001 |
| nHome | 1 | -5.91757 | 4.91015 | -1.21 | 0.2293 |
| nRuns | 1 | -5.17076 | 2.14914 | -2.41 | 0.0169 |
| nRBI | 1 | 0.77547 | 2.15469 | 0.36 | 0.7192 |
| nBB | 1 | 5.28866 | 1.67603 | 3.16 | 0.0018 |
| YrMajor | 1 | 6.61877 | 6.61798 | 1.00 | 0.3182 |
| CrAtBat | 1 | -0.04463 | 0.15485 | -0.29 | 0.7734 |
| CrHits | 1 | 0.07896 | 0.73594 | 0.11 | 0.9146 |
| CrHome | 1 | 3.78231 | 1.90065 | 1.99 | 0.0477 |
| CrRuns | 1 | 1.23105 | 0.77137 | 1.60 | 0.1118 |
| CrRbi | 1 | -0.70695 | 0.76888 | -0.92 | 0.3588 |
| CrBB | 1 | -0.68911 | 0.41382 | -1.67 | 0.0971 |
| League American | 1 | -34.39136 | 24.37175 | -1.41 | 0.1595 |
| League National | 0 | 0 | . | . | . |
| Division East | 1 | 60.30856 | 27.28730 | 2.21 | 0.0280 |
| Division West | 0 | 0 | . | . | . |
| nOuts | 1 | 0.23273 | 0.12110 | 1.92 | 0.0558 |
| nAssts | 1 | 0.09824 | 0.18888 | 0.52 | 0.6035 |
| nError | 1 | -0.81574 | 3.51436 | -0.23 | 0.8166 |
It is well known that both overfitting and underfitting harm the prediction performance of a model. You can prevent overfitting and underfitting by using a good effect-selection technique. The following statements apply the forward selection method and the SL (significance level) criterion to choose a parsimonious model for the baseball data table:
proc cas;
action quantreg.quantreg/
table={name='baseball'},
class={'league','division'},
model={depvars='Salary',
effects={'nAtBat', 'nHits', 'nHome', 'nRuns',
'nRBI', 'nBB', 'yrMajor', 'crAtBat',
'crHits', 'crHome', 'crRuns', 'crRbi',
'crBB', 'league', 'division', 'nOuts',
'nAssts', 'nError'},
quantiles={0.5},
clb=1},
selection={method='forward', select='sl', sle=0.1};
run;
The clb subparameter in the model parameter requests 95% confidence limits for the parameter estimates. The sle=0.1 subparameter in the selection parameter specifies the significance level for entry. A candidate effect can enter the model at a certain selection step only if the following conditions are met:
Its p-value is the smallest among all the valid candidate effects.
Its p-value is smaller than 0.1 (the significance level for entry).
Output 1.6: Selection Information
The "Selection Information" table in Output 1.6 provides details about the method and criteria used to perform the model selection. The requested selection method is the forward selection method, in which the decisions about what effects to add at any step and when to terminate the selection are both based on the significance level criterion.
Output 1.7: Selection Summary
Each row in the "Selection Summary" table in Output 1.7 shows the effect that enters the model at the corresponding step of the effect selection process together with its p-value for adding the effect into the model at that step.
Output 1.8: Stopping and Selection Reasons
The "Stop Reason" and "Selection Reason" tables in Output 1.8 indicate that effect selection stopped because no candidate for entry was significant at the 0.1 level after step 8. The "Selected Effects" table in Output 1.8 lists the effects that are included in the selected model.
Output 1.9: Details of the Selected Model
| Parameter Estimates | |||||||
|---|---|---|---|---|---|---|---|
| Parameter | DF | Estimate | Standard Error | 95% Confidence Limits | t Value | Pr > |t| | |
| Intercept | 1 | -130.65536 | 33.04546 | -195.73336 | -65.57735 | -3.95 | <.0001 |
| nAtBat | 1 | -1.20522 | 0.41690 | -2.02624 | -0.38419 | -2.89 | 0.0042 |
| nHits | 1 | 7.76667 | 1.81045 | 4.20127 | 11.33207 | 4.29 | <.0001 |
| nRuns | 1 | -3.92180 | 1.87567 | -7.61566 | -0.22795 | -2.09 | 0.0375 |
| nBB | 1 | 3.92049 | 1.04341 | 1.86565 | 5.97532 | 3.76 | 0.0002 |
| CrHits | 1 | 0.17697 | 0.06856 | 0.04195 | 0.31198 | 2.58 | 0.0104 |
| CrHome | 1 | 1.66939 | 0.73083 | 0.23012 | 3.10866 | 2.28 | 0.0232 |
| Division East | 1 | 65.14327 | 24.48038 | 16.93289 | 113.35364 | 2.66 | 0.0083 |
| Division West | 0 | 0 | . | . | . | . | . |
| nOuts | 1 | 0.23719 | 0.11403 | 0.01261 | 0.46176 | 2.08 | 0.0385 |
The "Fit Statistics" and "Parameter Estimates" tables in Output 1.9 give details of the final selected model. You can see that all nine effective parameters (excluding Division West) are significant at the 5% significance level, corresponding to the 95% confidence limits.
Like the sample median, a median regression model is robust to extreme observations, because it depends only on a small middle subset of all the observations in the data set. However, it is less representative of the entire conditional distribution of the response variable.
Salary Data for Baseball Players
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 loaded into CAS. One way to do this is to convert the baseball data table to the comma-separated-value (CSV) file baseball.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="baseball.csv"}
For more information about coding in Lua, see SAS Cloud Analytic Services: Getting Started with Python and Lua and Chapter 2: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.1: Statistical Procedures.
The following code loads the quantreg action set, and then uses the quantreg action to fit quantile regression models to the baseball data table:
s:loadactionset{actionset="quantreg"}
m1 = s:quantreg{
table='baseball',
class={'league','division'},
model={depvars='Salary',
effects={'nAtBat', 'nHits', 'nHome', 'nRuns',
'nRBI', 'nBB', 'yrMajor', 'crAtBat',
'crHits', 'crHome', 'crRuns', 'crRbi',
'crBB', 'league', 'division', 'nOuts',
'nAssts', 'nError'},
quantiles=0.5} }
The following commands display the tables that are produced by this action call:
print(m1.ModelInfo) print(m1.NObs) print(m1.ClassInfo) print(m1.Dimensions) print(m1["Quantile1.FitStatistics"]) print(m1["Quantile1.ParameterEstimates"]) print(m1.Timing)
The following command displays all the tables that are produced by this action call along with their full name paths:
for k,t in pairs(m1) do print(k) print(t) end;
The following commands perform quantile regression model selection on the baseball data table by using the forward selection method:
m2 = s:quantreg{
table='baseball',
class={'league','division'},
model={depvars='Salary',
effects={'nAtBat', 'nHits', 'nHome', 'nRuns',
'nRBI', 'nBB', 'yrMajor', 'crAtBat',
'crHits', 'crHome', 'crRuns', 'crRbi',
'crBB', 'league', 'division', 'nOuts',
'nAssts', 'nError'},
quantiles={0.5},
clb=1},
selection={method='forward', select='sl', sle=0.1} }
The following commands display the tables that are produced by this action call:
print(m2.SelectionInfo) print(m2["Quantile1.Summary.SelectionSummary"]) print(m2["Quantile1.Summary.StopReason"]) print(m2["Quantile1.Summary.SelectionReason"]) print(m2["Quantile1.Summary.SelectedEffects"]) print(m2["Quantile1.SelectedModel.FitStatistics"]) print(m2["Quantile1.SelectedModel.ParameterEstimates"])
For details about the results of this analysis, see the CASL version of this example.
Salary Data for Baseball Players
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 loaded into CAS. One way to do this is to convert the baseball data table to the comma-separated-value (CSV) file baseball.csv and then use the following code to load the CSV file into CAS:
s.upload('baseball.csv')
For more information about coding in Python, see SAS Cloud Analytic Services: Getting Started with Python and Lua and Chapter 2: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.1: Statistical Procedures.
The following code loads the quantreg action set, and then uses the quantreg action to fit quantile regression models to the baseball data table:
s.loadactionset(actionset='quantreg')
m1=s.quantreg(
table='baseball',
classvars=['league','division'],
model={'depvars':'Salary',
'effects':['nAtBat', 'nHits', 'nHome', 'nRuns',
'nRBI', 'nBB', 'yrMajor', 'crAtBat',
'crHits', 'crHome', 'crRuns', 'crRbi',
'crBB', 'league', 'division', 'nOuts',
'nAssts', 'nError'],
'quantiles':{0.5}} )
The following commands display the tables that are produced by this quantreg action call:
print(m1.ModelInfo) print(m1.NObs) print(m1.ClassInfo) print(m1.Dimensions) print(m1["Quantile1.FitStatistics"]) print(m1["Quantile1.ParameterEstimates"]) print(m1.Timing)
The following command displays all the tables that are produced by this action call along with their full name paths:
print(m1)
The following commands perform quantile regression model selection on the baseball data table by using the forward selection method:
m2=s.quantreg(
table='baseball',
classvars=['league','division'],
model={'depvars':'Salary',
'effects':['nAtBat', 'nHits', 'nHome', 'nRuns',
'nRBI', 'nBB', 'yrMajor', 'crAtBat',
'crHits', 'crHome', 'crRuns', 'crRbi',
'crBB', 'league', 'division', 'nOuts',
'nAssts', 'nError'],
'quantiles':{0.5},
'clb':1},
selection={'method':'forward',
'select':'sl',
'sle':0.1} )
The following commands display the tables that are produced by this action call:
print(m2.SelectionInfo) print(m2["Quantile1.Summary.SelectionSummary"]) print(m2["Quantile1.Summary.StopReason"]) print(m2["Quantile1.Summary.SelectionReason"]) print(m2["Quantile1.Summary.SelectedEffects"]) print(m2["Quantile1.SelectedModel.FitStatistics"]) print(m2["Quantile1.SelectedModel.ParameterEstimates"])
For details about the results of this analysis, see the CASL version of this example.
Copyright © SAS Institute Inc. All rights reserved.