r - 处理 nls 中的 0 错误 - R 脚本

标签 r nls nonlinear-functions

有没有办法让我的 nls 在进行非线性拟合时具有 0 残差?我的数据中有这样的情况,其中所做的拟合应该有 0 个错误,但 nls 总是失败并吐出错误。

谁能告诉我:

  1. 我如何测试这是否是 nls 吐出的错误?
  2. 如何允许 0 个错误案例? (完美契合)

这是我的 nls 电话:

fit <- nls(y ~ ifelse(g, m1 * (x - x0) + y0, m2 * (x - x0) + y0),
            start = c(m1 = -1, m2 = 1, y0 = 0, x0 = split),
            algorithm = "port",
            lower = c(m1 = -Inf, m2 = -Inf, y0 = -Inf, x0 = split),
            upper = c(m1 = Inf, m2 = Inf, y0 = Inf, x0 = (split+1)),
            data=data.frame(x,y))

最佳答案

作为mentioned在之前的回答中,?nls 明确声明您不应将 nls 用于 0 错误数据。直接引用您正在使用的函数的帮助文件:

Do not use nls on artificial "zero-residual" data.

The nls function uses a relative-offset convergence criterion that compares the numerical imprecision at the current parameter estimates to the residual sum-of-squares. This performs well on data of the form

y = f(x, θ) + eps

(with var(eps) > 0). It fails to indicate convergence on data of the form

y = f(x, θ)

because the criterion amounts to comparing two components of the round-off error. If you wish to test nls on artificial data please add a noise component, as shown in the example below.

一个有潜在危险的选择是使用 warnOnly = TRUE 强制 nls 在收敛之前返回,仅带有警告(无错误):

x <- -(1:100)/10
y <- 100 + 10 * exp(x / 2)
nlmod <- nls(y ~  Const + A * exp(B * x),control = nls.control(warnOnly = TRUE))

上面的例子也几乎直接取自?nls

关于r - 处理 nls 中的 0 错误 - R 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133697/

相关文章:

r - 如何为海洋着色?

r - 如何在R中提取文本文件中间的数字

sql - 为什么小写的 'i' 在执行 NLS_UPPER 后映射到 '?'

c# - Azure 应用服务似乎在 .NET 5 模式下启用了 NLS

r - nls.lm : Non-numeric argument to binary operator error but all my args are numeric

python - 使用 Python 求解 4 参数常数(罗德巴德方程)

r - 在 R 中合并列具有相同值但大小写不同的行

performance - 在R中创建循环矩阵的有效方法

python - 在Python中求解动态数非线性方程

python - 比较 python 和 matlab 中的 fsolve 结果