0

I'm a novice with R. I want to do a nonlinear regression fitting on the following 1st order decaying time-series formula:

y = a*exp^(-kt)

Can anyone show me the scripts and process to do this nonlinear fitting? Thanks!

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • 4
    "please give me code" is not considered an appropriate question for SO. See how far you can get with the examples in `?nls`, or searching SO for `[r] nls`, or by googling "nls r examples", and come back and edit your question when you get stuck .. – Ben Bolker Dec 12 '13 at 02:13
  • 1
    Perhaps look for "fitting distributions"? You have violated the Cardinal Rule of an R Question: Always post data. So no data in; no advice out. – IRTFM Dec 12 '13 at 02:29
  • This blog post may also be of interest: http://www.walkingrandomly.com/?p=5254 – bdemarest Dec 12 '13 at 04:13

1 Answers1

4

The nls() function performs non-linear least squares, which I think is what you're asking about. Here's an example:

> data = data.frame(Year=0:7, Count=c(3, 5, 9, 30, 62, 154, 245, 321))
> out = nls(Count~a*exp(b*Year), data=data, start=list(a=1, b=1))

# Look at output
> summary(out)

Formula: Count ~ a * exp(b * Year)

Parameters:
  Estimate Std. Error t value Pr(>|t|)    
a 11.52774    4.30644   2.677 0.036689 *  
b  0.48412    0.05778   8.379 0.000157 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 24.54 on 6 degrees of freedom

Number of iterations to convergence: 11 
Achieved convergence tolerance: 4.533e-06

Another option might be to do straight linear regression on the log-transformed data -- I'm not sure if that violates your modeling assumptions.

Source for this: https://stat.ethz.ch/pipermail/r-help/2007-July/137063.html

josliber
  • 43,891
  • 12
  • 98
  • 133