1)线性化以获得起始值您需要更好的起始值:
# starting values
fm0 <- nls(log(y) ~ log(f(x, a, b)), dat2, start = c(a = 1, b = 1))
nls(y ~ f(x, a, b), dat2, start = coef(fm0))
giving:
Nonlinear regression model
model: y ~ f(x, a, b)
data: x
a b
4214.4228 -0.8106
residual sum-of-squares: 2388
Number of iterations to convergence: 6
Achieved convergence tolerance: 3.363e-06
1a)同样我们可以使用lm
通过写入获得初始值
y ~ a * exp(b * x)
as
y ~ exp(log(a) + b * x)
并取两者的对数以获得 log(a) 和 b 的线性模型:
log(y) ~ log(a) + b * x
可以使用以下方法解决lm
:
fm_lm <- lm(log(y) ~ x, dat2)
st <- list(a = exp(coef(fm_lm)[1]), b = coef(fm_lm)[2])
nls(y ~ f(x, a, b), dat2, start = st)
giving:
Nonlinear regression model
model: y ~ f(x, a, b)
data: dat2
a b
4214.423 -0.811
residual sum-of-squares: 2388
Number of iterations to convergence: 6
Achieved convergence tolerance: 3.36e-06
1b)我们还可以通过重新参数化来使其工作。在这种情况下,只要我们按照参数变换来变换初始值,a = 1 和 b = 1 就可以工作。
nls(y ~ exp(loga + b * x), dat2, start = list(loga = log(1), b = 1))
giving:
Nonlinear regression model
model: y ~ exp(loga + b * x)
data: dat2
loga b
8.346 -0.811
residual sum-of-squares: 2388
Number of iterations to convergence: 20
Achieved convergence tolerance: 3.82e-07
所以 b 如图所示,a = exp(loga) = exp(8.346) = 4213.3
2) 线性另一种更简单的可能性是使用alg="plinear"
在这种情况下,线性输入的参数不需要起始值。在这种情况下,起始值b=1
在问题中似乎足够了。
nls(y ~ exp(b * x), dat2, start = c(b = 1), alg = "plinear")
giving:
Nonlinear regression model
model: y ~ exp(b * x)
data: dat2
b .lin
-0.8106 4214.4234
residual sum-of-squares: 2388
Number of iterations to convergence: 11
Achieved convergence tolerance: 2.153e-06