R中的3参数非线性方程拟合

发布于 2025-01-09 05:20:53 字数 1078 浏览 1 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-16 05:20:53

给定两个零约束,我们可以通过分析将其简化为单参数问题:

x1 = 0 → y1 = a + c → c = y1-a
y3 = 0 → 0 = a*b^x3 + (y1-a)
→ a*(b^x3 - 1) = -y1
→ a = y1/(1-b^x3)

因此,我们有一个预测 y 的单参数函数,其中包含 x1 = y3 = 0 > 约束:

predfun <- function(b = 1, x, y)  {
  a <- y[1]/(1-b^x[3])
  c <- y[1] - a
  a*b^x +c
}

平方和目标函数:

target <- Vectorize(function(b) sum((y - predfun(b, x, y))^2))

可视化:

curve(target, from = -10000, to = 100000, log = "y")

曲线显示负值的分歧,在 6e+04 附近急剧最小值

现在使用 optimize() 对于一维优化(我们仍然需要指定一个起始间隔,尽管不是特定的起始点)。

optimize(target, c(-10000, 1000000))

结果:

$minimum
[1] 58928.93

$objective
[1] 2.066598e-20

Given the two zero constraints, we can reduce this analytically to a one-parameter problem:

x1 = 0 → y1 = a + c → c = y1-a
y3 = 0 → 0 = a*b^x3 + (y1-a)
→ a*(b^x3 - 1) = -y1
→ a = y1/(1-b^x3)

So we have a one-parameter function that predicts y, incorporating the x1 = y3 = 0 constraints:

predfun <- function(b = 1, x, y)  {
  a <- y[1]/(1-b^x[3])
  c <- y[1] - a
  a*b^x +c
}

A sum-of-squares target function:

target <- Vectorize(function(b) sum((y - predfun(b, x, y))^2))

Visualize:

curve(target, from = -10000, to = 100000, log = "y")

curve showing divergence at negative values, sharp minimum around 6e+04

Now use optimize() for 1D optimization (we still need to specify a starting interval, although not a specific starting point).

optimize(target, c(-10000, 1000000))

Results:

$minimum
[1] 58928.93

$objective
[1] 2.066598e-20
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文