1

I would like to fit a two-term exponential model to my data given as (x,y), i.e.

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

In essence, I need to replicate Matlab's exp2 model type in R calculated as

f = fit(x, y, 'expo')
saladi
  • 3,103
  • 6
  • 36
  • 61
Mel Z.
  • 21
  • 3
  • Do you have any data sample to show? Is `x` negative? Is `y` negative? – Severin Pappadeux Mar 08 '16 at 00:28
  • 1
    @Benjamin - just to clarify, `?nls` is a function included with R by default – thelatemail Mar 08 '16 at 00:30
  • You've listed the nls function (stats package), which is a good place to start. Have a look at the documentation, and show us what you'e tried. See http://robinlovelace.net/2013/10/23/nls-demonstation.html for a few hints. – Benjamin Mar 08 '16 at 00:37
  • You should encourage people to submit data so they have a MWE. I'll edit the question to remove the extraneous material. – IRTFM Mar 08 '16 at 00:44
  • @42-: I agree, that's exactly what Severin Pappadeux and I suggested. – Benjamin Mar 08 '16 at 00:49

1 Answers1

2

This post does a good job explaining how to fit an abstract model like that. The jist of it is- use nls() to fit a "Nonlinear Least Squares" model:

# 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)

and the function should do the rest...

The hard part is finding estimates to your model that will not give you an error when inputting this. The link provides insight into this. Good luck!

Edit: Made the changes @Ben Bolker suggested; also, didn't realize mpg was in ggplot2 and not base R, thanks for the clarification.

Community
  • 1
  • 1
cgage1
  • 579
  • 5
  • 15
  • You might want to define the variable before you use it... and explain how to get to mpg. – Benjamin Mar 08 '16 at 01:56
  • `mpg` is in the `ggplot2` package, not in base R. And it would be better to use `m1 = nls(displ ~ a*exp(b*cty) + c*exp(d*cyl),data =mpg, ...)` – Ben Bolker Mar 08 '16 at 02:51
  • ... and try `insertList = list(a=1,b=-0.01,c=1,d=-0.01)` for starting conditions that *do* work ... – Ben Bolker Mar 08 '16 at 02:52