英文:
Show sqrt(y) ~ assign(sqrt(x)) relationship with `stat_poly_eq`, `stat_fit_tb` or `stat_fit_tidy`
问题
require(utils)
x = runif(n = 10, min = 0.2, max = 1)
y = rnorm(n = 10, mean = 2.5, sd = .5)
df = expand.grid(x = x, y = y)
我使用下面的代码来绘制 `sqrt(y) ~ assign(sqrt(x))` 关系图,并在图上显示方程。回归线显示出来了,但方程没有显示。
ggplot(df, aes(x = x, y = y)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, formula = sqrt(y) ~ asin(sqrt(x)), col = "black") +
stat_poly_eq(mapping = aes(label = after_stat(eq.label)),
formula = sqrt(y) ~ asin(sqrt(x)))
警告: [38;5;254m`stat_poly_eq()` 中的计算失败[39m
[![enter image description here][1]][1]代码示例来自 <https://cran.r-project.org/web/packages/ggpmisc/vignettes/model-based-annotations.html#stat_poly_eq-and-stat_poly_line>
`stat_fit_tb` 或 `stat_fit_tidy` 返回相同的警告并且没有方程。
[1]: https://i.stack.imgur.com/2OoSV.png
感谢您的阅读。
英文:
require(utils)
x = runif(n = 10, min = 0.2, max = 1)
y = rnorm(n = 10, mean = 2.5, sd = .5)
df = expand.grid(x = x,
y = y)
I used the code below to plot the sqrt(y) ~ assign(sqrt(x))
relationship and show the equation on the graph. The regression line shows, but the equation doesn't.
ggplot(df , aes(x = x, y = y)) +
geom_point() +
geom_smooth( method = "lm", se = TRUE, formula = sqrt(y) ~ asin(sqrt(x)), col = "black") +
stat_poly_eq(mapping = aes(label = after_stat(eq.label)),
formula = sqrt(y) ~ asin(sqrt(x)))
Warning: [38;5;254mComputation failed in `stat_poly_eq()`[39m
Code sample was from <https://cran.r-project.org/web/packages/ggpmisc/vignettes/model-based-annotations.html#stat_poly_eq-and-stat_poly_line>
stat_fit_tb
or stat_fit_tidy
returned the same warning and no equation.
Thanks for stopping by.
答案1
得分: 1
如果在绘图层的模型方程中使用了变换,拟合的函数将不会被正确绘制,因为观测值没有进行变换。变换应该应用于比例尺,特别是对于 y 的情况。公式中的 x 和 y 是美学的值。这适用于拟合线和方程。
计算最初失败,因为无法从模型公式中推断出方向。可以通过传递参数 orientation = "x"
来解决这个问题。然而,在解决了这个问题之后,在计算 R2 的置信区间时会触发一个新错误。这第二个错误是 'ggpmisc' 包中的一个错误。应该更好地处理置信区间计算的失败,而不是在返回所有输出时出错。
在我修复了这个错误之后,下面的代码应该可以工作。(我不仅修复了代码中的错误,还尽可能简化了它。)
ggplot(df , aes(x = asin(sqrt(x)), y = sqrt(y))) +
geom_point() +
stat_poly_line() +
stat_poly_eq(use_label("eq"),
rsquared.conf.level = NA)
对 'ggpmisc' 代码进行快速编辑,可以手动禁用置信区间计算。这个例子是病态的,因为 R^2 几乎等于零。这个 bug 修复将包含在将来的版本 0.5.4 中,与此同时,可以从 GitHub 的 git 存储库 安装修复后的包。
英文:
If one uses a transformation as part of the model equation in a plot layer, the fitted function will not be plotted correctly, as the observations will not the transformed. The transformation should be applied to the scale, specially in the case of y. x and y in the formula are values of the aesthetics. This applies both to the fitted line and the equation.
The computaion fails initially, because the orientation cannot deduced from the model formula. This can be solved by passing an argument to orientation = "x"
. However, after solving this problem a new error is triggered in the computation of the confidence interval for R<sup>2</sup>. This second error is a bug in the 'ggpmisc' package. Failure of the confidence interval computation should be handled better, by returning NAs for the confidence limits instead of failing to return all output.
After I fix the bug, the code below should work. (I not only fixed the error in the code, but in addition simplified it as much as possible.)
ggplot(df , aes(x = asin(sqrt(x)), y = sqrt(y))) +
geom_point() +
stat_poly_line() +
stat_poly_eq(use_label("eq"),
rsquared.conf.level = NA)
A quick edit to the 'ggpmisc' code makes it possible to manually disable the confidence interval computation. This example is pathological in that R^2 is almost exactly zero. This bug-fix will be included in future version 0.5.4, meanwhile the fixed package can be installed from the git repository at GitHub.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论