[R] How to regress data into coefficients for a gamma function

Peter Ehlers ehlers at ucalgary.ca
Tue Mar 29 20:04:55 CEST 2011


On 2011-03-29 06:57, Walter Anderson wrote:
>    Hello,
>
> I need to regress data like the example below.  The data points
> represent friction factors derived from observed trip length data.  The
> function used to describe this data is a gamma function of the form,
> f(t) = a * t^b * e^(c*t)  and I need to regress the data to obtain the
> a,b, and c coefficients.  The gamma function can also be expressed in
> the log-linear form, ln[f(t)] = ln[a] + b * ln[t] + c*t, which may be
> easier to perform a regression on.  I have performed a search for
> information on the subject, and have found a few possibly related sites,
> I can not figure out how to perform the regression in R.  Any
> help/guidance would be appreciated.
>
> Walter
>
>              t                f      ln(f)
>     1      1    952893    13
>     2      2    951077    13
>     3      3    945991    13
>     4      4    942358    13
>     5      5    939452    13
[... more data snipped ...]

This looks like easy grist for the nls() mill. I would usually
use the log form to get reasonable starting values for the
nls interations:

## assume that your data are in a data.frame DF;
  fm1 <- lm( log(f) ~ log(t) + t, data = DF )
  coef(fm1)
  # (Intercept)      log(t)           t
  # 13.81370501  0.18469223 -0.05955323

  startvec <- c( aa=exp(14), bb=0.2, cc=-.06 )
  fm2 <- nls( f ~ aa * t^bb * exp(cc*t), data=DF, start=startvec )
  #summary(fm2)
  coef(fm2)
  #           aa            bb            cc
  # 9.093841e+05  2.294895e-01 -5.951806e-02

## plot the data
  plot( f ~ t, data=DF )
  lines( fitted(fm2) ~ t, data=DF )

I wouldn't trust the model for small t.

Peter Ehlers



More information about the R-help mailing list