我想重新创建在visreg()
逻辑回归图边界中找到的数据刻度,使用ggplot()
.
我可以得到用 绘制的数据点geom_point()
,但这些在轴空间中,我想将它们放在边界中。
我试过用函数查看源代码,getAnywhere()
但visreg()
在那里看不到任何信息。有任何想法吗?一个可重现的例子:
library(ggplot2)
library(visreg)
y <- rep(c("0","1"), each = 50)
x <- c(rnorm(50,70,20),rnorm(50,30,20))
y <- as.numeric(y)
df <- data.frame(x,y)
glm1 <- glm(y~x, data = df, family = binomial)
visreg(glm1, type="conditional", scale = "response")
df$fit <- predict(glm1)
df$fit.response <- exp(df$fit)/(1+exp(df$fit))
# For simple ribbon
df$lower <- df$fit - predict(glm1, se.fit = TRUE)$se.fit
df$upper <- df$fit + predict(glm1, se.fit = TRUE)$se.fit
df$low.response <- exp(df$lower)/(1+exp(df$lower))
df$upp.response <- exp(df$upper)/(1+exp(df$upper))
ggplot(data = df, aes(x = x, y = fit.response))+
geom_ribbon(aes(x=x, ymin = low.response, ymax = upp.response), alpha = 0.3)+
geom_line(colour = "blue")+
geom_point(data = df, aes(x=x, y=y))+
theme_bw()
getAnywhere(visreg)