A scorecard for Logistic Regression models

by Irina 25. August 2007 08:58

Scorecards are a common way of displaying the patterns found by a logistic regression model. They display in a clear, intuitive way the regression coefficients and can be used to perform risk evaluation operations (simplified predictions). For one particular state, y1, we start by extracting the coefficients (c0,c1, ...) that describe the logistic regression formula for that state.

We convert to 0 the minimal coefficient in each variable and the rest of coefficients transform in the way that difference between the minimal coefficients and the rest of coefficients remains the same These coefficients are then normalized between, say, 0 and 1000, giving an intuitive perspective on the relative importance of each coefficient. As each coefficient corresponds to a state of an input attribute, the normalized values are also describing the relative importance of each input attribute state. The score card presented here is computing these relative importance scores. Score cards check certain conditions, and for example, and if these conditions are met, points are added to an overall score.

proc logistic data=Panel OutModel= ModelParam   namelen=200
descend    ;
class &groupp
/ 	param=glm ;
model target=&groupp/selection=stepwise;
output out=toz_LOGISTIC_2 p=phat_new xbeta=xb;
ods output ParameterEstimates =  coeff_est;
run;

proc sql ;
create table score_card as
select
b.*,
sum(max_est1/counter) as sum_max,
case when est1=max_est1 then 1 else 0 end as max_cat,
round(1000*((est1)/calculated sum_max)) as score

from (select
a.*,
max(est1) as max_est1,
count(*) as counter

from (
select *,min(Estimate) as min_est,
count(*) as counter,
case when calculated min_est=Estimate
then 0
else Estimate-  calculated min_est end as est1
from coeff_est
where variable ne 'Intercept'
group by variable )  a
group by variable  )b

;
quit;

A More Involved Macro Application

by Irina 10. August 2007 10:19
Example: Generate several SAS dataset names.
%MACRO DSNAMES(PREFIX,FIRST,LAST);
%LOCAL N;
%DO N=&FIRST %TO &LAST;
&PREFIX&N
%END;
%MEND DSNAMES;
data
%dsnames(count,1,4)
;
. . .
Generates:
data count1 count2 count3 count4;

SAS DATA Step Interfaces

  • SYMGET, SYMPUT, and other interfaces using macro variables can transfer values between SAS steps.
  • SYMGET returns macro variable values to the DATA step
  • Macro variables created with SYMPUT, can be referenced via & in the NEXT step.
Example:
proc means nway median data=sample;
where team_nbr  ne  1 ;
var  ttl_income_neto1 ;
class team_nbr;
output out=res median=income_median;
run;

data k;
set res;
length m $1;
team=put(team_nbr,1.) ;
run;

data _null_;
set k;
call symput('norma'||team,income_median) ;
run;


data sample1;
set  sample;
length m $1;
team=put(team_nbr,1.) ;
run;

data sample3 ;
set sample;
norma=symget('norma'||team);
target=(ttl_income_neto1 ge symget('norma'||team));
run;

CALL SYMPUTX

SYMPUTX will automatically convert and trim numbers.

Example:

%LET SAMPSIZE=5;
DATA SAMPLE;
DO N=1 TO &SAMPSIZE;
OBSNO=CEIL(UNIFORM(0)*TOTOBS);
SET POP POINT=OBSNO NOBS=TOTOBS;
OUTPUT;
END;
CALL SYMPUTX('MTOTOBS',TOTOBS);
STOP;
RUN;

CALL EXECUTE:


Call Execute Using a Macro Define a macro to do most of the work, then call it.
Resolves an argument and executes the resolved value at step boundary. This is a very powerful tool can can use the power of the data step to generate code.
%macro printcty(mcountynm,mctyobs);
PROC PRINT DATA=PERM.COUNTYDT;
SUGI 31 Hands-on Workshops
WHERE COUNTYNM="&mcountynm";
OPTIONS PAGENO=1;
TITLE "REPORT FOR COUNTY &mcountynm";
FOOTNOTE "TOTAL OBSERVATION COUNT WAS &MCTYOBS";
RUN;
%mend printcty;
data pass2;
set perm.countydt;
by countynm;
if first.countynm then ctyobs=0;
ctyobs+1;
if last.countynm then
call execute('%printcty(' !!
countynm !!
',' !!
put(ctyobs,2.) !!
')'
);
run;
The RESOLVE Function

