如何在R中绘制非线性回归线

时间:2016-03-30 17:14:55

标签: r

我已将以下数据集输入R:

Distance    Overall Average
-9.867344   0.001928728
-8.769583   0.0011855
-8.667573   0.001401361
-8.373794   0.001514139
-7.443686   0.001046918
-3.862468   0.000790981
1.817748    0.000945557
2.5333892   0.000940648
4.190557    0.001649773

当绘制到散点图上时,这些会给出U形曲线,因此我试图将非线性回归“线”绘制到图上。 Plot of the original Data showing the U-shaped curve

我在上一个问题中看到了以下示例...

plot(speed ~ dist, data = cars)
fit1 = lm(speed ~ dist, cars) #fits a linear model
plot(speed ~ dist, data = cars)
abline(fit1) #puts line on plot
fit2 = lm(speed ~ I(dist^2) + dist, cars) #fits a model with a quadratic term
fit2line = predict(fit2, data.frame(dist = -10:130))

但是,经过多次尝试后,我一直会看到同样的错误评论:

plot(Ammonia$Overall.Average~Ammonia$Distance)
fit1=lm(Ammonia$Overall.Average~Ammonia$Distance)
abline(fit1)
fit2=lm(Ammonia$Overall.Average ~ I(Ammonia$Distance^2) + Ammonia$Distance)
fit2line = predict(fit2, data.frame(Ammonia$Distance = 9,3))
Error: unexpected '=' in "fit2line = predict(fit2, data.frame(Ammonia$Distance ="

我也不确定“9,3”是否是正确的数字,因为我不确定我所遵循的例子从哪里得到他们的数字。

0 个答案:

没有答案