
SAS—計(jì)算K-S值及畫圖
近來,時(shí)于夜半下雨,也常在夢(mèng)里被雨擾醒。究其原因,也是因?yàn)榻鼇砉ぷ鳠o趣,本身也只喜歡寫寫SAS或Python代碼,做模型,可近來卻連數(shù)據(jù)權(quán)限都沒有,萬灰俱滅。雨聲輕輕,卻也容易愁悶得睡不著。想著要去外包,卻因自己的猶豫不決也沒有去成。
好了,不說廢話了。昨天有人在微信上問KS的計(jì)算方式。今天介紹一下KS值吧。
先看一段程序:
data logistic;
input accident age vision drive;
datalines;
1 17 1 1
1 44 0 0
1 48 1 0
1 55 0 0
1 75 1 1
0 35 0 1
0 42 1 1
0 57 0 0
0 28 0 1
0 20 0 1
0 38 1 0
0 45 0 1
0 47 1 1
0 52 0 0
0 55 0 1
1 68 1 0
1 18 1 0
1 68 0 0
1 48 1 1
1 17 0 0
1 70 1 1
1 72 1 0
1 35 0 1
1 19 1 0
1 62 1 0
0 39 1 1
0 40 1 1
0 55 0 0
0 68 0 1
0 25 1 0
0 17 0 0
0 45 0 1
0 44 0 1
0 67 0 0
0 55 0 1
1 61 1 0
1 19 1 0
1 69 0 0
1 23 1 1
1 19 0 0
1 72 1 1
1 74 1 0
1 31 0 1
1 16 1 0
1 61 1 0
;
proc logistic data=logistic outest=model;
model accident(event='1')=age vision drive/selection=stepwise
sle=0.05 sls=0.05 outroc=roc;
output out= pred p=p1 ;
run;
proc npar1way data=pred noprint;
class accident;
var p1;
output out=ks;
run;
以上是一段常規(guī)的計(jì)算KS值。
首先要了解什么是KS值,K-S值(kolmogorov-smirnov curve)將總體進(jìn)行n等分組并按照違約概率降序排列,計(jì)算每一等份中違約與正常百分比的累積分布,繪制出兩者之間的差值就是K-S曲線。其中K-S曲線中的最大值即為K-S統(tǒng)計(jì)量,取值范圍在0到1之間。
以下是計(jì)算K-S值及畫圖的宏程序:
/**data:邏輯回歸后包含驗(yàn)證結(jié)果的數(shù)據(jù)集,var:違約概率變量,status:分類變量,data1:切分后的變量,Mks:最大ks值,M:分組組數(shù)**/
%macro KS(data, var, status, data1, Mks,M);
proc sort data=&data;
by &var;
run;
proc sql noprint;
select sum(&status) into:P from &data;
select count(*) into :Ntot from &data;
quit;
%let N=%eval(&Ntot-&P);
data &data1;
set &data nobs=NN;
by &var;
retain tile 1 totP 0 totN 0;
Tile_size=ceil(NN/&M);
if &status=1 then totP=totP+&status;
else totN=totN+1;
Pper=totP/&P;
Nper=totN/&N;
if _N_ = Tile*Tile_Size then
do;
output;
if Tile <&M then
do;
Tile=Tile+1;
SumResp=0;
end;
end;
keep Tile Pper Nper;
run;
data temp;
Tile=0;
Pper=0;
NPer=0;
run;
Data &data1;
set temp &data1;
run;
data &data1;
set &data1;
Tile=Tile/&M;
label Pper='Percent of Positives';
label NPer ='Percent of Negatives';
label Tile ='Percent of population';
KS=NPer-PPer;
run;
proc sql noprint;
select max(KS) into :&Mks from &data1;
run; quit;
proc datasets library=work nodetails nolist;
delete temp ;
run;
quit;
%mend;
%macro PlotKS(data1);
symbol1 value=dot color=red interpol=join height=1;
legend1 position=top;
symbol2 value=dot color=blue interpol=join height=1;
symbol3 value=dot color=green interpol=join height=1;
proc gplot data=&data1;
plot( NPer PPer KS)*Tile / overlay legend=legend1;
run;
quit;
goptions reset=all;
%mend;
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03