Я хотел бы воссоздать отметки данных, найденные на границе 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)