最近做项目刚刚开发的测试代码。
%let n=5;
%let cutoff=0.7;
proc iml;
reset deflib=work;
a={22 10 2 3 7,
14 7 10 0 8,
-1 13 -1 -11 3,
-3 -2 13 -2 4,
9 8 1 -2 4,
9 1 -7 5 -1,
2 -6 6 5 1,
4 5 0 -2 2};
create tb from a;
append from a ;
quit;
PROC STDIZE data=tb out=tb_std;run;
proc princomp data=tb_std out=ww;run;
proc iml;
reset deflib=work;
use tb_std;
read all into tb_std;
print tb_std;
call svd(u,q,v,tb_std);
reset fuzz;
print u q v ;
xx=tb_std*v;
print xx;/*the result xx matrix is consistent with data set ww**/
xxx=u*diag(q);/*the result xx matrix is consistent with data set ww**/
print xxx;
call svd(tu,tq,tv,t(tb_std));
reset fuzz;
print tu tq tv ; /*tu with rank is equal to v with rank; so are tq and tv(u)*/
sq=sum(q);
print sq;
retain_eig={0};
idx={0};
do i=1 to &n.;
eig=q;
retain_eig=retain_eig+eig;
rate_eig=retain_eig/sq;
if rate_eig >= &cutoff. then do;idx=i; new_eig=q[1:idx];stop;end;
end;
print retain_eig;print idx;print new_eig;
v1=v[,1:idx];print v1;
u1=u[,1:idx];print u1;
q1=diag(q[1:idx]);print q1;
create u_matrix from u1 ;
append from u1;
create v_matrix from v1 ;
append from v1;
/***valid eigvalue and single value***/
call eigen(eig_value,eig_vect,t(tb_std)*tb_std);
print eig_value;
single=sqrt(abs(eig_value));
print single;
quit;