R中的glm()与Excel中

时间:2017-08-18 19:44:42

标签: r excel logistic-regression glm solver

您可以在http://blog.excelmasterseries.com/2014/06/logistic-regression-performed-in-excel.html找到Excel中逻辑回归的手动实现。

此实现使用下面的数据集并报告以下系数

  

b0 = 12.48285608

     

b1 = -0.117031374

     

b2 = -1.469140055

但是,当我在 R 中使用glm()分析相同的数据集时,结果并不相同,即:

  

b0 = 1.687445

     

b1 = -0.012525

     

b2 = -0.116473

d <- structure(list(Y = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1 = c(78L, 73L, 73L, 
71L, 68L, 59L, 57L, 49L, 35L, 27L, 59L, 57L, 44L, 38L, 36L, 36L, 
22L, 22L, 15L, 10L), X2 = c(8L, 8L, 5L, 7L, 5L, 4L, 7L, 5L, 4L, 
7L, 3L, 4L, 5L, 5L, 4L, 2L, 6L, 5L, 4L, 6L)), .Names = c("Y", 
"X1", "X2"), class = "data.frame", row.names = c(NA, -20L))  

summary(glm(Y ~ X1+X2, data=d), family=binomial(link='logit'))


# > summary(glm(Y ~ X1+X2, data=d), family=binomial(link='logit'))
# 
# Call:
#   glm(formula = Y ~ X1 + X2, data = d)
# 
# Deviance Residuals: 
#   Min        1Q    Median        3Q       Max  
# -0.78318  -0.20641   0.07689   0.24375   0.49237  
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)  1.687445   0.319872   5.275 6.18e-05 ***
#   X1          -0.012525   0.004376  -2.862   0.0108 *  
#   X2          -0.116473   0.056959  -2.045   0.0567 .  
# ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# (Dispersion parameter for gaussian family taken to be 0.146843)
# 
# Null deviance: 5.0000  on 19  degrees of freedom
# Residual deviance: 2.4963  on 17  degrees of freedom
# AIC: 23.139
# 
# Number of Fisher Scoring iterations: 2

为什么结果不同?

1 个答案:

答案 0 :(得分:5)

您的family参数位于错误的位置。它应该在B,D来电,而不是glm()来电。

summary()

如果你不在summary(glm(Y ~ X1+X2, data=d, family=binomial(link='logit'))) 中加入家庭,那么它会进行高斯(线性)回归。