LIME Procedure
Example 21.1 Modify DS2 Code That Has a DROP or KEEP Statement
This example shows how to use the LIME procedure to calculate LIME values by using an analytic store and DS2 code. It shows how to include DS2 code alongside an analytic store for model specification, as well as how to avoid a possible error.
The following DATA step creates the input data table mylib.dmagecr. These statements assume that your CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.
data mylib.dmagecr;
set sampsio.dmagecr;
id = _N_;
keep age amount coapp duration foreign job good_bad id;
run;
The following DATA step creates the query data table mylib.query in your CAS session:
data mylib.query;
set sampsio.dmagecr;
keep age amount coapp duration foreign job good_bad;
if _N_ = 20;
run;
The following statements use the FOREST procedure to build a forest model to predict whether the credit rating of each individual in the mylib.dmagecr data table is good or bad. The procedure also outputs an analytic store named forest_astore.
proc forest
data=mylib.dmagecr
maxDepth=4
seed=1234;
input age amount duration / level=interval;
input coapp foreign job /level=nominal;
target good_bad;
savestate rstore=mylib.forest_astore;
run;
The following statements run the ASTORE procedure to generate DS2 code for the analytic store and save the DS2 code to a file named DS2Code.sas:
proc astore;
describe rstore=mylib.forest_astore epcode="DS2Code.sas";
run;
The DS2 code that PROC ASTORE creates contains a KEEP statement that does not keep all the model inputs. Because of this KEEP statement, if you use the DS2 code with the analytic store in PROC LIME, the analysis in the procedure will fail. When you use DS2 code in PROC LIME and the DS2 code includes a KEEP statement, the KEEP statement must include all input variables, and likewise a DROP statement cannot include any input variables. The full set of input variables is needed in the scored table as part of the analysis that PROC LIME performs. If you have a KEEP or DROP statement in your DS2 code, the procedure issues a warning. An error can occur if the required variables do not exist when PROC LIME uses the model for scoring.
There are several ways to ensure that your DS2 code is compatible with PROC LIME. You can manually save the DS2 code and edit any KEEP or DROP statements that it includes. Alternatively, you can use regular expressions to remove the KEEP or DROP statements.
The following DATA step modifies the DS2 code to remove the KEEP statement, and it outputs the modified DS2 code to a file named DS2code_modified.sas. The regular expression that is used here is an example; it might not always work for the DS2 code that you have.
data _null_;
length allcode $30000.;
retain allcode;
file "DS2code_modified.sas";
infile "DS2code.sas" end=eof;
input;
allcode = cats(allcode, _infile_, '0A'x);
if eof then do;
allcode = prxchange('s/keep[^;]*;//',1,allcode);
put allcode;
end;
run;
The following statements run PROC LIME and produce ODS tables of the results:
proc lime
data=mylib.query
referenceData=mylib.dmagecr
seed=12345;
input age amount duration / level=interval;
input coapp foreign job /level=nominal;
predictedTarget P_good_badbad;
astoreModel rstore=mylib.forest_astore;
code file="DS2Code_modified.sas";
run;
The DATA= option names the input query data table, which specifies the data to use for calculating LIME values. The REFERENCEDATA= option names the reference data table. The SEED= option specifies the seed to use for pseudorandom number generation. The two INPUT statements specify that age, amount, and duration are the interval variables and coapp, foreign, and job are the nominal variables. The PREDICTEDTARGET statement specifies that P_good_badbad be used as the predicted target variable. The ASTOREMODEL and CODE statements specify the analytic store and the DS2 code that contain the model to explain.
Output 21.1.1 displays the explainer information. Output 21.1.2 displays the LIME values for the query observation, which are the estimated parameters of the local regression model. Output 21.1.3 displays the explainer fidelity information.
Output 21.1.1: Explainer Information
| Explainer Information | |
|---|---|
| Data Generation Method | Query Centered |
| Distance Method | Normalized Euclidean with Exponential Kernel |
| Explainer Type | Regression with LASSO |
| Binary Encoding | None |
| Standardize Parameter Estimates | None |
| Include Missing as a Level | No |
| Number of Samples | 3000 |
| Exponential Kernel Divisor | 1.8371173071 |
| Mixed Distance Weight | 1 |
| Seed | 12345 |
Output 21.1.2: Parameter Estimates
| Parameter Estimates | |||||
|---|---|---|---|---|---|
| Variable | Nominal Variables | Estimate | Query Level | ||
| coapp | foreign | job | |||
| Intercept | . | . | . | 0.2931126743 | . |
| age | . | . | . | -0.002966058 | 31 |
| amount | . | . | . | 7.6573524E-6 | 3430 |
| duration | . | . | . | 0.0028286862 | 24 |
| coapp | 2 | . | . | 0.044016531 | 0 |
| coapp | 3 | . | . | -0.015015446 | 0 |
| coapp | 1 | . | . | 0 | 1 |
| foreign | . | 2 | . | -0.108113816 | 0 |
| foreign | . | 1 | . | 0 | 1 |
| job | . | . | 1 | 0.0239071866 | 0 |
| job | . | . | 3 | 0 | 1 |
Output 21.1.3: Explainer Fidelity
| Explainer Fidelity | ||
|---|---|---|
| Model Prediction | Explainer Prediction | Explainer RMSE |
| 0.2824168939 | 0.2953180482 | 0.0421468777 |