非线性回归故障排除
Non Linear Regression Troubleshooting
以下是我的数据集的摘录:相对频率和大小。从空心圆圈可以看出,这是一个高斯分布。我在 R 中使用 nls
包来拟合非线性曲线。
我的等式是
或纯文本:c * e^(-(x - z)^2/l)
我是这样过来的
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))
但是,我遇到了错误
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
这可能是因为我的初始值很差。不过,肯定还有其他问题。感谢任何帮助。
据我所知,您唯一的问题是在参数列表中包含 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)))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
仅适合参数(不是 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
以下是我的数据集的摘录:相对频率和大小。从空心圆圈可以看出,这是一个高斯分布。我在 R 中使用 nls
包来拟合非线性曲线。
我的等式是
或纯文本:c * e^(-(x - z)^2/l)
我是这样过来的
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))
但是,我遇到了错误
Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model
这可能是因为我的初始值很差。不过,肯定还有其他问题。感谢任何帮助。
据我所知,您唯一的问题是在参数列表中包含 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)))
Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model
仅适合参数(不是 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