0

I have a simple example below (which doesn't work) that attempts to do multivariate fit using the default algorithm (Gauss-Newton). I get the error: "Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates".

## 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))

Note: even when I set a = 2, and b = 0.5 above, I still get the same error message.

Entropy
  • 133
  • 2
  • 12
  • 4
    I assume your real data are more complex than this example. Here there's no random component; it's a perfect fit, which can cause the algorithm to fail. Additionally, `x1` and `x2` are perfectly collinear here, which will cause failure. See the **Warning** section in `?nls`. – Brian Jan 31 '18 at 20:50

1 Answers1

3

Thanks Brian, not sure how to make a comment the selected answer. Here is code that works... turns out I needed to add more randomness in the y1 dependent variable.

## 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))
Entropy
  • 133
  • 2
  • 12