【发布时间】:2020-06-24 09:25:16
【问题描述】:
我正在尝试绘制一个图来展示逻辑(或概率)回归背后的直觉。我如何在 ggplot 中制作一个看起来像这样的情节?
(Wolf & Best,The Sage Handbook of Regression Analysis and Causal Inference,2015 年,第 155 页)
实际上,我什至宁愿做的是沿 y 轴显示一个单一的正态分布,均值 = 0,以及一个特定的方差,这样我就可以画出从线性预测器到 y 轴和横向的水平线正态分布。像这样的:
这是应该显示的(假设我没有误解某些内容)是。到目前为止,我还没有取得太大的成功......
library(ggplot2)
x <- seq(1, 11, 1)
y <- x*0.5
x <- x - mean(x)
y <- y - mean(y)
df <- data.frame(x, y)
# Probability density function of a normal logistic distribution
pdfDeltaFun <- function(x) {
prob = (exp(x)/(1 + exp(x))^2)
return(prob)
}
# Tried switching the x and y to be able to turn the
# distribution overlay 90 degrees with coord_flip()
ggplot(df, aes(x = y, y = x)) +
geom_point() +
geom_line() +
stat_function(fun = pdfDeltaFun)+
coord_flip()
【问题讨论】:
标签: r ggplot2 logistic-regression