有没有办法让我的 nls 在进行非线性拟合时具有 0 残差?我的数据中有这样的情况,其中所做的拟合应该有 0 个错误,但 nls 总是失败并吐出错误。
谁能告诉我:
- 我如何测试这是否是 nls 吐出的错误?
- 如何允许 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/