1

I tried to fit my data with a gaussian curve using nls. Because that didn't work, i tried to make an easy example to see what goes wrong:

>x=seq(-4,4,0.1)
>y=2*dnorm(x-0.4,2)+runif( length(x) , min = -0.01, max = 0.01)
>df=data.frame(x,y)
>m <- nls(y ~ k*dnorm(x-mu,sigma), data = df, start = list(k=2,mu=0.4,sigma=2))

Error in nlsModel(formula, mf, start, wts, upper) :   singular gradient 
matrix at initial parameter estimates
> m <- nls(y ~ k*dnorm(x-mu,sigma), data = df, start == list(k=1.5,mu=0.4,sigma=2))

Error in nlsModel(formula, mf, start, wts, upper) :   singular gradient 
matrix at initial parameter estimates

Why doesn't this work?

  • First of all, i'm not sure you are using `dnorm` correctly. It's signature is `dnorm(x,mu,sigma)`. Your example is confusing because you have the `mu` variable in the x parameter and `sigma` in the mean parameter. There is no change in variance happening in this example. Is that what you intended? If so, that's a very confusing choice of variable name. – MrFlick Jul 06 '14 at 21:46

1 Answers1

1

First please use set.seed to make your example reproducible. Second I think you meant dnorm(x, 0.4, 2) and not dnorm(x-0.4, 2). These are not the same since in the x-0.4 case the mean of x-0.4 is 2 and in the other case the standard devaiation is 2. If we make this change then it works:

set.seed(123)
x=seq(-4,4,0.1)
y=2*dnorm(x, 0.4, 2)+runif( length(x) , min = -0.01, max = 0.01)
df=data.frame(x,y)
nls(y ~ k*dnorm(x, mu,sigma), data = df, start = list(k=2,mu=0.4,sigma=2))

giving:

Nonlinear regression model
  model: y ~ k * dnorm(x, mu, sigma)
   data: df
     k     mu  sigma 
2.0034 0.3914 2.0135 
 residual sum-of-squares: 0.002434

Number of iterations to convergence: 2 
Achieved convergence tolerance: 5.377e-06
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Right, the error was likely from a lack of identify-ability. It's true that `2*dnorm(x-a,2)==2*dnorm(x,2+a)` for all `a` so it's impossible to determine what belongs to the `mu` variable and what belongs to `sigma` in the original formulation. – MrFlick Jul 06 '14 at 21:50
  • sd is the third parameter, not the second. The problem in the original formula is that both mu and sigma were specifying the mean. – G. Grothendieck Jul 06 '14 at 21:52
  • Yes, I realize that for the `dnorm` function. I was just using the variable names as the OP assigned them. I was just trying to make it clear why NLS wasn't able to solve the formula as originally written. – MrFlick Jul 06 '14 at 21:55