I am following these two approaches (which are the same)this and this, to estimate the two parameters of Gamma dist based on mean and var. I am not sure why I cannot get the same mean and var from rgamma based on the estimated parameters in R:


 shapeg = (mean1*2)/var1
 scaleg = var1/mean1

 nn=(rgamma(1000, shape = shapeg, scale = scaleg))

 > mean(nn);
 [1] 1.975715
 > var(nn);
 [1] 3.6593

Based on https://stat.ethz.ch/R-manual/R-devel/library/stats/html/GammaDist.html

The Gamma distribution with parameters shape = a and scale = s

The mean and variance are E(X) = as and Var(X) = as^2.

Based on this: https://stats.stackexchange.com/questions/280459/estimating-gamma-distribution-parameters-using-sample-mean-and-std

$\mbox{Var}[X] = \alpha \theta^2$

therefore, $\theta$ must be equal to scale in R. SO I am replacing the parameters correctly.


I think the mistake is in your code. It looks like you tried to square with *2 instead of ^2. Maybe you were thinking of Python, which would use **2.

  • 1
    $\begingroup$ Yes, confused, thx a lot, worked, need 9 min to accept this as answer $\endgroup$ – mohsen hs Feb 8 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.