2010年2月1日 星期一

NxN列聯表計算卡方檢定

繼之前簡易2X2列聯表計算卡方檢定只能計算2X2之列聯表
windows都升級為Win7了
當然列聯表也得升級一下
所以我改寫了新版wondow視窗
擴充為NxN列聯表
程式如下
直接在SAS內執行即可
Data Welcom;
Window Welcom Color=white Columns=100 Rows=100
#5 @50 "--------------Welcom To Use this SAS Program--------------" color=red
#7 @50 "------------This Program Test For Independence------------" color=red
#9 @50 "----------------Contingency Table Analysis----------------" color=blue
#14 @50 "請按Enter進入欄位設定及檢定視窗"
;
Display Welcom ;
stop ;
run;
Data SelectTable;
retain x 2 y 2;
Window Select Color=white Columns=100 Rows=100

#1 @58 '--------------Contingency Table Analysis--------------' color=red
#3 @58 '請輸入為X乘Y列聯表' color=red
#5 @52 'X*Y=' color=blue
#5 @63 '*' color=red
#5 @58 x 5. color=red a=underline auto=yes required=yes
#5 @64 y 5. color=red a=underline auto=yes required=yes



#14 @50 "各欄位數值鍵入完請按Enter以方便跳位"
#15 @50 "鍵入完成後請至上方Command===>處鍵入 : end"

;
Display Select;

run;

proc sql ;
select x , y into :x ,:y
from SelectTable (obs=1);
run;
quit;


%macro Tabletrans;


Data Tablewindow;

retain
%do ssy= 1 %to &y;
%do ssx = 1 %to &x ;
x&ssx.y&ssy 0
%end;
%end;
;

Window Table Color=white Columns=100 Rows=100

#3 @58 '變相X' color=red


%do xn=1 %to &x;
%let xsit=%eval(43+10*&xn);
#4 @&xsit "X=&xn" color=blue
%end;

#7 @43 '變' color=red
#8 @43 '項' color=red
#9 @43 'Y' color=red

%do yn=1 %to &y;
%let ysit=%eval(4+3*&yn);
#&ysit @46 "Y=&yn" color=blue
%end;

%let vlbar=%eval(5+3*&y);
%let xbar="----------" ;
#5 @50
%do b=1 %to &x ;
&xbar
%end;

%let xbar="----------" ;
#&vlbar @50
%do b=1 %to &x ;
&xbar
%end;

%do vb=1 %to &y;
%let vspec=%eval(5+3*(&vb-1));
%Let xcbar='|---------' ;
#&vspec @50
%do b=1 %to &x ;
&xcbar
%end;
%end;

%do a= 1 %to &y ;
%let vca1=%eval(6+3*(&a-1));
%let vca2=%eval(7+3*(&a-1));
%let vca3=%eval(8+3*(&a-1));
%let h=%eval(&x+1);
%do hbarn= 1 %to &h ;
%let hbar=%eval(40+10*&hbarn) ;
#&vca1 @&hbar '|'
#&vca2 @&hbar '|'
%end;
#&vca3 @&hbar '|'
%end;

%do yyy= 1 %to &y;
%let varys=%eval(3+3*&yyy);

%do xxx = 1 %to &x ;
%let varxspe=%eval(41+10*&xxx);


#&varys @&varxspe x&xxx.y&yyy 5. color=red a=underline auto=yes required=yes

%end;

%end;

%let footnote1=%eval(&vlbar+5);
%let footnote2=%eval(&vlbar+6);
#&footnote1 @50 "各欄位數值鍵入完請按Enter以方便跳位"
#&footnote2 @50 "鍵入完成後請至上方Command===>處鍵入 : end"
;
Display Table;
run;

%mend;
%Tabletrans;
proc transpose data=Tablewindow prefix=weight name=local out=weightnum;
run;


data tablecut;
set weightnum;
xx=scan(local,1 , 'y');
xxx=scan(xx,1,'x');
yyy=scan(local,2 , 'y');
label xxx='變項X' yyy='變項Y';
run;




proc freq data= tablecut;
table yyy*xxx /chisq norow nocol nopercent;
weight weight1;
run;







不過最大值好像18*18 就很緊繃了
因為會超出視窗..............