2013年11月14日 星期四

多層次模型 (Multilevel Model)

多層次模型 (Multilevel Model)
簡單介紹與使用SAS處理Multilevel Model,
本篇使用到三個比較常用的簡單模型,分別為The Variance components model、The Random intercept model、The Random slope model

SAS Codes
LIBNAME lib EXCEL "D:\SasData\jsp.XLSX" ;
data jsp; set lib."jsp2$"N; run;

ods graphics on;
/*The_variance_components_model*/
Proc mixed  data=jsp  method= ml noclprint covtest;
Class Gender  Social_class  School_ID;
Model  math_yr_3=   /s ddfm=bw  OUTP=p1  OUTPM=pm1   RESIDUAL;
Random intercept    /type=un   sub=School_ID;
Run;
/*畫圖*/
goptions reset=global;
axis1 label =( f= "arial/bo" h=1.2 "8-year math score")
order = (0 to 45 by 5);
axis2 label =(a=90 f= "arial/bo" h=1.2 "11-year math score")
order = (26 to 37 by 5);
title "The variance components model";
symbol i=j r=50;
proc gplot data = p1;
where school_id<51;
plot pred*math_yr_1=school_id/ haxis=axis1 vaxis=axis2;
run;
quit;

/*Random_intercept_model*/
Proc mixed  data=jsp  method= ml noclprint covtest;
Class Gender  Social_class  School_ID;
Model  math_yr_3=math_yr_1  /s ddfm=bw OUTP=p2  OUTPM=pm2   RESIDUAL;
Random intercept  /type=un
sub=School_ID;
Run;
/*畫圖*/
goptions reset=global;
axis1 label =( f= "arial/bo" h=1.2 "8-year math score")
order = (0 to 45 by 5);
axis2 label =(a=90 f= "arial/bo" h=1.2 "11-year math score")
order = (0 to 45 by 5);
title "The Random Intercept Model";
symbol i=j r=50;
proc gplot data = p2;
where school_id<51;
plot pred*math_yr_1=school_id/ haxis=axis1 vaxis=axis2;
run;
quit;

/*Random_Slope_model*/
Proc mixed  data=jsp  method= ml noclprint covtest;
      Class Gender  Social_class  School_ID;
      Model  math_yr_3=math_yr_1  /s ddfm=bw    OUTP=p3  OUTPM=pm3   RESIDUAL;
      Random intercept  math_yr_1  /type=un   sub=School_ID; 
Run;
proc print data=pm3;
run;
/*畫圖*/
goptions reset=global;
axis1 label =( f= "arial/bo" h=1.2 "8-year math score")
order = (0 to 45 by 5);
axis2 label =(a=90 f= "arial/bo" h=1.2 "11-year math score")
order = (0 to 45 by 5);
title "The Random Coefficient Model";
symbol i=j r=50;
proc gplot data=p3;
where school_id<51 ;
plot pred*math_yr_1=school_id/ haxis=axis1 vaxis=axis2 href=0;
run;
quit;

縱貫性資料畫圖 (Longitudinal data: plots of individual profiles)

縱貫性資料畫圖 (Longitudinal data: plots of individual profiles)
當我們面對Longitudinal data時,想看outcome(Y)與共變數(X)時間或是年齡的探索圖,
SAS提供一個簡單清楚的package。
這裡舉一個例子,有一群人分成三個組別,Outcome是檢測時受試者一分鐘可以做幾下仰臥起坐,age是受試時的年齡。每個人分別有三次的檢查,若你的資料有Missing值或是每個人檢查的次數不同,使用這個Package也能幫你做圖,不同individual的ID也能印出,當有極端值或是pattern較特殊的individual,能簡單的找出來。

SAS Codes
/*資料輸入*/
data a;
/*當變數為文字變相非數字變相時,要在文字變相後面加上一個"$"*/
/*當我們要在同一行輸入多筆資料時,要加上"@@"*/
input y  age  group ID $  @@;
datalines;
30 21 1  A123    32 23  1  A123    40  27 1  A123
45 24 1  B123    47 27  1   B123    44 29  1  B123
41 23 2  C123    46 25  2   C123    40  27 2  C123
27 22 2  D123    33 25  2   D123   34  29  2  D123
50 24 2  E123    46 27  2    E123    48  31 2  E123
35 21 3  F123    41 25  3    F123    39 27  3  F123
45 24 3  G123   47 28  3    G123   44 32  3  G123
;
run;
proc print; run;
 
/*使用columns來顯示*/
Proc SGpanel data = a;
/*group 請填入受試者分組的變相   3請填入組別的總數*/
PanelBy group/ columns=3;
/*age請填入時間或是年齡變相  y請填入Outcome      ID請填入individual辨別的變相,例如每個人有不同的身分之號、學號、員工編號*/
series x=age  y=y / group =ID
LineAttrs= (pattern=1 );
TITLE1  "三組隨年齡增加,體重變化的圖(columns)";
label y=體重;
run;
 
/*使用rows來顯示*/
Proc SGpanel data = a;
/*group 請填入受試者分組的變相   3請填入組別的總數*/
PanelBy group/ columns=1;
/*age請填入時間或是年齡變相  y請填入Outcome      ID請填入individual辨別的變相,例如每個人有不同的身分之號、學號、員工編號*/
series x=age  y=y / group =ID
LineAttrs= (pattern=1 );
TITLE1  "三組隨年齡增加,體重變化的圖(rows)";
label y=體重;
run;
 

/*各組自己畫一個圖*/
/*若資料沒經過排序,要先使用proc sort排序*/
proc sort data=a; by group; run;
Proc SGpanel data = a;
/*group 請填入受試者分組的變相   3請填入組別的總數*/
PanelBy group/ columns=1;
/*age請填入時間或是年齡變相  y請填入Outcome      ID請填入individual辨別的變相,例如每個人有不同的身分之號、學號、員工編號*/
series x=age  y=y / group =ID
LineAttrs= (pattern=1 );
TITLE1  "三組隨年齡增加,體重變化的圖(rows)";
label y=體重;
/*分別畫圖要加by的宣告*/
by group;
run;
 


/*如過要調整圖的長寬,可以在Proc SGpanel前後加入以下宣告*/
ODS GRAPHICS  /  MAXLEGENDAREA=100   HEIGHT=1500    WIDTH=1500  ;

ods pdf close;