One way would be to use the nls package:
library(nls)
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
Cheers,
Simon.
Simon Blomberg
Consumer Research Unit
Centre for Mental Health Research, Australian National University
[EMAIL PROTECTED] +61 (2) 6125 3379
-----Original Message-----
From: C Grant [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, 29 January 2003 10:39 AM
To: [EMAIL PROTECTED]
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) +
rnorm(1000,0,0.05)
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
______________________________________________
[EMAIL PROTECTED] mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help
______________________________________________
[EMAIL PROTECTED] mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help