【问题标题】:R: nls() error. "singular gradient matrix at initial parameter estimates"R:nls() 错误。 “初始参数估计的奇异梯度矩阵”
【发布时间】:2018-07-11 02:03:25
【问题描述】:

我在下面有一个简单的示例(它不起作用),它尝试使用默认算法(高斯-牛顿)进行多元拟合。我收到错误消息:“nlsModel(formula, 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,我仍然会收到相同的错误消息。

【问题讨论】:

  • 我假设你的真实数据比这个例子更复杂。这里没有随机分量;这是一个完美的契合,这可能会导致算法失败。此外,x1x2 在这里完全共线,这将导致失败。请参阅?nls 中的警告部分。

标签: r regression nls


【解决方案1】:

谢谢布赖恩,不知道如何评论选定的答案。这是有效的代码...结果我需要在 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2023-01-27
    • 2014-08-27
    • 2023-04-03
    相关资源
    最近更新 更多