[R] Curve Fitting Question - Newbie

Simon Blomberg Simon.Blomberg at anu.edu.au
Wed Jan 29 01:57:06 CET 2003

One way would be to use the nls package:


foo.dat <- (0.83 * exp(-0.017 * 1:1000) + 0.5) + rnorm(1000,0,0.05) # your y values
foo.dat <- data.frame(cbind(foo.dat, 0:999)) # create some x values in a data frame
names(foo.dat) <- c("y", "x") # give them names

model <- nls( y ~ N * exp(-r * x) + c, start = list( N = 1, r = .01, c = 0), data = foo.dat) # assume you have reasonable guesses for the starting parameter values

resid(model) # display residuals



Simon Blomberg
Consumer Research Unit
Centre for Mental Health Research, Australian National University
Simon.Blomberg at anu.edu.au  +61 (2) 6125 3379

-----Original Message-----
From: C Grant [mailto:cgrant02589 at yahoo.com]
Sent: Wednesday, 29 January 2003 10:39 AM
To: r-help at stat.math.ethz.ch
Subject: [R] Curve Fitting Question - Newbie

Hello, I have what should be an easy question. I'm a
new r user and making the transition from menus to the
command line so as to do batch processing of tons of
data. One of my data streams needs to be detrended.
It's a vector of numbers that follows a negative
exponential decay. I need to fit a curve to it and use
the residuals as an object. The data looks something
like this:

foo.dat <- (0.83 * exp(-0.017 * 1:1000) + 0.5) +
plot(foo.dat, type = "l")

That is an example the parameters for foo.dat change
slightly. So, I need to fit a neg exponential curve to
that type of data without specifying the parameters
beforehand and save the residuals as an object. It
will have to accomadate being in a loop.

How can I do this?

Regards, CG

R-help at stat.math.ethz.ch mailing list

More information about the R-help mailing list