determine the formula of a dataset
I have a small dataframe that has a exponentially decreasing trend. I would like to extract a formula ba开发者_JAVA百科sed on the data in my dataframe? Is this possible, and if so, how can I go about extracting the formula?
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)
.
精彩评论