评价预测变量对结果几率的影响

并非很理解把,《R语言实战》第2版 ,p287函数

对于大多数来讲,以几率的方式思考比使用优点比更直观。使用 predict() 函数,能够观察某个预测变量在各个水平是对结果几率的影响。首先建立一个包含感兴趣预测变量值的虚拟数据集,而后对该数据集使用 predict() 以预测这些值的结果几率测试

咱们使用该方法评价婚姻评分对婚外情几率的影响。首先,建立一个虚拟数据集,设定年龄、婚龄、宗教信仰为他们的均值,婚姻评分的范围为1~5code

> 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

接下来使用测试数据集预测相应的几率it

> 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(假定年龄、婚姻和宗教信仰不变)。下面再看看年龄的影响:io

> 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。利用该方法,你能够探究每一个预测变量对结果几率的影响test

相关文章
相关标签/搜索