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
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.