从Logistic回归中提取概率和SE

时间:2018-07-01 18:43:35

标签: r logistic-regression prediction

我有一个给定变量x的任务(1或0)选择数据集。以mtcars为例

#binomial_smooth() from https://ggplot2.tidyverse.org/reference/geom_smooth.html
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}

#plot
ggplot(data = mtcars, aes(x = disp, y = am)) + geom_point(alpha = 0.5) + binomial_smooth()

#create a model
model <- glm(am ~ disp, family = "binomial", data = mtcars)

enter image description here

其中am是主题选择,并显示x变量。我想为二进制变量= 0.5得出x +/- SE的值(我想这是binomial_smooth正在绘制的内容,尽管可能是错误的)。

使用mtcar,我想找出哪个disp +/- SE am = 0.5。环顾四周,变得越来越困惑,所以任何帮助将不胜感激!

最好

1 个答案:

答案 0 :(得分:0)

好吧,所以我是在跟随RomanLuštrik的兔子洞(欢呼!)之后才想到的。

使用MASS软件包和用于计算LD50的函数。还可以手动选择要查找的p值。

library(ggplot2)
library(MASS)
#binomial_smooth() from https://ggplot2.tidyverse.org/reference/geom_smooth.html
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}

#create a model
model <- glm(am ~ disp, family = "binomial", data = mtcars)

#get the 'LD50'- the point at which the binomial regression crosses 50%
LD50 <- dose.p(model, p = 0.5)

#print the details
print(LD50)

#replot the figure with the LD50 vlines
ggplot(data = mtcars, aes(x = disp, y = am)) + 
  geom_point(alpha = 0.5) + 
  binomial_smooth() +
  geom_vline(xintercept = LD50[[1]])

enter image description here