r - 强制nls拟合通过指定点的曲线

标签 r curve-fitting nls

我正在尝试将 Boltzmann sigmoid 1/(1+exp((x-p1)/p2)) 拟合到这个小型实验数据集:

xdata <- c(-60,-50,-40,-30,-20,-10,-0,10)
ydata <- c(0.04, 0.09, 0.38, 0.63, 0.79, 1, 0.83, 0.56)

我知道这很简单。例如,使用 nls:

fit <-nls(ydata ~ 1/(1+exp((xdata-p1)/p2)),start=list(p1=mean(xdata),p2=-5))

我得到以下结果:

Formula: ydata ~ 1/(1 + exp((xdata - p1)/p2))

Parameters:
   Estimate Std. Error t value Pr(>|t|)    
p1  -33.671      4.755  -7.081 0.000398 ***
p2  -10.336      4.312  -2.397 0.053490 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1904 on 6 degrees of freedom

Number of iterations to convergence: 13 
Achieved convergence tolerance: 7.079e-06

但是,我需要(由于理论原因)拟合曲线精确地通过点(-70, 0)。尽管上面显示的拟合表达式的值在 x = -70 处接近零,但它并不完全为零,这不是我想要的。

所以,问题是:有没有办法告诉 nls(或其他一些函数)拟合相同的表达式,但强制它通过指定的点?

更新:

正如评论中提到的那样,使用我提供的函数(Boltzmann sigmoid)强制拟合通过点 (-70,0) 在数学上是不可能的。另一方面,@Cleb 和@BenBolker 解释了如何强制拟合通过任何其他点,例如 (-50, 0.09)。

最佳答案

正如我们在您的问题下方的评论中所讨论的那样,使用您提供的函数(没有偏移量)是不可能强制拟合通过 0 的。

但是,您可以通过为单个数据点设置 weights 来强制曲线通过其他数据点。所以例如如果您给数据点 A 的权重等于 1,数据点 B 的权重等于 1000,则数据点 B 对拟合更重要(就将要最小化的残差总和的贡献而言)比 A 和配合将因此被迫通过 B。

这是完整的代码,我将在下面对其进行更详细的解释:

# your data
xdata <- c(-60, -50, -40, -30, -20, -10, -0, 10)
ydata <- c(0.04, 0.09, 0.38, 0.63, 0.79, 1, 0.83, 0.56)
plot(xdata, ydata, ylim=c(0, 1.1))
fit <-nls(ydata ~ 1 / (1 + exp((xdata - p1) / p2)), start=list(p1=mean(xdata), p2=-5))

# plot the fit
xr = data.frame(xdata = seq(min(xdata), max(xdata), len=200))
lines(xr$xdata, predict(fit, newdata=xr))

# set all weights to 1, do the fit again; the plot looks identical to the previous one
we = rep(1, length(xdata))
fit2 = nls(ydata ~ 1 / (1 + exp((xdata - p1) / p2)), weights=we, start=list(p1=mean(xdata) ,p2=-5))
lines(xr$xdata, predict(fit2, newdata=xr), col='blue')

# set weight for the data point -30,0.38, and fit again
we[3] = 1000
fit3 = nls(ydata ~ 1 / (1 + exp((xdata - p1) / p2)), weights=we, start=list(p1=mean(xdata), p2=-5))
lines(xr$xdata, predict(fit3, newdata=xr), col='red')
legend('topleft', c('fit without weights', 'fit with weights 1', 'weighted fit for -40,0.38'), 
       lty=c(1, 1, 1),
       lwd=c(2.5, 2.5, 2.5),
       col=c('black', 'blue', 'red'))

输出如下;如您所见,拟合现在通过所需的数据点(红线):

enter image description here

那么这是怎么回事:我首先像你一样进行拟合,然后我进行权重拟合,所有权重都设置为 1;因此,该图看起来与之前的相同,蓝线隐藏了黑线。然后 - 对于 fit3 - 我将第三个数据点的权重更改为 1000,这意味着它现在对于最小二乘拟合比其他点更“重要”并且新的拟合通过这个数据点(红线)。

这也是我更改行的第二个示例

we[3] = 1000

we[2] = 1000

强制拟合通过第二个数据点:

enter image description here

如果您想获得有关weights 参数的更多信息,您可以在此处阅读:documentation

关于r - 强制nls拟合通过指定点的曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837610/

相关文章:

r - 使用 nls 在 R 中拟合曲线

r - 使用 nls() 进行非线性拟合在初始参数估计时给了我奇异的梯度矩阵。为什么?

r - "Error in mktdata[, keep] : number of dimensions incorrect "由于存货 "T"指的是 TRUE?

r - ggplot2:如何为facet_wrap中的每个方面单独设置轴中断?

math - 可能的正弦波最小二乘曲线拟合(使用 GSL)?

python - 将固定矩形拟合到点集

r - 如何使用 rvest R 从谷歌新闻中获取头条新闻?

r - 安装软件包时不区分大小写的软件包安装(忽略大小写)

python - 曲线拟合数据问题

r - 尝试用 R 和 nls 在一个有条件的函数上拟合数据