非线性回归故障排除

问题描述 投票:1回答:1

以下是我的数据集的摘录:相对频率和大小。从开圆圈可以看出,这是一个高斯分布。我在R中使用nls包来拟合非线性曲线。我的等式是Gaussian

或者用明文:c * e^(-(x - z)^2/l)

enter image description 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))

起始值似乎合理。所以我尝试得到非线性拟合

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

但是,我被抛出了一个错误

numericDeriv中的错误(表单[[3L]],名称(ind),env):评估模型时产生的缺失值或无穷大

这可能是因为我的起始值很差。但是还有其他问题。感谢任何帮助。

r nls non-linear-regression
1个回答
1
投票

据我所知,你唯一的问题是在你的参数列表中包含x,这让人感到困惑R(我不能确切地告诉你为什么......关于它实际上并不是模型参数的事实...... )。 nls(fit, data=mydata, start=pars)对我来说很好。

模拟数据:

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

尝试原始适合:

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

numericDeriv中的错误(表单[[3L]],名称(ind),env):评估模型时产生的缺失值或无穷大

仅适合参数(不是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

© www.soinside.com 2019 - 2024. All rights reserved.