使用ggplot和R如何获取多项式回归方程?

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

How do you get the equation of polynomial regression using ggplot and R?

问题

Here's the translated portion of your text:

我一直在尝试绘制一些数据并添加一个三次多项式趋势线。我正在使用lm()coef()函数来获得我的数据模型,然后使用ggplot来绘制。

以下是相关代码:

Bytes是我的因变量(y)的名称。Resolution是我的自变量(x)的名称。

model = lm(Bytes ~ poly(Resolution, 3), averaged_frame)
print(coef(model))

library(ggplot2)
plot = ggplot(averaged_frame, mapping = aes(x = Resolution, y = Bytes)) + 
  geom_point() + 
  stat_smooth(method = "lm", formula = y ~ poly(x, 3), se = FALSE)

但是,当我从print(coef(model))获取系数时:

         (Intercept) poly(Resolution, 3)1 poly(Resolution, 3)2 poly(Resolution, 3)3 
           0.3392046            0.3686154            0.2504288            0.1274490 

然后将它们转置成一个函数:

f(x) = 0.1274490x^3 + 0.2504288x^2 + 0.3686154x + 0.3392046

并绘制它,它看起来与ggplot中的图完全不同。

我想知道我在这里做错了什么,或者如何从ggplot中获得方程。

英文:

I've been trying to plot some data and add a third order polynomial trend line. I'm using the lm() and coef() functions to obtain a model for my data and then plot with ggplot.

Here is the relevant code:

Bytes is the name of my dependent variable (y). Resolution is the name of my independent variable (x).

model = lm(Bytes~poly(Resolution, 3), averaged_frame)
print(coef(model))

library(ggplot2)
plot = ggplot(averaged_frame, mapping = aes(x = Resolution, y = Bytes)) + 
  geom_point() + 
  stat_smooth(method="lm", formula = y~poly(x, 3), se = FALSE)

However, when I take the coefficients from print(coef(model)):

         (Intercept) poly(Resolution, 3)1 poly(Resolution, 3)2 poly(Resolution, 3)3 
           0.3392046            0.3686154            0.2504288            0.1274490 

And transpose them into a function:

f(x)=0.1274490x^3+0.2504288x^2+0.3686154x+0.3392046

And graph it, it looks completely different to the plot on ggplot.

使用ggplot和R如何获取多项式回归方程?

使用ggplot和R如何获取多项式回归方程?

I'm wondering what I am doing wrong here or how I might get an equation from the line in ggplot.

答案1

得分: 1

这是一个使用 ggpmisc 包的示例。这是一个一阶回归,你需要根据你的情况进行适应。

df <- data.frame(x = c(1:100))
df$y <- 20 + 30 * df$x + rnorm(100, sd = 80)

library(ggpmisc)

my_formula <- y ~ x

myformat <- "y = %s + %s x --- R²: %s"
ggplot(df, aes(x, y)) + 
  geom_point() +
  geom_smooth(method = "lm", formula = my_formula, se = FALSE) +
  stat_poly_eq(
    formula = my_formula, output.type = "numeric",
    mapping = aes(
      label = 
        sprintf(
          myformat,
          formatC(after_stat(coef.ls)[[1]][[1, "Estimate"]]),
          formatC(after_stat(coef.ls)[[1]][[2, "Estimate"]]),
          formatC(stat(r.squared))),
    ),
    vstep = 0.1,
    size = 10
  ) 

使用ggplot和R如何获取多项式回归方程?

英文:

Here is an example with the ggpmisc package. This is for an order one regression, you have to adapt to your case.

df &lt;- data.frame(x = c(1:100))
df$y &lt;- 20 + 30 * df$x + rnorm(100, sd = 80)

library(ggpmisc)

my_formula &lt;- y ~ x

myformat &lt;- &quot;y = %s + %s x --- R&#178;: %s&quot;
ggplot(df, aes(x, y)) + 
  geom_point() +
  geom_smooth(method = &quot;lm&quot;, formula = my_formula, se = FALSE) +
  stat_poly_eq(
    formula = my_formula, output.type = &quot;numeric&quot;,
    mapping = aes(
      label = 
        sprintf(
          myformat,
          formatC(after_stat(coef.ls)[[1]][[1, &quot;Estimate&quot;]]),
          formatC(after_stat(coef.ls)[[1]][[2, &quot;Estimate&quot;]]),
          formatC(stat(r.squared))),
    ),
    vstep = 0.1,
    size = 10
  ) 

使用ggplot和R如何获取多项式回归方程?

huangapple
  • 本文由 发表于 2023年5月13日 13:22:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241207.html
匿名

发表评论

匿名网友

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

确定