R語(yǔ)言多元Logistic邏輯回歸應(yīng)用實(shí)例
可以使用逐步過(guò)程確定多元邏輯回歸。此函數(shù)選擇模型以最小化AIC。
如何進(jìn)行多元邏輯回歸
可以使用階梯函數(shù)通過(guò)逐步過(guò)程確定多元邏輯回歸。此函數(shù)選擇模型以最小化AIC。
通常建議不要盲目地遵循逐步程序,而是要使用擬合統(tǒng)計(jì)(AIC,AICc,BIC)比較模型,或者根據(jù)生物學(xué)或科學(xué)上合理的可用變量建立模型。
多元相關(guān)是研究潛在自變量之間關(guān)系的一種工具。例如,如果兩個(gè)獨(dú)立變量彼此相關(guān),可能在最終模型中都不需要這兩個(gè)變量,但可能有理由選擇一個(gè)變量而不是另一個(gè)變量。
多元相關(guān)
創(chuàng)建數(shù)值變量的數(shù)據(jù)框
Data.num $ Status = as.numeric(Data.num $ Status) Data.num $ Length = as.numeric(Data.num $ Length) Data.num $ Migr = as.numeric(Data.num $ Migr) Data.num $ Insect = as.numeric(Data.num $ Insect) Data.num $ Diet = as.numeric(Data.num $ Diet) Data.num $ Broods = as.numeric(Data.num $ Broods) Data。 num $ Wood = as.numeric(Data.num $ Wood) Data.num $ Upland = as.numeric(Data.num $ Upland) Data.num $ Water = as.numeric(Data.num $ Water) Data.num $ Release = as.numeric(Data.num $ Release) Data.num $ Indiv = as.numeric(Data.num $ Indiv) ###檢查新數(shù)據(jù)框 headtail(Data.num) 1 1 1520 9600.0 1.21 1 12 2 6.0 1 0 0 1 6 29 2 1 1250 5000.0 0.56 1 0 1 6.0 1 0 0 1 10 85 3 1 870 3360.0 0.07 1 0 1 4.0 1 0 0 1 3 8 77 0 170 31.0 0.55 3 12 2 4.0 NA 1 0 0 1 2 78 0 210 36.9 2.00 2 8 2 3.7 1 0 0 1 1 2 79 0 225 106.5 1.20 2 12 2 4.8 2 0 0 0 1 2 ###檢查變量之間的相關(guān)性 ###這里使用了Spearman相關(guān)性

多元邏輯回歸的例子
在此示例中,數(shù)據(jù)包含缺失值。在R中缺失值用NA表示。SAS通常會(huì)無(wú)縫地處理缺失值。雖然這使用戶更容易,但可能無(wú)法確保用戶了解這些缺失值的作用。在某些情況下,R要求用戶明確如何處理缺失值。處理多元回歸中的缺失值的一種方法是從數(shù)據(jù)集中刪除具有任何缺失值的所有觀察值。這是我們?cè)谥鸩竭^(guò)程之前要做的事情,創(chuàng)建一個(gè)名為Data.omit的數(shù)據(jù)框。但是,當(dāng)我們創(chuàng)建最終模型時(shí),我們只想排除那些在最終模型中實(shí)際包含的變量中具有缺失值的觀察。為了測(cè)試最終模型的整體p值,繪制最終模型,或使用glm.compare函數(shù),我們將創(chuàng)建一個(gè)名為Data.final的數(shù)據(jù)框,只排除那些觀察結(jié)果。
盡管二項(xiàng)式和poission系列中的模型應(yīng)該沒(méi)問(wèn)題,但是對(duì)于使用某些glm擬合的步驟過(guò)程存在一些注意事項(xiàng)。
用逐步回歸確定模型
最終模型
summary(model.final)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.5496482 2.0827400 -1.704 0.088322 .
Upland -4.5484289 2.0712502 -2.196 0.028093 *
Migr -1.8184049 0.8325702 -2.184 0.028956 *
Mass 0.0019029 0.0007048 2.700 0.006940 **
Indiv 0.0137061 0.0038703 3.541 0.000398 ***
Insect 0.2394720 0.1373456 1.744 0.081234 .
Wood 1.8134445 1.3105911 1.384 0.166455
偽R方
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.700475
Cox and Snell (ML) 0.637732
Nagelkerke (Cragg and Uhler) 0.833284
模型總體p值
在最終模型中創(chuàng)建包含變量的數(shù)據(jù)框,并省略NA。
偏差表分析
Analysis of Deviance Table Model 1: Status ~ Upland + Migr + Mass + Indiv + Insect + Wood Model 2: Status ~ 1 Resid. Df Resid. Dev Df Deviance Pr(>Chi) 1 63 30.392 2 69 93.351 -6 -62.959 1.125e-11 ***
似然比檢驗(yàn)
Likelihood ratio test #Df LogLik Df Chisq Pr(>Chisq) 1 7 -15.196 2 1 -46.675 -6 62.959 1.125e-11 ***
標(biāo)準(zhǔn)化殘差圖

