R語言邏輯回歸、方差分析 、偽R平方分析
原文鏈接:http://tecdat.cn/?p=9589
?
目錄
怎么做測試
假設(shè)條件
并非所有比例或計(jì)數(shù)都適用于邏輯回歸分析
過度分散
偽R平方
測試p值
Logistic回歸示例
模型擬合
系數(shù)和指數(shù)系數(shù)
方差分析?
偽R平方
模型的整體p值
標(biāo)準(zhǔn)化殘差圖
繪制模型
Logistic回歸示例
模型擬合
系數(shù)和指數(shù)系數(shù)
方差分析
偽R平方
模型的整體p值
標(biāo)準(zhǔn)化殘差圖
繪制模型
Logistic回歸示例
怎么做測試
Logistic回歸可以使用glm??(廣義線性模型)函數(shù)在R中執(zhí)行??。該函數(shù)使用鏈接函數(shù)來確定要使用哪種模型,例如邏輯模型,概率模型或泊松模型。?
假設(shè)條件
廣義線性模型的假設(shè)少于大多數(shù)常見的參數(shù)檢驗(yàn)。觀測值仍然需要獨(dú)立,并且需要指定正確的鏈接函數(shù)。因此,例如應(yīng)該了解何時使用泊松回歸以及何時使用邏輯回歸。但是,不需要數(shù)據(jù)或殘差的正態(tài)分布。
?
并非所有比例或計(jì)數(shù)都適用于邏輯回歸分析
一個不采用邏輯回歸的例子中,飲食研究中人們減肥的體重?zé)o法用初始體重的比例來解釋作為“成功”和“失敗”的計(jì)數(shù)。在這里,只要滿足模型假設(shè),就可以使用常用的參數(shù)方法。
?
過度分散
使用廣義線性模型時要注意的一個潛在問題是過度分散。當(dāng)模型的殘余偏差相對于殘余自由度較高時,就會發(fā)生這種情況。這基本上表明該模型不能很好地擬合數(shù)據(jù)。?
?
但是據(jù)我了解,從技術(shù)上講,過度分散對于簡單的邏輯回歸而言不是問題,即具有二項(xiàng)式因果關(guān)系和單個連續(xù)自變量的問題。
?
偽R平方
對于廣義線性模型(glm),R不產(chǎn)生r平方值。pscl??包中的??pR2? 可以產(chǎn)生偽R平方值。
?
測試p值
檢驗(yàn)邏輯對數(shù)或泊松回歸的p值使用卡方檢驗(yàn)。方差分析??來測試每一個系數(shù)的顯著性。似然比檢驗(yàn)也可以用來檢驗(yàn)整體模型的重要性。
?
Logistic回歸示例
?
Data = read.table(textConnection(Input),header=TRUE)
Data$Total = Data$mpi90 + Data$mpi100
Data$Percent = Data$mpi100 / + Data$Total
模型擬合
?
Trials = cbind(Data$mpi100, Data$mpi90) ? ? ? ? # Sucesses, Failures
model = glm(Trials ~ Latitude,
data = Data,
family = binomial(link="logit"))
系數(shù)和指數(shù)系數(shù)
?
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -7.64686 ? ?0.92487 ?-8.268 ? <2e-16 ***
Latitude ? ? 0.17864 ? ?0.02104 ? 8.490 ? <2e-16 ***
2.5 % ? ? 97.5 %
(Intercept) -9.5003746 -5.8702453
Latitude ? ? 0.1382141 ?0.2208032
# exponentiated coefficients
(Intercept) ? ? Latitude
0.0004775391 1.1955899446
# 95% CI for exponentiated coefficients
2.5 % ? ? ?97.5 %
(Intercept) 7.482379e-05 0.002822181
Latitude ? ?1.148221e+00 1.247077992
方差分析?
Analysis of Deviance Table (Type II tests)
Response: Trials
Df ?Chisq Pr(>Chisq)
Latitude ? 1 72.076 ?< 2.2e-16 ***
?
偽R平方
?
$Models
Model: "glm, Trials ~ Latitude, binomial(link = \"logit\"), Data"
Null: ?"glm, Trials ~ 1, binomial(link = \"logit\"), Data"
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.425248
Cox and Snell (ML) ? ? ? ? ? ? ? ? ? 0.999970
Nagelkerke (Cragg and Uhler) ? ? ? ? 0.999970
?
模型的整體p值
?
Analysis of Deviance Table
Model 1: Trials ~ Latitude
Model 2: Trials ~ 1
Resid. Df Resid. Dev Df Deviance ?Pr(>Chi)
1 ? ? ? ? 6 ? ? 70.333
2 ? ? ? ? 7 ? ?153.633 -1 ?-83.301 < 2.2e-16 ***
Likelihood ratio test
Model 1: Trials ~ Latitude
Model 2: Trials ~ 1
#Df ?LogLik Df ?Chisq Pr(>Chisq)
1 ? 2 -56.293
2 ? 1 -97.944 -1 83.301 ?< 2.2e-16 ***
?
標(biāo)準(zhǔn)化殘差圖
?
?
?

?
標(biāo)準(zhǔn)化殘差與預(yù)測值的關(guān)系圖。殘差應(yīng)無偏且均等。?
?
繪制模型
?
?
?

