並非很理解把,《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