数据集准备
使用R语言婚外情数据集作为分析案例,导出为 Affairs.csv,供SAS备用
R语言代码
install.packages("AER")
data(Affairs, package = "AER")
write.csv(Affairs, "Affairs.csv")
变量描述
- Affairs 数据集中共 601 个样本,即 601行,共 9 个变量,即 9 列
- affairs:出轨次数
- gender:性别
- age:年龄
- yearsmarried:结婚年限
- children:有无小孩
- religiousness:宗教信仰程度
- education:教育程度(20为满分)
- occupation:职业种类
- rating:婚姻满意度(5为满分)
明确分析意向
假设我们想探索发生婚外情的影响因素,因此确定变量 affairs 作为因变量(y),其余变量作为自变量(x1, x2, ··· x8)
SAS分析代码
创建数据集
data affairs;
input affairs gender$ age yearsmarried children$ religiousness education
occupation rating;
datalines;
0 male 37 10 no 3 18 7 4
0 female 27 4 no 4 14 6 4
0 female 32 15 yes 1 12 1 4
/* ……(省略中间数据) */
2 male 32 10 yes 2 17 6 5
2 male 22 7 yes 3 18 6 2
1 female 32 15 yes 3 14 1 5
;
run;
我们对某些变量进行转化。
- affairs 变量:在这里由于我们要做二分类 logistic 回归,而目前因变量 affairs 为定量变量,因此需要将因变量 affairs 转化为分类变量。于是,我们认为只要 affairs >=1,即出轨次数大于等于 1 次,我们则认为发生出轨,赋值为1,affairs = 0,即出轨次数为 0 ,则认为未发生出轨,赋值为0
- gender 变量:数据集内 gender 为 “male” 和 “female” ,我们要将字符转化为数字形式, “male” 赋值为 1 ,“female”赋值为 0
- children 变量:同上, “yes” 赋值为 1 , “no” 赋值为 0
- age 变量:为了练习含有哑变量的SAS logistic回归分析,需要将年龄转化为无序分类变量。将 age 变量分为 [0,20) ,[20,35) ,[35,50) ,>50 四段,分别赋值为 1,2,3,4。
data affairs;
set affairs;
if affairs >=1 then
affairs=1;
else
affairs=0;
if gender="male" then
newgender=1;
else
newgender=0;
if children="yes" then
newchildren=1;
else
newchildren=0;
if age >=0 and age < 20 then
newage=1;
else if age >=20 and age < 35 then
newage=2;
else if age >=35 and age < 50 then
newage=3;
else if age >=50 then
newage=4;
run;
单因素 logistic 回归分析
proc logistic data=affairs desc;
model affairs=newgender;
proc logistic data=affairs desc;
model affairs=newage;
proc logistic data=affairs desc;
model affairs=yearsmarried;
proc logistic data=affairs desc;
model affairs=newchildren;
proc logistic data=affairs desc;
model affairs=religiousness;
proc logistic data=affairs desc;
model affairs=education;
proc logistic data=affairs desc;
model affairs=occupation;
proc logistic data=affairs desc;
model affairs=rating;
run;
分析结果:
参数 | 自由度 | 估计 | 标准误差 | Wald卡方 | Pr > 卡方 |
---|---|---|---|---|---|
newgender | 1 | 0.2356 | 0.1888 | 1.5573 | 0.2121 |
newage | 1 | 0.0297 | 0.1474 | 0.0405 | 0.8405 |
yearsmarried | 1 | 0.0588 | 0.0172 | 11.6394 | 0.0006 |
newchildren | 1 | 0.7593 | 0.2353 | 10.4130 | 0.0013 |
religiousness | 1 | -0.2598 | 0.0821 | 10.0144 | 0.0016 |
education | 1 | 0.0186 | 0.0394 | 0.2235 | 0.6364 |
occupation | 1 | 0.0487 | 0.0528 | 0.8502 | 0.3565 |
rating | 1 | -0.5082 | 0.0847 | 36.0125 | <.0001 |
结果解读:
可以看到单因素 logistic 回归分析有统计学意义的变量有:
- yearsmarried(结婚年限)
- newchildren(有无小孩)
- religiousness(宗教信仰程度)
- rating(婚姻满意度)
无统计学意义的变量有:
- newgender(性别)
- newage(年龄)
- education(教育程度)
- occupation(职业种类)
多因素 logstic 回归分析
全模型(全变量纳入方程)
proc logistic data=affairs desc;
class newage (param=reference ref=first);
model affairs=newage yearsmarried newchildren religiousness rating;
run;
/* 此处ref也可以制定为last,即以newage1、newage2、newage3与newage4作对比 */
/* 此处ref也可以指定具体变量赋值ref="1"等价于ref=first,ref="3"等价于ref=last,
同样我们可以指定ref="2",即以newage1、newage3、newage4与newage2作对比*/
分析结果
参数 | 自由度 | 估计 | 标准误差 | Wald卡方 | Pr > 卡方 |
---|---|---|---|---|---|
Intercept | 1 | 2.7356 | 0.9945 | 7.5668 | 0.0059 |
newage2 | 1 | -1.8557 | 0.8746 | 4.5020 | 0.0339 |
newage3 | 1 | -2.5091 | 0.9333 | 7.2276 | 0.0072 |
newage4 | 1 | -3.0666 | 1.0043 | 9.3231 | 0.0023 |
yearsmarried | 1 | 0.0973 | 0.0309 | 9.9472 | 0.0016 |
newchildren | 1 | 0.3167 | 0.2915 | 1.1810 | 0.2772 |
religiousness | 1 | -0.3398 | 0.0905 | 14.1100 | 0.0002 |
rating | 1 | -0.4709 | 0.0900 | 27.3495 | <.0001 |
效应 | 点估计 | 95% Wald置信限lower | 95% Wald置信限upper |
---|---|---|---|
newage 2 vs 1 | 0.156 | 0.028 | 0.868 |
newage 3 vs 1 | 0.081 | 0.013 | 0.507 |
newage 4 vs 1 | 0.047 | 0.007 | 0.333 |
yearsmarried | 1.102 | 1.038 | 1.171 |
newchildren | 1.373 | 0.775 | 2.430 |
religiousness | 0.712 | 0.596 | 0.850 |
rating | 0.624 | 0.523 | 0.745 |
结果解读
可以看到进行多因素logistic回归后除了newchildren其余变量均有统计学意义。
而newage2、newage3、newage4表示与newage1(SAS已省略)相比有统计学意义,具体效应值可以看点估计,即OR值和OR值置信区间。
变量选择
- selection = 指定变量选择方法,如前进法(forward)、后退法 (backward)、逐步法 (stepwise)、最优子集法 (scores)等,默认为 none
- slentry = 变量选择方法为 forward 或 stepwise 时,用来指定变量入选标准,默认值为 0.05
- slstay = 变量选择方法为 backward 或 stepwise 时,用来指定变量剔除标准,默认值为 0.05
以逐步变量筛选方法 (stepwise)为例:
proc logistic data=affairs desc;
class newage (param=reference ref=first);
model affairs=newage yearsmarried newchildren religiousness rating/stepwise stb expb;
run;
/* stb 输出标准化回归系数,用于比较不同变量的作用大小 */
/* expb 该选项输出 OR值,无95%可信区间 */
分析结果
参数 | 自由度 | 估计 | 标准误差 | Wald卡方 | Pr > 卡方 | 标准化估计 | Exp(Est) |
---|---|---|---|---|---|---|---|
Intercept | 1 | 2.8850 | 0.9820 | 8.6303 | 0.0033 | 17.903 | |
newage2 | 1 | -1.8591 | 0.8705 | 4.5605 | 0.0327 | -0.4922 | 0.156 |
newage3 | 1 | -2.5553 | 0.9296 | 7.5557 | 0.0060 | -0.6316 | 0.078 |
newage4 | 1 | -3.1242 | 1.0005 | 9.7521 | 0.0018 | -0.4443 | 0.044 |
yearsmarried | 1 | 0.1128 | 0.0277 | 16.6176 | <.0001 | 0.3464 | 1.119 |
religiousness | 1 | -0.3387 | 0.0904 | 14.0344 | 0.0002 | -0.2180 | 0.713 |
rating | 1 | -0.4777 | 0.0898 | 28.2770 | <.0001 | -0.2906 | 0.620 |
结果解读
略
注事事项
-
结果解释时要注意回归系数的正负号不要弄反。
例如婚姻满意度与出轨次数是正比还是反比呢?回归系数是 -0.4777,说明是反比,那么如何确定关系没有弄反呢?- 一种方法是:我推荐在分析之前将自变量与因变量做相关性分析,观察自变量与因变量方向。
- 另一种方法是:还可以看SAS分析结果中
建模的概率为 affairs=1
说明我们的分析预测 affairs=1 发生的情况。
区分好回归系数,标准回归系数以及e(βi)的区别。