[英]R : catching errors in `nls`
I'm fitting some exponential data using nls
. 我使用
nls
拟合一些指数数据。
The code I'm using is: 我正在使用的代码是:
fit <- nls(y ~ expFit(times, A, tau, C), start = c(A=100, tau=-3, C=0))
expFit
is defined as expFit
定义为
expFit <- function(t, A, tau, C)
{
expFit <- A*(exp(-t/tau))+C
}
This works well for most of my data, for which the starting parameters provided (100, -3 and 0) work well. 这适用于我的大多数数据,其中提供的起始参数(100,-3和0)运行良好。 Sometimes, though, I have data that doesn't go well with those parameters and I get errors from
nls
(eg "singular gradient" or things like that). 但有时,我的数据与这些参数不
nls
,我从nls
得到错误(例如“奇异梯度”或类似的东西)。 How do I "catch" these errors? 我如何“捕获”这些错误?
I tried to do something like 我试着做点什么
fit <- NULL
fit <- nls(...)
if (is.null(fit))
{
// Try nls with other starting parameters
}
But this won't work because nls
seems to stop the execution and the code after nls
will not execute... 但这不起作用,因为
nls
似乎停止执行,而nls
之后的代码将不会执行...
Any ideas? 有任何想法吗?
Thanks nico 谢谢你
I usually use this trick: 我通常使用这个技巧:
params<-... # setup default params.
while(TRUE){
fit<-NULL
try(fit<-nls(...)); # does not stop in the case of error
if(!is.null(fit))break; # if nls works, then quit from the loop
params<-... # change the params for nls
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.