gpt4 book ai didi

r - 使用 ggplot2 进行逻辑回归 + 直方图

转载 作者:行者123 更新时间:2023-12-04 12:20:51 24 4
gpt4 key购买 nike

我有一些二进制数据,我想在同一个图中绘制逻辑回归线和 0 和 1 的相对频率的直方图。
我在这里使用 popbio 包遇到了一个非常好的实现:shizuka lab's page
这是一个与库(popbio)一起运行的 MWE(由静香实验室提供)

bodysize=rnorm(20,30,2) # generates 20 values, with mean of 30 & s.d.=2
bodysize=sort(bodysize) # sorts these values in ascending order.
survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1) # assign 'survival' to these 20 individuals non-randomly... most mortality occurs at smaller body size
dat=as.data.frame(cbind(bodysize,survive))

#and now the plot
library(popbio)
logi.hist.plot(bodysize,survive,boxp=FALSE,type="hist",col="gray")
产生
enter image description here
现在,可以用 ggplot2 做到这一点吗?

最佳答案

这里有一些想法

ggplot(dat, aes(x = bodysize, y = survive)) + 
geom_dotplot(
aes(fill = factor(survive)), method = "histodot", binpositions = "all",
stackgroups = TRUE, stackdir = "centerwhole", binwidth = 1
) +
geom_smooth(method = "glm", family = "binomial")

ggplot(dat, aes(x = bodysize, y = survive)) +
geom_hex(bins = 10) +
geom_smooth(method = "glm", family = "binomial")

ggplot(dat, aes(x = bodysize, y = survive)) +
geom_bin2d(bins = 10) +
geom_smooth(method = "glm", family = "binomial")

关于r - 使用 ggplot2 进行逻辑回归 + 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com