gpt4 book ai didi

r - 具有给定权重向量的ggplot平滑线glm模型

转载 作者:行者123 更新时间:2023-12-05 00:19:16 26 4
gpt4 key购买 nike

我有如下数据:

numbers <- structure(list(density = c(1L, 4L, 10L, 22L, 55L, 121L, 210L, 
444L), females = c(1L, 3L, 7L, 18L, 22L, 41L, 52L, 79L), males = c(0L,
1L, 3L, 4L, 33L, 80L, 158L, 365L), maleProp = c(0, 0.25, 0.3,
0.181818181818182, 0.6, 0.661157024793388, 0.752380952380952,
0.822072072072072), total = c(1L, 4L, 10L, 22L, 55L, 121L, 210L,
444L)), .Names = c("density", "females", "males", "maleProp",
"total"), row.names = c(NA, -8L), class = "data.frame")

我想和 glm 有一个流畅的线条方法与 totalweight .我试过,
ggplot(numbers, aes(density, maleProp)) + geom_point() + 
stat_smooth(method = "glm",
method.args = list(family = "binomial",
type = "response",
weights = "total"))

我有错误,
Warning message:
Computation failed in `stat_smooth()`:
formal argument "weights" matched by multiple actual arguments

在这种情况下如何绘制平滑线?

最佳答案

如果您想使用 glm使用您概述的参数,您可以创建一个包装函数,如下所示:

binomial_smooth <- function(...) {
geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}

您可以直接在 ggplot2 上使用目的:
ggplot(numbers, aes(density, maleProp)) + geom_point() + binomial_smooth(aes(weight = total))

关于r - 具有给定权重向量的ggplot平滑线glm模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36286538/

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