/*********************************************************/
/*1.模拟数据1;测试标准化方法对聚类的影响
模拟数据,样本量相同,均值和方差不相同*/
/*********************************************************/
data compact;
keep x y c;
n=100;
scale=1; mx=0; my=0; c=1;link generate;
scale=2; mx=8; my=0; c=2;link generate;
scale=3; mx=4; my=8; c=3;link generate;
stop;
generate:
do i=1 to n;
x=rannor(1)*scale+mx;
y=rannor(1)*scale+my;
output;
end;
return;
run;
title '模拟数据1';
proc gplot data=compact;
plot y*x=c;
symbol1 c=blue;
symbol2 c=black;
symbol3 c=red;
run;
proc stdize data=compact method=std
out=scompacted2;
var x y;
run;
title '标准化后的模拟数据1';
proc gplot data=scompacted2;
plot y*x=c;
symbol1 c=blue;
symbol2 c=black;
symbol3 c=red;
run;
/*********************************************************/
/*2.create result table*/
/*********************************************************/
data result;
length method$ 12;
length misclassified 8;
length chisq 8;
stop;
run;
%let inputs=x y;
%let group=c;
%macro standardize(dsn=,nc=,method=);
title "&method";
%if %bquote(%upcase(&method))=NONE %then %do;
data temp;
set &dsn;
run;
%end;
%else %do;
proc stdize data=&dsn method=&method out=temp;
var &inputs;
run;
%end;
proc fastclus data=temp maxclusters=&nc least=2
out=clusout noprint;
var &inputs;
run;
proc freq data=clusout;
tables &group*cluster / norow nocol nopercent
chisq out=freqout;
output out=stats chisq;
run;
data temp sum;
set freqout end=eof;
by &group;
retain members mode c;
if first.&group then do;
members=0; mode=0;
end;
members=members+count;
if cluster NE . then do;
if count > mode then do;
mode=count;
c=cluster;
end;
end;
if last.&group then do;
cum+(members-mode);
output temp;
end;
if eof then output sum;
run;
proc print data=temp noobs;
var &group c members mode cum;
run;
data result;
merge sum (keep=cum) stats;
if 0 then modify result;
method = "&method";
misclassified = cum;
chisq = _pchi_;
pchisq = p_pchi;
output result;
run;
%mend standardize;
%standardize(dsn=compact,nc=3,method=ABW(.5));
%standardize(dsn=compact,nc=3,method=AGK(.9));
%standardize(dsn=compact,nc=3,method=AHUBER(.5));
%standardize(dsn=compact,nc=3,method=AWAVE(.25));
%standardize(dsn=compact,nc=3,method=EUCLEN);
%standardize(dsn=compact,nc=3,method=IQR);
%standardize(dsn=compact,nc=3,method=L(1));
%standardize(dsn=compact,nc=3,method=L(2));
%standardize(dsn=compact,nc=3,method=MAD);
%standardize(dsn=compact,nc=3,method=MAXABS);
%standardize(dsn=compact,nc=3,method=MEAN);
%standardize(dsn=compact,nc=3,method=MEDIAN);
%standardize(dsn=compact,nc=3,method=MIDRANGE);
%standardize(dsn=compact,nc=3,method=NONE);
%standardize(dsn=compact,nc=3,method=RANGE);
%standardize(dsn=compact,nc=3,method=SPACING(.3));
%standardize(dsn=compact,nc=3,method=STD);
%standardize(dsn=compact,nc=3,method=SUM);
%standardize(dsn=compact,nc=3,method=USTD);
proc sort data=result;
by misclassified;
run;
title '汇总数据';
title2 '聚类判定类别错误样本数排序';
proc print data=result;
run;
|