Ʈ  |  Contact Us
 
Ȩ > SAS Tech & Tip > SAS Programming
[SAS α׷ ǽ] IML Weighted Regression ϱ 2019.05.08
ڼ 69 0
http://www.mysas.co.kr/sas_tiptech/a_question.asp?b_no=11089&cmd=content&bd_no=5

䱸ϴ ϰֽϴ.

: Weighted regression with IML

 

 

 

1. Introduction

ð, University Edition , REG Ͻ Ͽ ȸ͸ غҽϴ. ( ũ ) ϰ غ, ȸ μ ˱⿣ ƽ ϴ. ̹ ð IML ڼϰ غ ϰڽϴ.

 

https://blog.naver.com/statpark1014/221521344309 

(PROC REG ȸ ϱ)

 

2. Data

ʹ ÿ ͷ ϰڽϴ.

<code1>

data RegData;

input y x w;

datalines;

2.3 7.4 0.058

3.0 7.6 0.073

2.9 8.2 0.114

4.8 9.0 0.144

1.3 10.4 0.151

3.6 11.7 0.119

2.3 11.7 0.119

4.6 11.8 0.114

3.0 12.4 0.073

5.4 12.9 0.035

6.4 14.0 0

;

3. Practice

IML غ ϰڽϴ.

ϰ Ʒ ϴ.


 

 



ȸ ̷п ڼ Ʒ Ͻñ ٶϴ.

 

https://blog.naver.com/statpark1014/221519820569

( ȸ ̷)

 

<code2>

proc iml;

/* IML */

start PolyRegEst(Y, X, w, deg);

Yw = sqrt(w)#Y; /* 1. w ٿ Yϱ */

XDesign = j(nrow(X), deg+1);


do j = 0 to deg; /* 2. */


Xdesign[,j+1] = X##j;


end;


Xw = sqrt(w)#Xdesign; /* 3. Ŀ ġ ϱ */


b = solve(Xw`*Xw, Xw`*Yw); /* 4. ȸ */


return b;


finish;


use RegData; read all var {Y X w}; close; /* Ϳ ġ ҷ*/



ùٿ PolyRegEst Ľ ־µ, ̸ Ͽ ȸ ֽϴ.


Yw մϴ. # Ұ Ÿϴ.( ij ƴ) 11x1 Yİ 11x1 w ҳ ֽϴ.


( w ƴ w Ŀ wٳ w ġ ״ DZ Դϴ. )



ؿ XDesign ϴ X ݴϴ. j(a,b,c) axb̰ Ұ c մϴ. nrow(X)̰ deg+1 ǰڱ. ٷ ؿ Ҹ ־ִµ ,X##j X j մϴ.




XDesign ظ Ϲ ȸ Խϴ. ȸʹ ۵DZ ڽ ִ Դϴ.

 


<code3>



/* ߿, ( ) ȸ */


do deg = 0 to 2;


b = PolyRegEst(Y, X, w, deg);


d = char(deg,1); /* '0', '1', or '2' */


labl = "Estimates (deg=" + d + ")";


print b[L=labl rowname=("b0":("b"+d))]; /* ȸ */


end;



charԼ Ұ ִ մϴ. ÷̸ b0, b1, b2 ̾Ƴ char Լ ߽ϴ.


 



IML ڵ Է Ϸϸ Ͱ ˴ϴ.



̷ IML ȸ ǽغҽϴ.





4. Visualization



Ư, ȸ ׷ / غ ð ڽϴ .



/* ȸͽ */



start PolyRegScore(x, coef);


p = nrow(coef);


y = j(nrow(x), 1, coef[p]); /* Y */


do j = p-1 to 1 by -1;


y = y # x + coef[j];


end;


return(y);


finish;



׷ ϰ Yް ϴ Ľ ҽϴ.


t = T( do(min(x), max(x), (max(x)-min(x))/25) ); /* X Uniform Grid */


Yhat = j(nrow(t), 3);


do d = 0 to 2;


b = PolyRegEst(Y, X, w, d); /* PolyRegEst ҷ*/


Yhat[,d+1] = PolyRegScore(t, b); /* PolyRegScore */


end;


T ֽϴ. T Uniform Grid ϴ մϴ. Uniform grid ״ ε, , x ϰ 26(25 ) ɰִ մϴ. غ 0.264 ׿. ū ǹ̴ ϴ.


Z = t || Yhat; /* Deg 3 */


create RegFit from Z[c={"t" "Pred0" "Pred1" "Pred2"}];


append from Z;


QUIT;


t y Z Ŀ ƺҽϴ. ̷ 񱳰 ?



data RegAll; /* 񱳿 */


label w="Weight" Pred0="Weighted Mean"


Pred1="Weighted Linear Fit" Pred2="Weighted Quadratic Fit";


merge RegData RegFit;


run;


ʵͰ Regdata, ȸͽĿ ͸ ƽϴ.


ȸ ׷ ϱ ؼ Դϴ.


title "Weighted Regression Models"; /* ׷ ׸ */


proc sgplot data=RegAll;


scatter x=x y=y / filledoutlinedmarkers markerattrs=(size=12 symbol=CircleFilled)


colorresponse=w colormodel=TwoColorRamp;


series x=t y=Pred0 / curvelabel;


series x=t y=Pred1 / curvelabel;


series x=t y=Pred2 / curvelabel;


xaxis grid; yaxis grid;


run;


 

 

PROC SGPLOT Series ׷ ַ մϴ. deg0,1,2 Pred0,1,2 غڽϴ. ׷ ռ Ȯ ְڳ׿.

 

 

 

ȸ, ȸ, ׷ Ǿϴ. ȸͰ ̻ڰ յǾ.

 

е鵵 ̷ ų پ м ϰ ñ ٶϴ.

 

 

5. Summary

 

 

ȸ ϸ鼭, ̵ Խñ ǰڳ׿. Ȯ PROC REG PROC SGPLOT ̿ ȸ ϴ. ޺м ϸ, μ Ķ ̳ н ϴ ֽϴ. ̷ IML ܰ辿 ϴ Ű м Ű Դϴ.

 

 

, ̹ ȸʹ ǵ͸ м ϰ ֽϴ. ġ Ŭ ġ ˾ ϴ. ð Kernel Regression ٷﺸڽϴ.