具有给定权重向量的ggplot平滑线glm模型
ggplot smooth line glm model with given vector of weights
我有如下数据:
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
方法与 total
和 weight
有一条平滑的线。我试过了,
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))
作为 ggplot2
版本 2.2.1 的更新答案:
ggplot(numbers, aes(x = density, y = maleProp, weight = total)) +
geom_point() +
stat_smooth(method = "glm",
method.args = list(family = "binomial"))
请注意,您可以使用 weight
作为 aes()
参数。
我有如下数据:
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
方法与 total
和 weight
有一条平滑的线。我试过了,
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))
作为 ggplot2
版本 2.2.1 的更新答案:
ggplot(numbers, aes(x = density, y = maleProp, weight = total)) +
geom_point() +
stat_smooth(method = "glm",
method.args = list(family = "binomial"))
请注意,您可以使用 weight
作为 aes()
参数。