我有一些数据,我正在尝试使用以下方法拟合幂律曲线:
z <- nls(y ~ a*x^b+c, start = list(a=1, b=1))
Run Code Online (Sandbox Code Playgroud)
但是,我不断收到以下错误消息:
*x ^ b + c中的错误:二元运算符的非数字参数
(较短的版本即y ~ a*x^b+c工作正常,但我需要免费的术语c).
有任何想法吗 ?
你没有c在开始时指定,所以R试图从工作区中获取它.如果没有c,它很可能最终得到这个c功能.所以它试图在c函数中添加一些东西,然后抛出:
> z <- nls(y ~ a*x^b+c, start = list(a=1, b=1))
Error in a * x^b + c : non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)
这里的"二元运算符"是+"非数字参数"是c函数.
如果你想适合c:
> z <- nls(y ~ a*x^b+c, start = list(a=1, b=1, c=1))
> z
Nonlinear regression model
model: y ~ a * x^b + c
data: parent.frame()
a b c
1.647 1.575 2.596
residual sum-of-squares: 9.07
Number of iterations to convergence: 6
Achieved convergence tolerance: 6.503e-07
Run Code Online (Sandbox Code Playgroud)
如果要修复c,请定义它然后将其保留:
> c=2
> z <- nls(y ~ a*x^b+c, start = list(a=1, b=1))
> z
Nonlinear regression model
model: y ~ a * x^b + c
data: parent.frame()
a b
1.802 1.539
residual sum-of-squares: 9.42
Number of iterations to convergence: 7
Achieved convergence tolerance: 2.899e-08
Run Code Online (Sandbox Code Playgroud)