并不是很理解把,《R语言实战》第2版 ,p287
对于大多数来说,以概率的方式思考比使用优势比更直观。使用 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$prob <- predict(fit.reduced, newdata=testdata, type="response")
> testdata
rating age yearsmarried religiousness prob
1 1 32.48752 8.177696 3.116473 0.5302296
2 2 32.48752 8.177696 3.116473 0.4157377
3 3 32.48752 8.177696 3.116473 0.3096712
4 4 32.48752 8.177696 3.116473 0.2204547
5 5 32.48752 8.177696 3.116473 0.1513079
接下来使用测试数据集预测相应的概率
> testdata$prob <- predict(fit.reduced, newdata=testdata, type="response")
> testdata
rating age yearsmarried religiousness prob
1 3.93178 17 8.177696 3.116473 0.3350834
2 3.93178 27 8.177696 3.116473 0.2615373
3 3.93178 37 8.177696 3.116473 0.1992953
4 3.93178 47 8.177696 3.116473 0.1488796
5 3.93178 57 8.177696 3.116473 0.1094738
从这些结果可以看到,当婚姻评分从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$prob <- predict(fit.reduced, newdata=testdata, type="response")
> testdata
rating age yearsmarried religiousness prob
1 3.93178 17 8.177696 3.116473 0.3350834
2 3.93178 27 8.177696 3.116473 0.2615373
3 3.93178 37 8.177696 3.116473 0.1992953
4 3.93178 47 8.177696 3.116473 0.1488796
5 3.93178 57 8.177696 3.116473 0.1094738
此处可以看到,当其他变量不变,年龄从17增加到57时,婚外情的概率将从0.34降低到0.11。利用该方法,你可以探究每个预测变量对结果概率的影响