Skip to content

R exponential regression

4 messages · chrisli1223, Murray Cooper, Peter Ehlers

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

Since I am new to R (and statistics), any advice on how to achieve this will
be greatly appreciated.

Many thanks,
Chris
2 days later
#
Chris,

I haven't seen anyone post a reply yet so thought I'd
throw in my thoughts. I'm no R expert!

When you talk about an exponential trend line are you
refering to:

1)  y=ax^b
or
2) y=ae^(bx)

If 1) then take base10 logs of y and x and then fit them
with simple linear regression. Then calculate the antilog
of the residulas and plot these as your trendline.

If 2) then take natural logs of y and x and follow the rest
of the procedure described in 1).

Hope this helps.

Murray M Cooper, Ph.D.
Richland Statistics
9800 N 24th St
Richland, MI, USA 49083
Mail: richstat at earthlink.net

----- Original Message ----- 
From: "chrisli1223" <chrisli at austwaterenv.com.au>
To: <r-help at r-project.org>
Sent: Thursday, January 07, 2010 10:33 PM
Subject: [R] R exponential regression
#
Thank you very much Murray! Greatly appreciated. :]

Chris
Murray Cooper wrote:

  
    
#
chrisli1223 wrote:
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