显示sqrt(y) ~ assign(sqrt(x))与`stat_poly_eq`、`stat_fit_tb`或`stat_fit_tidy`之间的关系。

huangapple go评论67阅读模式
英文:

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)))

警告: `stat_poly_eq()` 中的计算失败

[![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 = &quot;lm&quot;, se = TRUE, formula = sqrt(y) ~ asin(sqrt(x)), col = &quot;black&quot;) +
stat_poly_eq(mapping = aes(label = after_stat(eq.label)), 
               formula =  sqrt(y) ~ asin(sqrt(x)))

Warning: Computation failed in `stat_poly_eq()`

显示sqrt(y) ~ assign(sqrt(x))与`stat_poly_eq`、`stat_fit_tb`或`stat_fit_tidy`之间的关系。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 的情况。公式中的 xy 是美学的值。这适用于拟合线和方程。

计算最初失败,因为无法从模型公式中推断出方向。可以通过传递参数 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 = &quot;x&quot;. 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(&quot;eq&quot;), 
               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.

huangapple
  • 本文由 发表于 2023年7月28日 04:29:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定