簡(jiǎn)單的預(yù)測(cè)值圖
在最終模型中創(chuàng)建包含變量的數(shù)據(jù)框,并在NA中省略

過(guò)度離散檢驗(yàn)
過(guò)度離散是glm的deviance殘差相對(duì)于自由度較大的情況。這些值顯示在模型的摘要中。一個(gè)指導(dǎo)原則是,如果deviance殘差與剩余自由度的比率超過(guò)1.5,則模型過(guò)度離散。過(guò)度離散表明模型不能很好地?cái)M合數(shù)據(jù):解釋變量可能無(wú)法很好地描述因變量,或者可能無(wú)法為這些數(shù)據(jù)正確指定模型。如果存在過(guò)度離散,一種可能的解決方案是 在glm中使用quasibinomial family選項(xiàng)。
Null deviance: 93.351 on 69 degrees of freedom Residual deviance: 30.392 on 63 degrees of freedom deviance / df.residual [1] 0.482417
評(píng)估模型的替代方法
使用逐步程序的替代或補(bǔ)充是將模型與擬合統(tǒng)計(jì)進(jìn)行比較。我的compare.glm 函數(shù)將為glm模型顯示AIC,AICc,BIC和偽R平方。使用的模型應(yīng)該都擬合相同的數(shù)據(jù)。也就是說(shuō),如果數(shù)據(jù)集中的不同變量包含缺失值,則應(yīng)該謹(jǐn)慎使用。如果您對(duì)使用哪種擬合統(tǒng)計(jì)數(shù)據(jù)沒(méi)有任何偏好,您希望在最終模型中使用較少的術(shù)語(yǔ),我可能會(huì)推薦AICc或BIC。
一系列模型可以與標(biāo)準(zhǔn)的anova 功能進(jìn)行比較。模型應(yīng)嵌套在先前模型中或anova函數(shù)列表中的下一個(gè)模型中; 和模型應(yīng)該擬合相同的數(shù)據(jù)。在比較多個(gè)回歸模型時(shí),通常放寬p值為0.10或0.15。
在以下示例中,使用通過(guò)逐步過(guò)程選擇的模型。請(qǐng)注意,雖然模型9最小化了AIC和AICc,但模型8最小化了BIC。anova結(jié)果表明模型8不是對(duì)模型7的顯著改進(jìn)。這些結(jié)果支持選擇模型7,8或9中的任何一個(gè)。
compareGLM(model.1, model.2, model.3, model.4, model.5, model.6,
model.7, model.8, model.9)
$Models
Formula
1 "Status ~ 1"
2 "Status ~ Release"
3 "Status ~ Release + Upland"
4 "Status ~ Release + Upland + Migr"
5 "Status ~ Release + Upland + Migr + Mass"
6 "Status ~ Release + Upland + Migr + Mass + Indiv"
7 "Status ~ Release + Upland + Migr + Mass + Indiv + Insect"
8 "Status ~ Upland + Migr + Mass + Indiv + Insect"
9 "Status ~ Upland + Migr + Mass + Indiv + Insect + Wood"
$Fit.criteria
Rank Df.res AIC AICc BIC McFadden Cox.and.Snell Nagelkerke p.value
1 1 66 94.34 94.53 98.75 0.0000 0.0000 0.0000 Inf
2 2 65 62.13 62.51 68.74 0.3787 0.3999 0.5401 2.538e-09
3 3 64 56.02 56.67 64.84 0.4684 0.4683 0.6325 3.232e-10
4 4 63 51.63 52.61 62.65 0.5392 0.5167 0.6979 7.363e-11
5 5 62 50.64 52.04 63.87 0.5723 0.5377 0.7263 7.672e-11
6 6 61 49.07 50.97 64.50 0.6118 0.5618 0.7588 5.434e-11
7 7 60 46.42 48.90 64.05 0.6633 0.5912 0.7985 2.177e-11
8 6 61 44.71 46.61 60.14 0.6601 0.5894 0.7961 6.885e-12
9 7 60 44.03 46.51 61.67 0.6897 0.6055 0.8178 7.148e-12
Analysis of Deviance Table
Model 1: Status ~ 1
Model 2: Status ~ Release
Model 3: Status ~ Release + Upland
Model 4: Status ~ Release + Upland + Migr
Model 5: Status ~ Release + Upland + Migr + Mass
Model 6: Status ~ Release + Upland + Migr + Mass + Indiv
Model 7: Status ~ Release + Upland + Migr + Mass + Indiv + Insect
Model 8: Status ~ Upland + Migr + Mass + Indiv + Insect
Model 9: Status ~ Upland + Migr + Mass + Indiv + Insect + Wood
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 66 90.343
2 65 56.130 1 34.213 4.94e-09 ***
3 64 48.024 1 8.106 0.004412 **
4 63 41.631 1 6.393 0.011458 *
5 62 38.643 1 2.988 0.083872 .
6 61 35.070 1 3.573 0.058721 .
7 60 30.415 1 4.655 0.030970 *
8 61 30.710 -1 -0.295 0.587066
9 60 28.031 1 2.679 0.101686
總結(jié)
到此這篇關(guān)于R語(yǔ)言多元Logistic邏輯回歸應(yīng)用的文章就介紹到這了,更多相關(guān)R語(yǔ)言多元邏輯回歸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R語(yǔ)言變量級(jí)別的數(shù)據(jù)處理操作
這篇文章主要介紹了R語(yǔ)言變量級(jí)別的數(shù)據(jù)處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
R語(yǔ)言中邏輯回歸知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家總結(jié)了關(guān)于R語(yǔ)言中邏輯回歸知識(shí)點(diǎn)相關(guān)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2021-05-05
解決R語(yǔ)言 數(shù)據(jù)不平衡的問(wèn)題
這篇文章主要介紹了解決R語(yǔ)言 數(shù)據(jù)不平衡的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
R語(yǔ)言數(shù)據(jù)類型與相應(yīng)運(yùn)算的實(shí)現(xiàn)
本文主要介紹了R語(yǔ)言數(shù)據(jù)類型與相應(yīng)運(yùn)算的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
R語(yǔ)言ggplot2設(shè)置圖例(legend)的操作大全
ggplot2是一個(gè)繪制可視化圖形的R包,汲取了R語(yǔ)言基礎(chǔ)繪圖系統(tǒng)(graphics)和l?attice包的優(yōu)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于R語(yǔ)言ggplot2設(shè)置圖例(legend)的操作大全,需要的朋友可以參考下2022-07-07
R語(yǔ)言兩組變量特征相關(guān)關(guān)系熱圖繪制畫(huà)法
本文為大家介紹了如何畫(huà)兩組變量(特征)的相關(guān)關(guān)系熱圖的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
R包c(diǎn)lusterProfiler如何安裝成功(新手必看!)
最近在我以為ClusterProfiler已經(jīng)安裝好的時(shí)候,又遇到了一些問(wèn)題,所以這篇文章主要給大家介紹了關(guān)于R包c(diǎn)lusterProfiler如何安裝成功的相關(guān)資料,需要的朋友可以參考下2023-02-02

