大家好!
我使用brglm::brglm
在R中拟合了多变量(偏差减少)逻辑回归。我一直在尝试制作一个看起来很漂亮的情节
我尝试使用popbio::log.hist.plot
,但我没有设法添加置信区间,并且在对多个图表进行分组时未使用par(mar, oma, mgp, mai)
实现移动。
然后我尝试了visreg::visreg
,它创建了带有分隔地毯的漂亮地块,但没有添加直方图的选项。
我找到了ggplot2: How to combine histogram, rug plot, and logistic regression prediction in a single graph,但我又无法在ggplot
- 图表中添加一个明确间隔。
最后,我使用上面链接中的一些行以及我的一些相当粗略的代码合并了visreg
和ggplot
:
library(brglm)
library(plyr)
library(dplyr)
library(visreg)
library(ggplot2)
# creating random data
set.seed(60)
df <- cbind(y <- c(floor(runif(30, min=0, max=1.2)),
floor(runif(50, min=0, max=2))),
x1 <- c(runif(30, min=0, max=50), runif(50, min=20, max=100)),
x2 <- runif(80, min=0, max=100),
x3 <- runif(80, min=0, max=100))
colnames(df) <- c("y", "x1", "x2", "x3")
df <- data.frame(df)
# fitting the model
mod <- brglm(y ~ x1 + x2 + x3, binomial, data=df)
# setting breaks for histograms
h = df %>% group_by(y) %>%
mutate(breaks = cut(x1, breaks=seq(from= 0, to= 105, by=5),
labels=seq(from= 0, to= 100, by= 5),
include.lowest=TRUE),
breaks = as.numeric(as.character(breaks))) %>%
group_by(y, breaks) %>%
summarise(n = n()) %>%
mutate(ext = ifelse(y==0, n/sum(n), 1 - n/sum(n)))
# splitting h for y = 0 or 1, setting mins and maxs
h.split <- split(h, h$y)
min1_p <- min((h.split$"1")$ext)
max1_n <- max((h.split$"1")$n)
max0_p <- max((h.split$"0")$ext)
max0_n <- max((h.split$"0")$n)
# plottin visreg with hist
visreg (mod, "x1", ylab=NULL, xlab= "x1",
line=list(col="black", size=2), gg=T, scale="response") +
geom_segment(data=h, size=5, show.legend=FALSE,
aes(x=breaks+2.5, xend=breaks+2.5, y=y, yend=ext))+
scale_y_continuous(name = expression("p(y=1)"), limits = c(0, 1),
sec.axis = sec_axis(~ . +0 , name = "n", breaks = c(
seq(from=(max0_p/max0_n),
to=max0_p,
by=(max0_p/max0_n)),
seq(from= min1_p,
to=1-(1-min1_p)/max1_n,
by=(1-min1_p)/max1_n)),
labels = c(
seq(from=1, to=max0_n, by=1),
rev(seq(from=1, to=max1_n,
by=1)))
)) +
scale_x_continuous(limits=c(min(x1)-1,max(x1))) +
theme(panel.background = element_rect(fill = "white", colour = "grey"))
结果情节如下所示 visreg + confedence interval + histograms with ggplot2
感谢您的关注,
兰尼