英文:
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.
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
)
英文:
Here is an example with the ggpmisc package. This is for an order one regression, you have to adapt to your case.
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论