nlm起始值的选择问题
需要使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
FFF
时,在 try 块内,如果nlm
成功完成,则分配fit
,并且stopifnot
条件被激活,抛出错误。疯狂猜测,您的意思是
为了将来的参考,与
try
一起使用的标准代码块是When you call
FFF
, inside the try block ifnlm
successfully completes, thenfit
is assigned, and thestopifnot
condition is activated, throwing an error.Wildly guessing, did you mean
For future reference, a standard chunk of code for use with
try
is