1

Following is an excerpt from my data set: relative frequency and size. As you can see from the open-circles, this is a Gaussian distribution. I am using the nls package in R to fit a nonlinear curve. My equation is Gaussian

or in plain text: c * e^(-(x - z)^2/l)

enter image description here

Here is how I got here

fit <- as.formula(y~c*(exp((-(x-z)^2/l))))
preview(fit_partial, data=mydata, start=list(c=0.005, x=mydata$x_values, z=130,l=2000))

The starting values seem reasonable. So I try to get a non-linear fit

nls_fit <- nls(fit, data=mydata, start=list(c=0.005, x=mydata$x_values, z=130, l=2000))

However, I'm thrown with an error

Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model

This is likely because my starting values are poor. Something else must be the issue though. Appreciate any help.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
mindhabits
  • 421
  • 1
  • 3
  • 10
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. But I don't think `x` should be in your start list. If you use a formula with a `data=` parameter it will be able to find the values in the data.frame. You are not tying to estimate the values of `x`. Those are given. – MrFlick Apr 25 '19 at 20:35
  • @MrFlick Ok, I will work to add more to my example. Yes, I am not trying to predict x; x comes from my data set. If I remove x from the start list i get the error ``parameters without starting value in 'data': x`` I have a very tenuous grasp of programming and R in general; can you clarify what you meant? – mindhabits Apr 25 '19 at 20:39
  • I think this example is well-written, but it's hard to troubleshoot without understanding what your data looks like. Can you take a small sample of it, reproduce the issue on the sample, and share that sample data in your post (using a function such as `dput(my_data)`)? – bschneidr Apr 25 '19 at 20:59

1 Answers1

1

As far as I can tell, your only problem is including x in your list of parameters, which is confusing R (I can't exactly tell you why ... something about the fact that it's not actually a parameter of the model ...). nls(fit, data=mydata, start=pars) works fine for me.

Simulate data:

fit <- as.formula(y~c*(exp((-(x-z)^2/l))))
mydata <- data.frame(x=80:200)
pars <- list(c=0.005, z=130,l=2000)
set.seed(101)
mydata$y_det <- eval(fit[[3]],
                     env=c(pars,as.list(mydata)))
mydata$y <- rnorm(nrow(mydata),mean=mydata$y_det,sd=0.0002)
plot(y~x,data=mydata) ## check

Try original fit:

nls_fit <- nls(fit, data=mydata, start=c(pars,list(x=mydata$x)))

Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model

Fit with parameters only (not x).

nls_fit <- nls(fit, data=mydata, start=pars)
lines(mydata$x,predict(nls_fit),col=2)
coef(nls_fit)
##           c            z            l 
## 4.963097e-03 1.302308e+02 2.035007e+03 

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Excellent solution. I was including x in the list of start parameters which was the source of the error. I followed your example and achieved similar results to your last figure (only with my real data). – mindhabits Apr 25 '19 at 22:02