geom_smooth 中二项式公式的语法
Syntax for binomial formula in geom_smooth
我在 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")
但我真正想要的是通过数据绘制逻辑曲线。当我尝试时:
ggplot(tb1, aes(x, success/(success+failure), colour=f)) +
geom_point() +
geom_smooth(
method="glm",
method.args=list(family="binomial"),
)
我得到这张图:
这似乎不对。标准误差不应该这么大。我以为我需要在 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
)
现在可以使用了:
感谢 NelsonGon 指出这一点。
我在 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")
但我真正想要的是通过数据绘制逻辑曲线。当我尝试时:
ggplot(tb1, aes(x, success/(success+failure), colour=f)) +
geom_point() +
geom_smooth(
method="glm",
method.args=list(family="binomial"),
)
我得到这张图:
这似乎不对。标准误差不应该这么大。我以为我需要在 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 instat_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
)
现在可以使用了:
感谢 NelsonGon 指出这一点。