program development (2) - stat.ipb.ac.id filereview sweep • to use the sweep function for...
TRANSCRIPT
Review SWEEP Operator
SWEEP Algorithm:
• D = akk
• Divide kth row using D
• For every i k rows, do:
1.B = aik
2.ith row minus using B* kth row
3.aik = -B/D
• akk = 1/D
Review SWEEP
• To use the SWEEP function for regression, suppose the matrix A contains 𝑋′𝑋 𝑋′𝑌𝑌′𝑋 𝑌′𝑌
• where X'X is k ×k. Then B = SWEEP(A,1 ... k) contains
𝑋′𝑋 −1 𝑋′𝑋 −1𝑋′𝑌
−𝑌′𝑋 𝑋′𝑋 −1 𝑌′(𝐼 − 𝑋 𝑋′𝑋 −1𝑋′)𝑌
• The partitions of B form the beta values, SSE, and a matrix proportional to the covariance of the beta values for the least-squares estimates of B in the linear model
Review SWEEP
6120
12280
006
' XX
4
6
14
y'X 42y'y
y'yX'y
y'XX'XA
Step Model1 y=b0+ b1X1
2 y=b0+b1X1+b2X2
3 y=b0+b2X2
4 y=b0
Review SWEEP
proc iml;reset print;a1={6 0 0,0 28 -12,0 -12 6};a2={14, 6, -4};a3=42;a4=a1//a2`;a5=a2//a3;A=a4||a5;
B=SWEEP(A,{1 2 3});C=SWEEP(B,{3});D=SWEEP(A,{1 2});E=SWEEP(B,{2});F=SWEEP(A,{1 3});
x1=A[{1 2},{1 2}];b=a2[{1 2},];beta=inv(x1)*b;D=SWEEP(A,{1 2});
x2=A[{1 3},{1 3}];b2=a2[{1 3},];beta2=inv(x2)*b2;E=SWEEP(A,{1 3});
proc iml;reset print;start regg(x,y);nc=ncol(x);nb=nrow(x);satu=J(nb,1,1);xb=satu||x;beta=inv(xb`*xb)*(xb`*y);yduga = xb*beta; sisaan =y-yduga;JKR= y`*(xb*inv(xb`*xb)*xb`)*y-(y`*satu*satu`*y)/nb;JKT= y`*y-(y`*satu*satu`*y)/nb;JKG= JKT-JKR;
KTR=JKR/nc;KTG=JKG/(nb-nc-1);Fh=KTR/KTG;P_value=1-PROBF(Fh,nc,(nb-nc-1));
W=inv(xb`*xb);D=diag(W);Z=sqrt(W[1,1]);
do i=1 to (nc);Z=Z||sqrt(W[i+1,i+1]);end;SE=Z`*sqrt(KTG);
T=beta[1]/SE[1];do i=1 to (nc);T=T||(beta[i+1]/SE[i+1]);end;T_hitung=T`;
pT=1-PROBT(T_hitung[1],(nb-nc-1));do i=1 to (nc);pT=pT||(1-PROBT(T_hitung[i+1],(nb-nc-1)));end;P_value2=pT`;
F_h=Fh//.//.;p_v=P_value//.//.;hasil=beta||SE||T_hitung||P_value2||F_h||p_v;ket={beta,SE,T,P_valueT,F,P_valueF};print hasil[colname=ket];
return(hasil);finish;
A={69.0 1.0 112.5,62.8 1.0 102.5,59.8 1.0 84.5,59.0 2.0 99.5,56.3 2.0 77.0,64.8 2.0 128.0,66.5 3.0 112.0,56.5 3.0 84.0,63.5 3.0 102.5,62.5 4.0 112.5,51.3 4.0 50.5,66.5 4.0 112.0};X=A[,{2 3}];Y=A[,1];
j=regg(X,Y);print j;
Module Programming (2)
• Create a module for principal componentanalysis in regression (using module definedbefore-nested module)
start rku(cor,p,x,y);n=ncol(cor);call eigen(ac,vc,cor);pac=ac/sum(ac);kpac=pac[1];
do i=2 to n;kpac=kpac||sum(pac[1:i]);end;
if kpac[1]>=p then c=1;else c=0;
do i=2 to n;if kpac[i]>=p thendo;
c=c||1;end;
elsedo;
c=c||0;end;end;
i=1;j=0;k=0;do while(i<=n);
j=j+c[i];if j=1 then k=i;else i=i+1;
end;
acb=ac[1:k];vcb=vc[,1:k];
sku=x*vcb[,1];if k>1 thendo;
do i=1 to (k-1);sku=sku||x*vcb[,i+1];
end;end;
h=regg(sku,y);
return(h);finish;
rho={1 0.9 0.3,0.9 1 0.4,0.3 0.4 1};X={6.1 6.4 4.0,-1.0 -1.0 2.6,2.9 3.0 3.5,-1.1 -1.2 3.4,1.4 1.6 3.8,0.9 1.6 2.2,2.4 2.7 2.5,2.5 3.0 -0.3,4.7 4.3 3.5,4.0 2.4 0.4};y={3,4,2,5,6,1,8,9,3,5};k=rku(rho,0.75,X,y);print k;