對于我們大多數(shù)人來說,以概率的方式思考比使用優(yōu)勢比更直觀。使用predict()函數(shù),可
以觀察某個預測變量在各個水平時對結果概率的影響。首先創(chuàng)建一個包含你感興趣預測變量值的
虛擬數(shù)據(jù)集,然后對該數(shù)據(jù)集使用predict()函數(shù),以預測這些值的結果概率。
現(xiàn)在我們使用該方法評價婚姻評分對婚外情概率的影響。首先,創(chuàng)建一個虛擬數(shù)據(jù)集,設定
年齡、婚齡和宗教信仰為它們的均值,婚姻評分的范圍為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
接下來,使用測試數(shù)據(jù)集預測相應的概率:
> 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(很不幸福)變?yōu)?(非常幸福)時,婚外情概率從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。
利用該方法,你可探究每一個預測變量對結果概率的影響。








暫無數(shù)據(jù)