Search code examples
rgammamle

Maximum Likelihood Estimator for a Gamma density in R


I just simulated 100 randoms observations from a gamma density with alpha(shape parameter)=5 and lambda(rate parameter)=5 :

x=rgamma(100,shape=5,rate=5)

Now, I want to fin the maximum likelihood estimations of alpha and lambda with a function that would return both of parameters and that use these observations.

Any hints would be appreciate. Thank you.


Solution

  • You can use fitdistr(...) for this in the MASS package.

    set.seed(1)   # for reproducible example
    x <- rgamma(100,shape=5,rate=5)
    
    library(MASS)
    fitdistr(x, "gamma", start=list(shape=1, rate=1))$estimate
    #    shape     rate 
    # 6.603328 6.697338 
    

    Notice that with a small sample like this you don't get great estimates.

    x <- rgamma(10000,shape=5,rate=5)
    library(MASS)    # may be loaded by default
    fitdistr(x, "gamma", start=list(shape=1, rate=1))$estimate
    #    shape     rate 
    # 4.984220 4.971021 
    

    fitdistr(...) also returns the standard error of the estimates and the log-likelihood.