nlm起始值的选择问题

发布于 2024-12-01 04:21:22 字数 1588 浏览 0 评论 0原文

需要使用nlm函数估计两个参数;

fit<-nlm(hood2par,c(x01[i],x02[j]),iterlim=300, catch=x[,c(3,4,5)],sp=.5)

其中 hood2par 是修改后的逻辑。

nlm 的收敛取决于这些参数的起始值。为了找到这样的初始值,我自动生成两个起始值向量,

x01 = seq(-10,-20,-0.1)
x02 = seq(0.1,0.9,0.01)

接下来我创建一个包含在 double for() 中的例程,以查找导致函数收敛的值:

for (i in 1:length(x01)) { for (j in 1:length(x02)) {

fit <- NULL
try(fit <- nlm(hood2par, c(x01[i],x02[j]), iterlim = 300, catch = x[,c(3,4,5)],
               sp = .5), 
    silent = TRUE)
stopifnot(is.null(fit))}} 

我遇到的问题是,当我包含前一个例程时在函数中:

FFF <- function(x01, x02, catch){
    for (i in 1:length(x01)) { 
        for (j in 1:length(x02)) {
            fit <- NULL
            try(fit <- nlm(hood2par, c(x01[i], x02[j]), iterlim = 300,
                           catch = x[,c(3,4,5)], sp = .5), 
               silent = TRUE) # does not stop in the case of err
            stopifnot(is.null(fit))
        }
     }  
return(fit)
}

我无法从 FFF() 获取“fit”值:

> fit.fff<-FFF(x01,x02,catch)
#Error: is.null(fit) is not TRUE 

>fit.fff
fit.fff
Error: object 'fit.fff' not found

当 fit 不为 NULL 时,我使用 stopifnot(is.null(fit)) 停止循环(因为 fit 为之前定义为 NULL 对象 尝试(...))。关于你分享的尝试代码,我只需要这个;

res <- try(some_expression)
if(inherits(res, "try-error"))
{
  #some code to keep loops running
} else
{
  #stop the loops and gather "res" 
}

我尝试在条件的第二个参数中包含 break 函数,但它无法在我的 R 版本中运行...知道吗?

Need to estimate two parameters using the nlm function;

fit<-nlm(hood2par,c(x01[i],x02[j]),iterlim=300, catch=x[,c(3,4,5)],sp=.5)

where hood2par is a modified logistic

The convergence of nlm depends on the starting values ​​of these parameters. To find such initial values I ​​automatically generate two vectors of starting values

x01 = seq(-10,-20,-0.1)
x02 = seq(0.1,0.9,0.01)

next I create a routine included in a double for() to find the values ​​that lead to the convergence of the function:

for (i in 1:length(x01)) { for (j in 1:length(x02)) {

fit <- NULL
try(fit <- nlm(hood2par, c(x01[i],x02[j]), iterlim = 300, catch = x[,c(3,4,5)],
               sp = .5), 
    silent = TRUE)
stopifnot(is.null(fit))}} 

The problem I have is that when I include the previous routine in a function:

FFF <- function(x01, x02, catch){
    for (i in 1:length(x01)) { 
        for (j in 1:length(x02)) {
            fit <- NULL
            try(fit <- nlm(hood2par, c(x01[i], x02[j]), iterlim = 300,
                           catch = x[,c(3,4,5)], sp = .5), 
               silent = TRUE) # does not stop in the case of err
            stopifnot(is.null(fit))
        }
     }  
return(fit)
}

I can´t get the 'fit' values from FFF():

> fit.fff<-FFF(x01,x02,catch)
#Error: is.null(fit) is not TRUE 

>fit.fff
fit.fff
Error: object 'fit.fff' not found

I used stopifnot(is.null(fit)) to stop the loops when fit is not NULL (as fit is defined as a NULL object before try(...)). Regarding the try code you have shared, I just need this;

res <- try(some_expression)
if(inherits(res, "try-error"))
{
  #some code to keep loops running
} else
{
  #stop the loops and gather "res" 
}

I tried to include the break function in the second argument of the condictional, but it doesn´t run in my R version...Any idea??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-12-08 04:21:22

当您调用 FFF 时,在 try 块内,如果 nlm 成功完成,则分配 fit,并且 stopifnot条件被激活,抛出错误。

疯狂猜测,您的意思是

stopifnot(!is.null(fit))

为了将来的参考,与 try 一起使用的标准代码块是

res <- try(some_expression)
if(inherits(res, "try-error"))
{
  #some error handling code
} else
{
  #normal execution
}

When you call FFF, inside the try block if nlm successfully completes, then fit is assigned, and the stopifnot condition is activated, throwing an error.

Wildly guessing, did you mean

stopifnot(!is.null(fit))

For future reference, a standard chunk of code for use with try is

res <- try(some_expression)
if(inherits(res, "try-error"))
{
  #some error handling code
} else
{
  #normal execution
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文