如何拟合 R 中的两项指数?

How to fit a two-term exponential in R?

我想用 (x,y) 形式对我的数据拟合一个两项指数模型,即

f(x) = a * exp(b * x) + c * exp(d * x)

本质上,我需要在 R 中复制 Matlab 的 exp2 模型类型,计算为

f = fit(x, y, 'expo')

This post 很好地解释了如何拟合这样的抽象模型。它的要点是-使用 nls() 来拟合 "Nonlinear Least Squares" 模型:

# Using the mpg data in ggplot2
library(ggplot2)
# Create list of initial estimates
insertList = list(a=1,b=-0.01,c=1,d=-0.01)
# fit model
m1 = nls(displ ~ a*exp(b*cty) + c*exp(d*cyl),data =mpg, start = insertList)

函数 应该 完成剩下的事情...

困难的部分是找到你的模型的估计值,在输入时不会给你一个错误。 link 提供了对此的洞察力。祝你好运!

编辑:根据@Ben Bolker 的建议进行更改;另外,没有意识到 mpg 在 ggplot2 而不是 base R 中,感谢您的澄清。