gpt4 book ai didi

r - geom_smooth 中二项式公式的语法

转载 作者:行者123 更新时间:2023-12-04 02:42:46 26 4
gpt4 key购买 nike

我在 R 中计算了二项式回归:

Call:
glm(formula = cbind(success, failure) ~ x * f, family = "binomial",
data = tb1)

Deviance Residuals:
Min 1Q Median 3Q Max
-3.6195 -0.9399 -0.0493 0.5698 2.0677

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.3170182 0.0622600 -37.215 < 2e-16 ***
x 0.0138201 0.0009892 13.972 < 2e-16 ***
fTRUE 0.6466238 0.1976115 3.272 0.00107 **
x:fTRUE -0.0035741 0.0032587 -1.097 0.27273
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 479.88 on 147 degrees of freedom
Residual deviance: 201.53 on 144 degrees of freedom
(7 observations deleted due to missingness)
AIC: 870.72

Number of Fisher Scoring iterations: 4

我想把它形象化。我想绘制数据和回归曲线。我可以轻松地使线性更平滑:
ggplot(tb1, aes(x, success/(success+failure), colour=f)) +
geom_point() +
geom_smooth(method="lm")

Linear Regression

但我真正想要的是通过数据绘制逻辑曲线。当我尝试:
ggplot(tb1, aes(x, success/(success+failure), colour=f)) +
geom_point() +
geom_smooth(
method="glm",
method.args=list(family="binomial"),
)

我得到这个图:

Suspicious binomial regression

这似乎不对。标准误差不应该那么大。我想我需要在 geom_smooth 中明确指定公式,但我无法正确使用语法。当我尝试
ggplot(tb1, aes(x, success/(success+failure), colour=f)) +
geom_point() +
geom_smooth(
method="glm",
method.args=list(
family="binomial",
formula = cbind(success, failure) ~ x
)
)

我得到

Warning message:
Computation failed in stat_smooth():
object 'success' not found



如何正确指定公式?

最佳答案

与二项式回归一样,geom_smooth 中的公式需要有一个成功和失败的矩阵作为响应。需要在美学中定义相应的变量:

ggplot(tb1, aes(x, y=success/(success+failure), colour=f, succ=success, fail=failure)) +
geom_point() +
geom_smooth(
method="glm",
method.args=list(family="binomial"),
formula = cbind(succ, fail) ~ x
)

现在它的工作原理:

Correct binomial smoothing

感谢 NelsonGon指出这一点。

关于r - geom_smooth 中二项式公式的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560260/

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