如何为nls函数找到好的开始值?

编程入门 行业动态 更新时间:2024-10-26 14:39:04
本文介绍了如何为nls函数找到好的开始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不明白为什么我无法使用这些数据的nls函数。 我尝试了很多不同的起始值,并且总是有相同的错误。

以下是我一直在做的事情:

expFct2 = function(x,a,b,c) {a *(1-exp(-x / b ))+ c } vec_x <-c(77.87,87.76,68.6,66.29) vec_y <-c(1,1,0.8,0.6) dt < - data.frame(vec_x = vec_x,vec_y = vec_y) ggplot(data = dt,aes(x = vec_x,y = vec_y))+ geom_point()+ geom_smooth(data = dt ,方法=nls,formula = y_expFct2(x,a,b,c), se = F,start = list(a = 1,b = 75,c = -5)

我总是遇到这样的错误:

方法(公式,data = data,weights = weight,...)中的错误:奇异梯度

$这可以写成两个线性参数( .lin1 和 .lin2 )和一个非线性参数( b ),如下所示: <$ p $ (1-exp(-x / b))+ c =(a + c) - a * exp(-x / b) = .lin1 + .lin2 * exp(-x / b)

其中 .lin1 = a + c 和 .lin2 = -a (所以 a = - .lin2 和 c = .lin1 + .lin2 )这让我们可以使用plinear,它只需要指定单个非线性参数的起始值(消除了如何设置其他参数的起始值的问题),并且尽管起始值 b = 75 远不及解决方案:

nls (y_cbind(1,exp(-x / b)),start = list(b = 75),alg =plinear)

下面是我们从 .lin2 的大小中看到问题严重缩放的结果:

> x< - c(77.87,87.76,68.6,66.29)> y< - c(1,1,0.8,0.6)> nls(y_cbind(1,exp(-x / b)),start = list(b = 75),alg =plinear)非线性回归模型模型:y〜cbind(1 ,exp(-x / b)) data:parent.frame()b .lin1 .lin2 3.351e + 00 1.006e + 00 -1.589e + 08 residual平方和:7.909e-05 收敛的迭代次数:9 实现的收敛容限:9.887e-07 > R.version.string [1]R version 2.14.2 Patched(2012-02-29 r58660)> win.version() [1]Windows Vista(build 6002)Service Pack 2

<编辑:添加示例运行和缩放评论。

I don't understand why I can't have a nls function for these data. I have tried with a lot of different start values and I have always the same error.

Here is what I have been doing:

expFct2 = function (x, a, b,c) { a*(1-exp(-x/b)) + c } vec_x <- c(77.87,87.76,68.6,66.29) vec_y <- c(1,1,0.8,0.6) dt <- data.frame(vec_x=vec_x,vec_y=vec_y) ggplot(data = dt,aes(x = vec_x, y = vec_y)) + geom_point() + geom_smooth(data=dt, method="nls", formula=y~expFct2(x, a, b, c), se=F, start=list(a=1, b=75, c=-5)

I have always this error:

Error in method(formula, data = data, weights = weight, ...) : singular gradient

解决方案

This can be written with two linear parameters (.lin1 and .lin2) and one nonlinear parameter (b) like this:

a*(1-exp(-x/b)) + c = (a+c) - a * exp(-x/b) = .lin1 + .lin2 * exp(-x/b)

where .lin1 = a+c and .lin2 = -a (so a = - .lin2 and c = .lin1 + .lin2) This lets us use "plinear" which only requires specification of a starting value for the single nonlinear parameter (eliminating the problem of how to set the starting values for the other parameters) and which converges despite the starting value of b=75 being far from that of the solution:

nls(y ~ cbind(1, exp(-x/b)), start = list(b = 75), alg = "plinear")

Here is the result of a run from which we can see from the size of .lin2 that the problem is badly scaled:

> x <- c(77.87,87.76,68.6,66.29) > y <- c(1,1,0.8,0.6) > nls(y ~ cbind(1, exp(-x/b)), start = list(b = 75), alg = "plinear") Nonlinear regression model model: y ~ cbind(1, exp(-x/b)) data: parent.frame() b .lin1 .lin2 3.351e+00 1.006e+00 -1.589e+08 residual sum-of-squares: 7.909e-05 Number of iterations to convergence: 9 Achieved convergence tolerance: 9.887e-07 > R.version.string [1] "R version 2.14.2 Patched (2012-02-29 r58660)" > win.version() [1] "Windows Vista (build 6002) Service Pack 2"

EDIT: added sample run and comment on scaling.

更多推荐

如何为nls函数找到好的开始值?

本文发布于:2023-07-26 09:30:37,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   函数   nls

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!