# estimate Gamma parameters based on mean and variance

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:

 mean1=20
var1=30

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

nn=(rgamma(1000, shape = shapeg, scale = scaleg))
mean(nn);
var(nn);

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


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

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

$$\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.