多層次模型 (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;