简体   繁体   中英

Non Linear Regression Troubleshooting

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高斯

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

在此处输入图片说明

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.

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 

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM