有一个SAS帮助里的例子,某校7/8/9年级共4000名学生,想了解家庭收入X1、家庭子女数X2对每周冰激凌消费量(Y)的影响。如果采用简单随机抽样,从中抽取40名学生,可以调用proc surveyreg data=IceCream total=4000;命令来做。
data IceCream;
input Grade Spending Income Kids @@;
datalines;
7 7 39 2 7 7 38 1 8 12 47 1
9 10 47 4 7 1 34 4 7 10 43 2
7 3 44 4 8 20 60 3 8 19 57 4
7 2 35 2 7 2 36 1 9 15 51 1
8 16 53 1 7 6 37 4 7 6 41 2
7 6 39 2 9 15 50 4 8 17 57 3
8 14 46 2 9 8 41 2 9 8 41 1
9 7 47 3 7 3 39 3 7 12 50 2
7 4 43 4 9 14 46 3 8 18 58 4
9 9 44 3 7 2 37 1 7 1 37 2
7 4 44 2 7 11 42 2 9 8 41 2
8 10 42 2 8 13 46 1 7 2 40 3
9 6 45 1 9 11 45 4 7 2 36 1
7 9 46 1
;
run;
proc surveyreg data=IceCream total=4000;
class Kids;
model Spending = Income Kids / solution anova;
run;
如果采用分层抽样,分别从7/8/9年级中抽取部分学生,可以调用proc surveyreg ,并按如下命令来做。
data StudentTotals;
input Grade _TOTAL_;
datalines;
7 1824
8 1025
9 1151
;
data IceCream;
set IceCream;
if Grade=7 then Prob=20/1824;
if Grade=8 then Prob=9/1025;
if Grade=9 then Prob=11/1151;
Weight=1/Prob;
proc surveyreg data=IceCream total=StudentTotals;
strata Grade /list;
class Kids;
model Spending = Income Kids / solution anova;
weight Weight;
run;
也可以尝试对上面例子用stata重新运算。