使用GLM从Logistic回归模型中获取均值和标准差

时间:2019-12-09 02:55:40

标签: r logistic-regression glm

使用下面的数据集并在Rstudio中使用R,试图使用“ glm”形成一个非常简单的逻辑回归模型,然后给出公式logit ProbOfBreastFeed = a + b * Age,其中a is the interceptb is the slope我试图得到a和b的均值和标准差。我是R的新手,所以在这里可能是对如何解决这个问题的一种误解,但是当尝试使用glm时,我会得到错误Error in model.frame.default(formula = logisticBabies ~ No + Yes * Age, : invalid type (list) for variable 'logisticBabies',并且不确定为什么是这样或如何解决。有见识吗?

数据

Age   No    Yes
28     4     2
29     3     2
30     2     7
31     2     7
32     4     16
33     1     14 

当前R代码

glm(logisticBabies ~ No + Yes * Age, data = logisticBabies, family = binomial)

1 个答案:

答案 0 :(得分:1)

logisticBabies <- structure(list(Age = 28:33, No = c(4L, 3L, 2L, 2L, 4L, 1L), Yes = c(2L, 
2L, 7L, 7L, 16L, 14L)), class = "data.frame", row.names = c(NA, 
-6L))

您想要的是:

fit <- glm(cbind(Yes, No) ~ Age, family = binomial, logisticBabies)

,然后进行参数估计:

summary(fit)
#Call:
#glm(formula = cbind(Yes, No) ~ Age, family = binomial, data = d)
#
#Deviance Residuals: 
#      1        2        3        4        5        6  
#-0.1472  -0.4602   0.8779   0.1114  -0.6119   0.3251  
#
#Coefficients:
#            Estimate Std. Error z value Pr(>|z|)   
#(Intercept) -16.7198     6.0630  -2.758  0.00582 **
#Age           0.5769     0.1977   2.918  0.00352 **
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#(Dispersion parameter for binomial family taken to be 1)
#
#    Null deviance: 11.1772  on 5  degrees of freedom
#Residual deviance:  1.4968  on 4  degrees of freedom
#AIC: 19.556
#
#Number of Fisher Scoring iterations: 4