[R] R exponential regression
Peter Ehlers
ehlers at ucalgary.ca
Mon Jan 11 03:57:42 CET 2010
chrisli1223 wrote:
> Hi all,
>
> I have a dataset which consists of 2 columns. I'd like to plot them on a x-y
> scatter plot and fit an exponential trendline. I'd like R to determine the
> equation for the trendline and display it on the graph.
Here's one way:
f <- function(x,a,b) {a * exp(b * x)}
# generate some data
x <- 1:10
set.seed(44)
y <- 2*exp(x/4) + rnorm(10)*2
dat <- data.frame(x, y)
# plot the data
plot(y ~ x)
# fit a nonlinear model
fm <- nls(y ~ f(x,a,b), data = dat, start = c(a=1, b=1))
# get estimates of a, b
co <- coef(fm)
# plot the curve
curve(f(x, a=co[1], b=co[2]), add = TRUE)
-Peter Ehlers
>
> Since I am new to R (and statistics), any advice on how to achieve this will
> be greatly appreciated.
>
> Many thanks,
> Chris
--
Peter Ehlers
University of Calgary
403.202.3921
More information about the R-help
mailing list