R 奇异梯度
我试着搜索这个主题的其他线程,但没有一个修复对我有效。我有一个自然实验的结果,我想显示一个事件连续发生的次数符合指数分布。我的R壳贴在下面R 奇异梯度,r,regression,nls,R,Regression,Nls,我试着搜索这个主题的其他线程,但没有一个修复对我有效。我有一个自然实验的结果,我想显示一个事件连续发生的次数符合指数分布。我的R壳贴在下面 f <- function(x,a,b) {a * exp(b * x)} > x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 > y [1] 1880 813 376 161 100 61
f <- function(x,a,b) {a * exp(b * x)}
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27
> y
[1] 1880 813 376 161 100 61 31 9 8 2 7 4 3 2 0
[16] 1 0 0 0 0 0 1 0 0 0 0 1
> dat2
x y
1 1 1880
2 2 813
3 3 376
4 4 161
5 5 100
6 6 61
7 7 31
8 8 9
9 9 8
10 10 2
11 11 7
12 12 4
13 13 3
14 14 2
> fm <- nls(y ~ f(x,a,b), data = dat2, start = c(a=1, b=1))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
> fm <- nls(y ~ f(x,a,b), data = dat2, start = c(a=7, b=-.5))
Error in nls(y ~ f(x, a, b), data = dat2, start = c(a = 7, b = -0.5)) :
singular gradient
> fm <- nls(y ~ f(x,a,b), data = dat2, start = c(a=7,b=-.5),control=nls.control(maxiter=1000,warnOnly=TRUE,minFactor=1e-5,tol=1e-10),trace=TRUE)
4355798 : 7.0 -0.5
Warning message:
In nls(y ~ f(x, a, b), data = dat2, start = c(a = 7, b = -0.5), :
singular gradient
fx
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27
>y
[1] 1880 813 376 161 100 61 31 9 8 2 7 4 3 2 0
[16] 1 0 0 0 0 0 1 0 0 0 0 1
>dat2
xy
1 1 1880
2 2 813
3 3 376
4 4 161
5 5 100
6 6 61
7 7 31
8 8 9
9 9 8
10 10 2
11 11 7
12 12 4
13 13 3
14 14 2
>fm1)线性化以获得起始值您需要更好的起始值:
# 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))
1a)类似地,我们可以使用lm
通过写入来获得初始值
y ~ a * exp(b * x)
作为
取两者的对数,得到对数(a)和b中的线性模型:
可使用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)
1b)我们还可以通过重新参数化使其工作。在这种情况下,a=1和b=1将起作用,前提是我们按照参数转换转换初始值
nls(y ~ exp(loga + b * x), dat2, start = list(loga = log(1), b = 1))
给予:
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
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
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
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
所以b如图所示,a=exp(loga)=exp(8.346)=4213.3
2)plinear另一种更容易的方法是使用alg=“plinear”
,在这种情况下,线性输入的参数不需要起始值。在这种情况下,问题中b=1的起始值似乎足够了
nls(y ~ exp(b * x), dat2, start = c(b = 1), alg = "plinear")
给予:
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
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
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
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
请检查minpack.lm包中的nlsLM函数。这是nls的一个更健壮的版本,可以处理剩余平方和为零的数据
谢谢,之前曾尝试使用y~aexp(bx)计算系数,但收到错误,使用日志这一非常好的方法来计算起始值,谢谢!只是出于兴趣,通过log(函数)的方式“引导”初始条件是一般的标准方法还是仅仅针对指数函数的标准方法?log
的动机是在log(a)
和b
中将其转换为线性,并且线性函数易于优化。好的,明白了。本可以用lm完成sts,必须转换截距,这增加了一个步骤,这就是为什么我没有首先尝试的原因;但是,如果双方的记录不充分,那么下一步应该尝试的是lm
。请解释您提供的解决方案是如何解决问题的。外部链接也有一个缺点,就是将来不总是可以访问的。对于我试图解决的问题,nlsLM()函数能够返回一个合理的解决方案,其中nls()失败,出现奇异梯度错误。试试下面的exmaple。对不起,格式太乱了。仍在学习这种格式。nls()失败,但nlsLM正确恢复参数#模拟一些数据集。seed(20160227)xBest答案,至少用于强非线性拟合。