?
?
?
Logistic回歸示例
?
Data = read.table(textConnection(Input),header=TRUE)
?
模型擬合
?
model
?
系數(shù)和指數(shù)系數(shù)
?
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) ?4.41379 ? ?6.66190 ? 0.663 ? ?0.508
Height ? ? ?-0.05016 ? ?0.09577 ?-0.524 ? ?0.600
2.5 % ? ? 97.5 %
(Intercept) -8.4723648 18.4667731
Height ? ? ?-0.2498133 ?0.1374819
# exponentiated coefficients
(Intercept) ? ? ?Height
82.5821122 ? 0.9510757
# 95% CI for exponentiated coefficients
2.5 % ? ? ? 97.5 %
(Intercept) 0.0002091697 1.047171e+08
Height ? ? ?0.7789461738 1.147381e+0
?
方差分析
?
Analysis of Deviance Table (Type II tests)
Response: Insect
Df ?Chisq Pr(>Chisq)
Height ? ? 1 0.2743 ? ? 0.6004
Residuals 23
?
偽R平方
?
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden ? ? ? ? ? ? ? ? ? ? ? ? ? 0.00936978
Cox and Snell (ML) ? ? ? ? ? ? ? ? 0.01105020
Nagelkerke (Cragg and Uhler) ? ? ? 0.01591030
?
模型的整體p值
?
Analysis of Deviance Table
Model 1: Insect ~ Height
Model 2: Insect ~ 1
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 ? ? ? ?23 ? ? 29.370
2 ? ? ? ?24 ? ? 29.648 -1 -0.27779 ? 0.5982
Likelihood ratio test
Model 1: Insect ~ Height
Model 2: Insect ~ 1
#Df ?LogLik Df ?Chisq Pr(>Chisq)
1 ? 2 -14.685
2 ? 1 -14.824 -1 0.2778 ? ? 0.5982
標(biāo)準(zhǔn)化殘差圖
?
?
?
?

繪制模型
?
Height Insect Insect.num
1 ? ? ?62 beetle ? ? ? ? ?0
2 ? ? ?66 ?other ? ? ? ? ?1
3 ? ? ?61 beetle ? ? ? ? ?0
23 ? ? 72 ?other ? ? ? ? ?1
24 ? ? 70 beetle ? ? ? ? ?0
25 ? ? 74 ?other ? ? ? ? ?1
?
?
?

?
Height Insect Insect.num Insect.log
1 ? ? ?62 beetle ? ? ? ? ?0 ? ? ?FALSE
2 ? ? ?66 ?other ? ? ? ? ?1 ? ? ? TRUE
3 ? ? ?61 beetle ? ? ? ? ?0 ? ? ?FALSE
23 ? ? 72 ?other ? ? ? ? ?1 ? ? ? TRUE
24 ? ? 70 beetle ? ? ? ? ?0 ? ? ?FALSE
25 ? ? 74 ?other ? ? ? ? ?1 ? ? ? TRUE
?

?
Logistic回歸示例
?
Data = read.table(textConnection(Input),header=TRUE)
model
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -66.4981 ? ?32.3787 ?-2.054 ? 0.0400 *
Continuous ? ?0.9027 ? ? 0.4389 ? 2.056 ? 0.0397 *
Analysis of Deviance Table (Type II tests)
Response: Factor
Df Chisq Pr(>Chisq)
Continuous ?1 4.229 ? ?0.03974 *
Residuals ?27
Pseudo.R.squared
McFadden ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.697579
Cox and Snell (ML) ? ? ? ? ? ? ? ? ? 0.619482
Nagelkerke (Cragg and Uhler) ? ? ? ? 0.826303
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 ? ? ? ?27 ? ? 12.148
2 ? ? ? ?28 ? ? 40.168 -1 ? -28.02 ?1.2e-07 ***

?
將因子轉(zhuǎn)換為數(shù)字變量,級別為0和1?
?
Continuous Factor Factor.num
1 ? ? ? ? ?62 ? ? ?A ? ? ? ? ?0
2 ? ? ? ? ?63 ? ? ?A ? ? ? ? ?0
3 ? ? ? ? ?64 ? ? ?A ? ? ? ? ?0
27 ? ? ? ? 84 ? ? ?B ? ? ? ? ?1
28 ? ? ? ? 85 ? ? ?B ? ? ? ? ?1
29 ? ? ? ? 86 ? ? ?B ? ? ? ? ?1
?
?

?
將Factor轉(zhuǎn)換為邏輯變量,級別為TRUE和FALSE?
?
Continuous Factor Factor.num Factor.log
1 ? ? ? ? ?62 ? ? ?A ? ? ? ? ?0 ? ? ?FALSE
2 ? ? ? ? ?63 ? ? ?A ? ? ? ? ?0 ? ? ?FALSE
3 ? ? ? ? ?64 ? ? ?A ? ? ? ? ?0 ? ? ?FALSE
27 ? ? ? ? 84 ? ? ?B ? ? ? ? ?1 ? ? ? TRUE
28 ? ? ? ? 85 ? ? ?B ? ? ? ? ?1 ? ? ? TRUE
29 ? ? ? ? 86 ? ? ?B ? ? ? ? ?1 ? ? ? TRUE