Resolves the value of a text expression during DATA step execution. Syntax: variable=RESOLVE(argument);
%let event=Holiday;
%macro mdate;
SUGI 31 Hands-on Workshops
4th of July
%mend mdate;
data test;
length var1-var3 $ 15;
when='%mdate';
var1=resolve('&event'); /* macro variable reference */
var2=resolve('%mdate'); /* macro invocation */
var3=resolve(when); /* DATA step var with macro call */
put '*** ' var1= var2= var3=;
run;
*** var1=Holiday var2=4th of July var3=4th of July

System options for debugging macros

by Irina 10. August 2007 09:23

These five system options affect the kinds of messages SAS writes in your log. The default settings appear in bold.
MERROR | NOMERROR when this option is on, SAS will issue a warning if you invoke a macro that SAS cannot find.
SERROR | NOSERROR    when this option is on, SAS will issue a warning if you use a macro variable that SAS cannot find.
MLOGIC | NOMLOGIC    when this option is on, SAS prints in your log details about the execution of macros.
MPRINT | NOMPRINT     when this option is on, SAS prints in your log the standard SAS code generated by macros.
SYMBOLGEN | NOSYMBOLGEN when this option is on, SAS prints in your log the values of macro variables.
While you want the MERROR and SERROR options to be on at all times, you will probably want to turn on MLOGIC, MPRINT, and SYMBOLGEN one at a time and only while you are debugging since they tend to make your log hard to read. To turn them on (or off), use the
OPTIONS statement, for example:
OPTIONS MPRINT NOSYMBOLGEN NOMLOGIC;

How write the proc sort as a proc sql in SAS.

by Irina 30. July 2007 12:02
data new; 
input system $ findings $ visit $; 
cards; 
car abnormal base 
car abnormal base 
car normal base 
car normal eot 
car normal eot 
resp abnormal base 
resp abnormal base 
resp abnormal base 
resp abnormal eot 
resp normal eot 
resp normal eot 
; 
run; 

PROC SORT VERSION:

proc sort nodupkey data=new out=res_nodup;
   by system   findings;
   run;

PROC SQL VERSION:

  proc sql;
   create table res_nodup_sql as
   select system,
   findings,
   visit,
   monotonic() As row_numb ,
   min(calculated row_numb) as indicator
   from new
   group by 1,2
   having row_numb=indicator ;
   quit;

Tags: sort, sort_sql

SAS

Proc datasets tips

by Irina 28. July 2007 11:46
Splitting the data.frame into 3 parts
data en1(drop = course) en2 en3;
set deet07.dx2007;
if 0 <= _N_ <= 1000 then output en1;
else if 1000 < _N_ <= 2000 then output en2;
else if 2000 < _N_ <= 3000 then output en3;
run;

/* 1. list contents of last dataset used, en3 */
proc contents data= _LAST_;
run;
/* 2. list contents of all datasets in the work library */
proc contents data=work._ALL_;
run;
/* 3. list datasets in the work library */

proc contents data=work._ALL_ nods;

run;

/* 4. proc datasets version of above - list all datasets in the specified library */
proc datasets library=work;
quit; run;

/* 5. describe a single dataset */
proc datasets library=work;
contents data=en1;
quit; run;

/* 6. describe all work datasets */
proc datasets;
contents data=_ALL_;
quit; run;

/* 7. copy all datasets in work to stud07, excluding e3*/
proc datasets;
copy in=work out=stud07;
exclude e3;
quit; run;
 

/* 8. delete everything in work (deletes datasets and formats) */
proc datasets library=work kill;
quit; run;


/* 9. delete just the datasets */

proc datasets library=work memtype=data kill;
quit; run;


/* 10. move all datasets except en3 from stud07 to work */

proc datasets;
copy move in=stud07 out=work;
exclude en3;
quit; run;


/* 11. move just one dataset from stud07 to work */

proc datasets;
copy move in=stud07 out=work;
select en3;
quit; run;


/* 12. delete an individual dataset */
proc datasets library=work;
delete en3;
quit; run;


/* 13. change the name of a dataset */
proc datasets library=work;
change en2=en4;
quit; run; 


/* 14. append datasets - note this is quicker than using a set command, as only the appended dataset is read in */
proc datasets library=work;
append out=en4 data=en1;
quit; run;


/* 15. rename a variable */

proc datasets library=work NOlist;
modify en1;
rename student=personid course = course_cd;
run; quit;

About the author

Irina Spivak Irina Spivak
Team Leader at G-Stat. More...


Send mail Email

Authors

Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Sign in

    eXTReMe Tracker