对于我们大多数人来说,以概率的方式思考比使用优势比更直观。使用predict()函数,可
以观察某个预测变量在各个水平时对结果概率的影响。首先创建一个包含你感兴趣预测变量值的
虚拟数据集,然后对该数据集使用predict()函数,以预测这些值的结果概率。
现在我们使用该方法评价婚姻评分对婚外情概率的影响。首先,创建一个虚拟数据集,设定
年龄、婚龄和宗教信仰为它们的均值,婚姻评分的范围为1~5。
> testdata <- data.frame(rating=c(1, 2, 3, 4, 5), age=mean(Affairs$age),
yearsmarried=mean(Affairs$yearsmarried),
religiousness=mean(Affairs$religiousness))
> testdata
rating age yearsmarried religiousness
1 1 32.5 8.18 3.12
2 2 32.5 8.18 3.12
3 3 32.5 8.18 3.12
4 4 32.5 8.18 3.12
5 5 32.5 8.18 3.12
接下来,使用测试数据集预测相应的概率:
> testdata$prob <- predict(fit.reduced, newdata=testdata, type="response")
testdata
rating age yearsmarried religiousness prob
1 1 32.5 8.18 3.12 0.530
2 2 32.5 8.18 3.12 0.416
3 3 32.5 8.18 3.12 0.310
4 4 32.5 8.18 3.12 0.220
5 5 32.5 8.18 3.12 0.151
从这些结果可以看到,当婚姻评分从1(很不幸福)变为5(非常幸福)时,婚外情概率从0.53
降低到了0.15(假定年龄、婚龄和宗教信仰不变)。下面我们再看看年龄的影响:
> testdata <- data.frame(rating=mean(Affairs$rating),
age=seq(17, 57, 10),
yearsmarried=mean(Affairs$yearsmarried),
religiousness=mean(Affairs$religiousness))
> testdata
rating age yearsmarried religiousness
1 3.93 17 8.18 3.12
2 3.93 27 8.18 3.12
3 3.93 37 8.18 3.12
4 3.93 47 8.18 3.12
5 3.93 57 8.18 3.12
> testdata$prob <- predict(fit.reduced, newdata=testdata, type="response")
> testdata
rating age yearsmarried religiousness prob
1 3.93 17 8.18 3.12 0.335
2 3.93 27 8.18 3.12 0.262
3 3.93 37 8.18 3.12 0.199
4 3.93 47 8.18 3.12 0.149
5 3.93 57 8.18 3.12 0.109
此处可以看到,当其他变量不变,年龄从17增加到57时,婚外情的概率将从0.34降低到0.11。
利用该方法,你可探究每一个预测变量对结果概率的影响。








暂无数据