R:nls()错误。 "初始参数估计时的奇异梯度矩阵"

时间:2018-01-31 20:36:17

标签: r regression nls

我在下面有一个简单的例子(它没有用)试图使用默认算法(Gauss-Newton)进行多变量拟合。我得到错误:" nlsModel中的错误(公式,mf,start,wts):初始参数估计时的奇异梯度矩阵"。

## Defining the two independent x variables, and the one dependent y variable.
x1 = 1:100*.01
x2 = (1:100*.01)^2
y1 = 2*x1 + 0.5*x2

## Putting into a data.frame for nls() funcion.
df = data.frame(x1, x2, y1)

## Starting parameters: a = 2.1, b = 0.4 (and taking c = 0)
fit_results <-nls(y1 ~ x1*a + x2*b +c, data=df, start=c(a=2.1, b=0.4, c=0))

注意:即使我设置了a = 2,并且b = 0.5,我仍然会收到相同的错误消息。

1 个答案:

答案 0 :(得分:2)

感谢Brian,不知道如何评论所选答案。这是有效的代码......结果我需要在y1因变量中添加更多随机性。

## Defining the two independent x variables, and the one dependent y variable.
x1 = 1:100*0.1
x2 = runif(100,0,10)
y1 = 2*x1 + 0.5*x2*runif(100,0.9,1.1)

## Putting into a data.frame for nls() funcion.
df = data.frame(x1, x2, y1)

fit_results <-nls(y1 ~ x1*a + x2*b +c, data=df, start=c(a=2.1, b=0.4, c=0))