[R] find parameters for a gamma distribution

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Thu Jan 6 00:01:03 CET 2005


Andrew Collier wrote:
> hello,
> 
> i have just started exploring R as an alternative to matlab for data analysis. so
> +far everything is _very_ promising. i have a question though regarding parameter
> +estimation. i have some data which, from a histogram plot, appears to arise from
> +a gamma distribution. i gather that you can fit the data to the distribution
> +using glm(). i am just not quite sure how this is done in practice... so here is
> +a simple example with artificial data:
> 
> d <- rgamma(100000, 20, scale = 2)
> h <- hist(d, breaks = c(seq(10, 80, 2), 100))
> 

[...]

The help page of ?dgamma says:

"The mean and variance are E(X) = a*s and Var(X) = a*s^2."

So, to estimate the parameters in your example, try the following:

> d <- rgamma(100000, 20, scale = 2)
> var(d)/mean(d)
[1] 1.992091
> mean(d)^2/var(d)
[1] 20.09559

... and you may use these as start values, see ?fitdistr, e.g

> fitdistr(d, dgamma, list(shape = 20.1, scale = 1.99))

or simply

> fitdistr(d, "gamma")

Hint: scale=1/rate

You mentioned "glm", but this is something completely different. It is
used to fit generalized linear models, see Venables and Ripley's book or 
the extra chapter no. 31 of  Crawley's book on:

http://www.bio.ic.ac.uk/research/mjcraw/statcomp/chapters.htm

Thomas P.




More information about the R-help mailing list