我正在学习用R进行非线性平方拟合,我遵循了本教程:
p = function(x) x^3+2*x^2+5
x = seq(-0.99, 1, by = .01)
y = p(x) + runif(200)
df = data.frame(x = x, y = y)
head(df)
x y
1 -0.99 6.183018
2 -0.98 6.611669
3 -0.97 6.762615
4 -0.96 6.594278
5 -0.95 5.990637
6 -0.94 6.048369
# Then the author conducted a nonlinear regression fit.
fit = nls(y~a*x^2+b*x, data = df, start(a=0, b=0))
但当我尝试运行代码时,它总是说"hasTsp(x(中的错误:参数";x〃;缺少,没有默认的";
有人知道问题出在哪里吗?
非常感谢!
我们需要start
作为list
参数
nls(y~a*x^2+b*x, data = df, start = list(a=0, b=0))
-输出
Nonlinear regression model
model: y ~ a * x^2 + b * x
data: df
a b
11.1241 0.5711
residual sum-of-squares: 2713
Number of iterations to convergence: 1
Achieved convergence tolerance: 3.246e-10