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
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
*2instead of^2. Maybe you were thinking of Python, which would use**2.