I have a small dataframe that has a exponentially decreasing trend. I would like to extract a formula based on the data in my dataframe? Is this possible, and if so, how can I go about extracting the formula?
Asked
Active
Viewed 2,083 times
1
-
2Hi nick - R can certainly do this for you, and SO can help. However - nobody can really provide any guidance without seeing what your data looks like. Here's a tutorial on how to ask a great question: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Please update your question and I bet you'll get some high quality answers. – Chase Aug 26 '11 at 01:09
-
Sounds like you need some sort of polynomial fit: [http://en.wikipedia.org/wiki/Curve_fitting](http://en.wikipedia.org/wiki/Curve_fitting) – O_O Aug 26 '11 at 00:44
1 Answers
1
To get a best fit exponential curve, you'll want to basically convert an the curve to a linear one, and find the "linear fit" and then convert it back. For example, here's data decreasing exponentially.
t <- c(0,2,4,7)
y <- c(25,11,4,3)
Then take the log of y
.
y2 <- log10(y)
Then model y2 as a function of time.
lm(y2~t)
You'll get the slope and intercept in usual linear equation form (y = mx + b
), but using log(y). To get the exponential equation of the line, put the slope and intercept into the following form:
y = A*r^t
Where A = 10^intercept
and r = 10^slope
.
For these data, your equation will be:
y = 20.77304*0.7333309^t
If you want to plot them together, define:
expLine <- function(t) 20.77304*0.7333309^t
Then plot(t,y)
and curve(expLine,0,7,n=101,add=TRUE)
.

Amyunimus
- 1,583
- 4
- 20
- 43
-
If you want to plot them together, define: `expLine <- function(t) 20.77304*0.7333309^t`. Then `plot(t,y)` and `curve(expLine,0,7,n=2000,add=TRUE)`. – Amyunimus Aug 26 '11 at 18:30
-
or `with(as.list(coef(lmfit)),curve(t*x+#(Intercept)#,0,7,add=TRUE))` (**note**, you'll have to replace # with a back-tick (single back-quote), comment syntax can't handle it). `n=2000` is overkill for this problem; the default value of `n=101` should be fine. By the way, @Amyunimus -- you're allowed and indeed encouraged to edit your question (this time I went ahead and did it for you ...) – Ben Bolker Aug 26 '11 at 19:03