【问题标题】:How to perform a nonlinear regression of a complex function that has a summation using R?如何使用 R 对具有求和的复杂函数执行非线性回归?
【发布时间】:2021-09-27 05:11:56
【问题描述】:

我有以下功能:

在这个函数中,参数R是一个常数,值为22.5。我想使用非线性回归(nls() 函数)估计参数 A 和 B。我做了几次尝试,但都没有成功。我对 R 中的这种类型的操作不是很熟悉,所以我需要你的帮助。

另外,如果可能的话,我还想使用ggplot2 绘制这个函数。

# Initial data

x <- c(0, 60, 90, 120, 180, 240)
y <- c(0, 0.967676, 1.290101, 1.327099, 1.272404, 1.354246)
R <- 22.5

df <- data.frame(x, y)

f <- function(x) (1/(n^2))*exp((-B*(n^2)*(pi^2)*x)/(R^2))

# First try

nls(formula = y ~ A*(1-(6/(pi^2))*sum(f, seq(1, Inf, 1))),
    data = df,
    start = list(A = 1,
                 B = 0.7))

Error in seq.default(1, Inf, 1) : 'to' must be a finite number

# Second try

nls(formula = y ~ A*(1-(6/(pi^2))*integrate(f, 1, Inf)),
    data = df,
    start = list(A = 1,
                 B = 0.7))

Error in f(x, ...) : object 'n' not found

【问题讨论】:

  • 我认为 R 不能充分近似 sum(1, Inf, 1),我认为您需要找到 sum(1/n^2) 的近似值并使用它来代替。
  • @r2evans 我注意到了这一点,所以我也测试了integrate() 函数。
  • @r2evans 我认为sum(1/n^2) 的近似值是不够的,因为指数函数内部也出现了n^2
  • 你需要先使用一些数学知识。
  • 你说得完全正确,丹尼尔。我的观点是,如果您没有预先确定近似形式,R 就不会像那样进行渐近(无限)计算。

标签: r sum regression non-linear-regression nls


【解决方案1】:

您可以使用有限和近似。使用 25 个术语:

f <- function(x, B, n = 1:25) sum((1/(n^2))*exp((-B*(n^2)*(pi^2)*x)/(R^2)))
fm <- nls(formula = y ~ cbind(A = (1-(6/pi^2))* Vectorize(f)(x, B)),
    data = df,
    start = list(B = 0.7),
    alg = "plinear")
fm

给予:

Nonlinear regression model
  model: y ~ cbind(A = (1 - (6/pi^2)) * Vectorize(f)(x, B))
   data: df
       B   .lin.A 
-0.00169  1.39214 
 residual sum-of-squares: 1.054

Number of iterations to convergence: 12 
Achieved convergence tolerance: 9.314e-06

模型似乎不太适合数据(下图中的实线);然而,逻辑模型似乎运作良好(虚线)。

fm2 <- nls(y ~ SSlogis(x, Asym, xmid, scal), df)

plot(y ~ x, df)
lines(fitted(fm) ~ x, df)
lines(fitted(fm2) ~ x, df, lty = 2)
legend("bottomright", c("fm", "fm2"), lty = 1:2)

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2017-02-04
    • 2012-03-14
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多