R中的3参数非线性方程拟合
我试图在 R 中找到一种方法,它允许一般方程 a*b^x + c 的参数 (a, b, c),它提供对 3 个约束随机坐标/点 (p1, p2, p3) 的最佳拟合- 坐标分别为 x1/y1、x2/y2 和 x3/y3)。
这些坐标的约束是:
- x1 和 y3 都等于 0
- x3 和 y1 都是随机选择的,并且小于 1
- x2 被分配一个小于 x3 的随机值
- y2 被分配一个小于 y1 的随机值
我想找到一种能够生成 a、b 和 c 值的方法,生成一条接近 p1、p2 和 p3 的线。这只是使用 desmos(参见此处的示例 - https://www.desmos.com/calculator/ 4lmgazmrko),但我一直无法在 R 中找到解决方案。我尝试了以下方法:
x <- c(0, 0.7, 0.9)
y <- c(0.9, 0.8, 0)
df_test <- as.data.frame(cbind(x, y))
predict_y_nonlinearly <- function(beta, x){
beta[1]*(beta[2]^x) + beta[3]
}
a_nonlinearmodel <- function(beta, x, y){
y_hat <- predict_y_nonlinearly(beta, x)
sum((y-y_hat)^2)
}
beta <- optim(rnorm(3), a_nonlinearmodel, method = "SANN",
y = df_test$y, x = df_test$x)$par
predict_y_nonlinearly(beta, df_test$x)
但是优化函数似乎陷入局部最小值,并且很少产生正确的解决方案(即使当不同的使用“方法”设置)。我知道 nls 函数,但这需要选择起始值 - 我更喜欢一种在此阶段不需要手动输入的方法(因为 desmos 方法能够实现)。
谢谢
I'm trying to find a method in R which allows the parameters (a, b, c) of the general equation a*b^x + c which provides the best fit to 3 constrained random coordinates/points (p1, p2, p3 - with coordinates x1/y1, x2/y2 and x3/y3 respectively).
The constraints to these coordinates are:
- x1 and y3 are both equal to 0
- x3 and y1 are randomly both randomly selected, and less than 1
- x2 is assigned a random value less than x3
- y2 is assigned a random value less than y1
I want to find a method which is able to generate values for a, b, and c which produces a line close to p1, p2 and p3. This is simply using desmos (see here for an example - https://www.desmos.com/calculator/4lmgazmrko) but I haven't been able to find a solution in R. I've tried the following:
x <- c(0, 0.7, 0.9)
y <- c(0.9, 0.8, 0)
df_test <- as.data.frame(cbind(x, y))
predict_y_nonlinearly <- function(beta, x){
beta[1]*(beta[2]^x) + beta[3]
}
a_nonlinearmodel <- function(beta, x, y){
y_hat <- predict_y_nonlinearly(beta, x)
sum((y-y_hat)^2)
}
beta <- optim(rnorm(3), a_nonlinearmodel, method = "SANN",
y = df_test$y, x = df_test$x)$par
predict_y_nonlinearly(beta, df_test$x)
But the optimisation function appears to get stuck in local minima, and rarely produces the correct solution (even when a different 'method' setting is used). I'm aware of the nls function, but this requires starting values to be chosen - I'd prefer a method which does not require manual input at this stage (as the desmos method is able to achieve).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定两个零约束,我们可以通过分析将其简化为单参数问题:
因此,我们有一个预测
y
的单参数函数,其中包含x1 = y3 = 0
> 约束:平方和目标函数:
可视化:
现在使用
optimize()
对于一维优化(我们仍然需要指定一个起始间隔,尽管不是特定的起始点)。结果:
Given the two zero constraints, we can reduce this analytically to a one-parameter problem:
So we have a one-parameter function that predicts
y
, incorporating thex1 = y3 = 0
constraints:A sum-of-squares target function:
Visualize:
Now use
optimize()
for 1D optimization (we still need to specify a starting interval, although not a specific starting point).Results: