【发布时间】:2020-11-24 01:25:24
【问题描述】:
我需要针对作物/年份数据进行 nls 拟合(请参阅 Question on nls fit in R - why is this such a strange fit?)。
我从这些数据开始,并使用来自常规线性模型拟合的系数作为初始参数估计值。
x <- c(1979L, 1980L, 1981L, 1982L, 1983L, 1984L, 1985L, 1986L, 1987L,
1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1997L, 1998L,
2000L, 2001L, 2002L, 2003L, 2004L, 2012L, 2014L, 2018L)
y <- c(94.4, 100, 120.6, 97.3, 110, 110, 80, 110, 117, 115, 50, 120,
68.4, 137, 83, 106, 124, 113, 95.8, 115.7, 60, 105, 60, 74, 95.7,
100)
mod_lm <- lm(y~x)
我现在在使用 minpack.lm 包时遇到此错误,
library(minpack.lm)
mod_nls <- nlsLM(y ~ a+x^b, start=list(a = mod_lm$coefficients[1],b=mod_lm$coefficients[2]))
Error in nlsModel(formula, mf, start, wts) :
singular gradient matrix at initial parameter estimates
或者这个,如果我尝试使用 nls2 包。
library(nls2)
mod_nls <- nls2(y ~ a+x^b, start=list(a = mod_lm$coefficients[1],b=mod_lm$coefficients[2]))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
我假设问题是初始参数估计不佳。最初如何获得更好的参数估计?谢谢。
【问题讨论】:
-
我得到了同样的错误,但在使用
x <- 1:26时没有。在不使用起始参数时,它似乎也能正常工作。 -
您的解决方案运行良好(只需一次调整)。无论如何,谢谢。
标签: r non-linear-regression nls