r语言中残差与回归值的残差图_R语言逻辑回归、方差分析、伪R平方分析
原標題:R語言邏輯回歸、方差分析、偽R平方分析
原文鏈接:http://tecdat.cn/?p=9589
目錄
怎么做測試
假設條件
并非所有比例或計數都適用于邏輯回歸分析
過度分散
偽R平方
測試p值
Logistic回歸示例
模型擬合
系數和指數系數
方差分析
偽R平方
模型的整體p值
標準化殘差圖
繪制模型
Logistic回歸示例
模型擬合
系數和指數系數
方差分析
偽R平方
模型的整體p值
標準化殘差圖
繪制模型
Logistic回歸示例
怎么做測試
Logistic回歸可以使用glm (廣義線性模型)函數在R中執行 。該函數使用鏈接函數來確定要使用哪種模型,例如邏輯模型,概率模型或泊松模型。
假設條件
廣義線性模型的假設少于大多數常見的參數檢驗。觀測值仍然需要獨立,并且需要指定正確的鏈接函數。因此,例如應該了解何時使用泊松回歸以及何時使用邏輯回歸。但是,不需要數據或殘差的正態分布。
并非所有比例或計數都適用于邏輯回歸分析
一個不采用邏輯回歸的例子中,飲食研究中人們減肥的體重無法用初始體重的比例來解釋作為“成功”和“失敗”的計數。在這里,只要滿足模型假設,就可以使用常用的參數方法。
過度分散
使用廣義線性模型時要注意的一個潛在問題是過度分散。當模型的殘余偏差相對于殘余自由度較高時,就會發生這種情況。這基本上表明該模型不能很好地擬合數據。
但是據我了解,從技術上講,過度分散對于簡單的邏輯回歸而言不是問題,即具有二項式因果關系和單個連續自變量的問題。
偽R平方
對于廣義線性模型(glm),R不產生r平方值。pscl 包中的 pR2 可以產生偽R平方值。
測試p值
檢驗邏輯對數或泊松回歸的p值使用卡方檢驗。方差分析 來測試每一個系數的顯著性。似然比檢驗也可以用來檢驗整體模型的重要性。
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"))
系數和指數系數
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 ***
標準化殘差圖
標準化殘差與預測值的關系圖。殘差應無偏且均等。
繪制模型
Logistic回歸示例
Data = read.table(textConnection(Input),header=TRUE)
模型擬合
model
系數和指數系數
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
標準化殘差圖
繪制模型
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 ***
將因子轉換為數字變量,級別為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轉換為邏輯變量,級別為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
責任編輯:
總結
以上是生活随笔為你收集整理的r语言中残差与回归值的残差图_R语言逻辑回归、方差分析、伪R平方分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3DMAX入门教程,这样还担心学不会?
- 下一篇: 8. spark学习之旅(二)