欢迎光临散文网 会员登陆 & 注册

R语言逻辑回归、方差分析 、伪R平方分析

2021-03-02 22:32 作者:拓端tecdat  | 我要投稿

原文链接: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回归示例

 



  1. Data = read.table(textConnection(Input),header=TRUE)


  2. Data$Total = Data$mpi90 + Data$mpi100


  3. Data$Percent = Data$mpi100 / + Data$Total

模型拟合

 

  1. Trials = cbind(Data$mpi100, Data$mpi90)         # Sucesses, Failures


  2. model = glm(Trials ~ Latitude,

  3. data = Data,

  4. family = binomial(link="logit"))

系数和指数系数

 


  1. Coefficients:


  2. Estimate Std. Error z value Pr(>|z|)


  3. (Intercept) -7.64686    0.92487  -8.268   <2e-16 ***


  4. Latitude     0.17864    0.02104   8.490   <2e-16 ***







  5. 2.5 %     97.5 %


  6. (Intercept) -9.5003746 -5.8702453


  7. Latitude     0.1382141  0.2208032





  8. # exponentiated coefficients




  9. (Intercept)     Latitude


  10. 0.0004775391 1.1955899446





  11. # 95% CI for exponentiated coefficients




  12. 2.5 %      97.5 %


  13. (Intercept) 7.482379e-05 0.002822181


  14. Latitude    1.148221e+00 1.247077992

方差分析 



  1. Analysis of Deviance Table (Type II tests)




  2. Response: Trials


  3. Df  Chisq Pr(>Chisq)


  4. Latitude   1 72.076  < 2.2e-16 ***

 

伪R平方

 


  1. $Models




  2. Model: "glm, Trials ~ Latitude, binomial(link = \"logit\"), Data"


  3. Null:  "glm, Trials ~ 1, binomial(link = \"logit\"), Data"




  4. $Pseudo.R.squared.for.model.vs.null


  5. Pseudo.R.squared


  6. McFadden                             0.425248


  7. Cox and Snell (ML)                   0.999970


  8. Nagelkerke (Cragg and Uhler)         0.999970

 

模型的整体p值

 




  1. Analysis of Deviance Table




  2. Model 1: Trials ~ Latitude


  3. Model 2: Trials ~ 1


  4. Resid. Df Resid. Dev Df Deviance  Pr(>Chi)


  5. 1         6     70.333


  6. 2         7    153.633 -1  -83.301 < 2.2e-16 ***





  7. Likelihood ratio test




  8. Model 1: Trials ~ Latitude


  9. Model 2: Trials ~ 1


  10. #Df  LogLik Df  Chisq Pr(>Chisq)


  11. 1   2 -56.293


  12. 2   1 -97.944 -1 83.301  < 2.2e-16 ***

 

标准化残差图

 

 

 

 

标准化残差与预测值的关系图。残差应无偏且均等。 

 

绘制模型

 

 

 

 

 

 

Logistic回归示例

 



  1. Data = read.table(textConnection(Input),header=TRUE)



 

模型拟合

 

model

 

系数和指数系数

 



  1. Coefficients:


  2. Estimate Std. Error z value Pr(>|z|)


  3. (Intercept)  4.41379    6.66190   0.663    0.508


  4. Height      -0.05016    0.09577  -0.524    0.600









  5. 2.5 %     97.5 %


  6. (Intercept) -8.4723648 18.4667731


  7. Height      -0.2498133  0.1374819




  8. # exponentiated coefficients




  9. (Intercept)      Height


  10. 82.5821122   0.9510757





  11. # 95% CI for exponentiated coefficients




  12. 2.5 %       97.5 %


  13. (Intercept) 0.0002091697 1.047171e+08


  14. Height      0.7789461738 1.147381e+0



 

方差分析

 



  1. Analysis of Deviance Table (Type II tests)




  2. Response: Insect


  3. Df  Chisq Pr(>Chisq)


  4. Height     1 0.2743     0.6004


  5. Residuals 23

 

伪R平方

 



  1. $Pseudo.R.squared.for.model.vs.null


  2. Pseudo.R.squared


  3. McFadden                           0.00936978


  4. Cox and Snell (ML)                 0.01105020


  5. Nagelkerke (Cragg and Uhler)       0.01591030

 

模型的整体p值

 



  1. Analysis of Deviance Table




  2. Model 1: Insect ~ Height


  3. Model 2: Insect ~ 1


  4. Resid. Df Resid. Dev Df Deviance Pr(>Chi)


  5. 1        23     29.370


  6. 2        24     29.648 -1 -0.27779   0.5982







  7. Likelihood ratio test




  8. Model 1: Insect ~ Height


  9. Model 2: Insect ~ 1


  10. #Df  LogLik Df  Chisq Pr(>Chisq)


  11. 1   2 -14.685


  12. 2   1 -14.824 -1 0.2778     0.5982

标准化残差图

 

 

 

 

绘制模型

 




  1. Height Insect Insect.num


  2. 1      62 beetle          0


  3. 2      66  other          1


  4. 3      61 beetle          0


  5. 23     72  other          1


  6. 24     70 beetle          0


  7. 25     74  other          1




 

 

 

 




  1. Height Insect Insect.num Insect.log


  2. 1      62 beetle          0      FALSE


  3. 2      66  other          1       TRUE


  4. 3      61 beetle          0      FALSE


  5. 23     72  other          1       TRUE


  6. 24     70 beetle          0      FALSE


  7. 25     74  other          1       TRUE


 

 

Logistic回归示例

 



  1. Data = read.table(textConnection(Input),header=TRUE)



  2. model


  3. Coefficients:


  4. Estimate Std. Error z value Pr(>|z|)


  5. (Intercept) -66.4981    32.3787  -2.054   0.0400 *


  6. Continuous    0.9027     0.4389   2.056   0.0397 *







  7. Analysis of Deviance Table (Type II tests)




  8. Response: Factor


  9. Df Chisq Pr(>Chisq)


  10. Continuous  1 4.229    0.03974 *


  11. Residuals  27






  12. Pseudo.R.squared


  13. McFadden                             0.697579


  14. Cox and Snell (ML)                   0.619482


  15. Nagelkerke (Cragg and Uhler)         0.826303







  16. Resid. Df Resid. Dev Df Deviance Pr(>Chi)


  17. 1        27     12.148


  18. 2        28     40.168 -1   -28.02  1.2e-07 ***





 

将因子转换为数字变量,级别为0和1 
 



  1. Continuous Factor Factor.num


  2. 1          62      A          0


  3. 2          63      A          0


  4. 3          64      A          0


  5. 27         84      B          1


  6. 28         85      B          1


  7. 29         86      B          1




 

 

 

将Factor转换为逻辑变量,级别为TRUE和FALSE 
 




  1. Continuous Factor Factor.num Factor.log


  2. 1          62      A          0      FALSE


  3. 2          63      A          0      FALSE


  4. 3          64      A          0      FALSE


  5. 27         84      B          1       TRUE


  6. 28         85      B          1       TRUE


  7. 29         86      B          1       TRUE




R语言逻辑回归、方差分析 、伪R平方分析的评论 (共 条)

分享到微博请遵守国家法律