【发布时间】:2018-03-25 23:20:33
【问题描述】:
我正在尝试为我的数据拟合一条回归曲线。我的代码生成了我想要的图和曲线,但是,我不需要散点图——只需要线。如果我注释掉情节,我的代码就会失败。有没有办法(绕过、关闭、隐藏)散点图?
最终,我需要比较图表上的多条回归曲线,而散点图会分散注意力。另外,我的 R2 显示为 NULL。 R2有系数吗?
代码如下。
# get underlying plot
y <- dataset$'Fuel Consumed 24h'
x <-dataset$'Performance Speed'
plot(x, y, xlab = "Performance Speed", ylab = "24h Fuel Consumption")
# polynomial
f <- function(x,a,b,d) {(a*x^2) + (b*x) + d}
fit <- nls(y ~ f(x,a,b,d), start = c(a=1, b=1, d=1))
co <- round(coef(fit), 2)
r2 <- format(summary(fit)$r.squared, digits = 3)
curve(f(x, a=co[1], b=co[2], d=co[3]), add = TRUE, col="red", lwd=2)
eq <- paste0("Fuel = ", co[1], "PS^2 ", ifelse(sign(co[2]) == 1, " + ", " - "), abs(co[2]), "PS +", co[3], " R2 = ", r2)
# print equation
mtext(eq, 3, line =-2)
mylabel = bquote(italic(R)^2 == .(format(r2, digits = 3)))
text(x = 1, y = 2.5, r2)
【问题讨论】:
-
尝试添加
type="n"...plot(x, y, type="n", ...,nls 不会返回 r^2,因为它没有意义(尽管我认为有些软件包可以这样做)。也就是说,你的模型不是非线性的,你可以用lm
标签: r plot non-linear-regression