[R] random number generation

Francisco J. Zagmutt gerifalte28 at hotmail.com
Wed Jul 18 17:37:57 CEST 2007


Hello Hadi,

See ?rgamma

The Gamma distribution usually takes two parameters, shape and scale, 
not the mean and st. deviation.  If you have data, you can estimate 
those parameters using MLE methods, which are nicely provided in MASS:

library(MASS)
fitdistr(yourdata,"Gamma")

Once you have your parameters you can generate random values using 
rgamma i.e. rgamma(1000,10,1) will generate 1000 random samples from a 
Gamma distribution with shape = 10 and scale = 1.

If you only have the mean and standard deviation you can approximate the 
shape and scale parameters using:

scale=variance/mean
shape=mean^2/var

Remember the variance is equal to sd^2

I hope this helps,


Francisco J. Zagmutt

PS: Please read the posting guide (see the link at the bottom of this 
email). It really helps people trying to help you :-)



Hadi Darzian Azizi wrote:
> Hi there,
> I am relatively new user of R. I need to generate random number following
> Gamma distribution with mean 14 und st.dev 3. I read the help-text but I can
> not understand it well.
> 
> Regards,
> Azizi
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list