【问题标题】:Is there a R function that finds parameters to arbitrary models?是否有一个 R 函数可以找到任意模型的参数?
【发布时间】:2020-02-28 03:36:12
【问题描述】:

我正在尝试拟合模型 y=c0*log(c1*x+1),其中 y 和 x 是我的数据,c0 和 c1 是模型的参数。我知道 Matlab 中的“fit”命令可以做到这一点,但我正试图在 R 中做到这一点。我想知道是否有人可以就可以实现这一点的函数提供建议。

model <- y ~ c0*log(c1*x + 1)

我期待一个对数曲线。

【问题讨论】:

  • 你可能想使用glm

标签: r model-fitting


【解决方案1】:

这是一个非线性回归问题,其中 y 等于右手边加上一个误差。

要拟合这样的模型,请在基数 R 中使用 nls(有关更多信息,请参阅 ?nls)或许多其他非线性回归函数中的任何一个。见https://cran.r-project.org/web/views/Optimization.html。根据某些注意事项nls 将找到使给定模型和起始值的误差平方和最小化的参数。

# create test data
set.seed(123)
c0 <- c1 <- 1
x <- 1:10
y <- c0 * log(c1 * x + 1) + rnorm(10)/10

# fit model to the data
fm <- nls(y ~ c0 * log(c1 * x + 1), start = list(c0 = mean(y), c1 = 1))
fm

给予:

Nonlinear regression model
  model: y ~ c0 * log(c1 * x + 1)
   data: parent.frame()
    c0     c1 
0.9207 1.2210 
 residual sum-of-squares: 0.07622

Number of iterations to convergence: 5 
Achieved convergence tolerance: 4.887e-06

以图形方式评估拟合度:

plot(y ~ x, pch = 20)
lines(fitted(fm) ~ x, col = "red")

【讨论】:

    【解决方案2】:

    试试fit.models(查看:https://cran.r-project.org/web/packages/fit.models/fit.models.pdf)。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-06
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 2017-10-30
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多