[R] find parameters for a gamma distribution

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


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

... you may use these as start values for 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 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