[R] Curve Fitting Question - Newbie

Douglas Bates bates at stat.wisc.edu
Wed Jan 29 17:23:48 CET 2003


"Simon Blomberg" <Simon.Blomberg at anu.edu.au> writes:

> One way would be to use the nls package:
> 
> library(nls)
>   # your y values
> foo.dat <- (0.83 * exp(-0.017 * 1:1000) + 0.5) + rnorm(1000,0,0.05) 
>   # create some x values in a data frame
> foo.dat <- data.frame(cbind(foo.dat, 0:999)) 
> names(foo.dat) <- c("y", "x") # give them names
>   # assume you have reasonable guesses for the starting parameter values
> model <- nls( y ~ N * exp(-r * x) + c,
>               start = list( N = 1, r = .01, c = 0), 
>               data = foo.dat) 
> 
> resid(model) # display residuals

Thanks for your answer Simon.  I would have suggested a similar
approach with two minor modifications:
  - use the logarithm of the rate constant r instead of r itself
  - take advantage of N and c being conditionally linear by using the
    'plinear' algorithm in nls.

The call to nls would be

 model <- nls(y ~ cbind(exp(-exp(lr) * x), 1), start = c(lr = log(.01)),
              data = foo.dat, alg = 'plinear')




More information about the R-help mailing list