在我的 r 脚本中,我执行 nls 来获取合适的值:
fit <- nls(...)
然后,我通过执行以下操作来测试 nls 是否成功:
if(is.na(fit)) {
print("succeeded")
}
但我收到警告:
the condition has length > 1 and only the first element will be used
我这样做错了吗?如果是这样,我该怎么办?如果没有,如何删除警告?谢谢!
最佳答案
如果拟合失败,
nls
会引发错误。因此,在 try(nls(...))
之后的 is.null
是正确的方法。
这是我在使用nls
适合不确定数据时使用的一段代码:
fit <- NULL
while (TRUE) {
start <- list(...) # try somewhat randomized initial parameter
try(fit <- nls(..., start = start)) # performe nls
if (!is.null(fit)) break;
}
关于R 脚本问题 - is.na 告诉我条件长度 > 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7135290/