Search code examples
pythonrcdfgamma-distribution

Fit points with gamma CDF in python or R


I have series of plots looking like this:

python code:

a = np.array([4,4,4,4,5,5,5,6,6,6,6,6,6,6,7,7,7,8,8,8,9])
b = np.array([i/len(a) for i in range(1, len(a)+1)])
pl.plot(a,b, 'ro')

r code:

a <- c(4,4,4,4,5,5,5,6,6,6,6,6,6,6,7,7,7,8,8,8,9)
b <- seq(0,1,length = length(a))
plot(a, b, col = "red")

imge

For some purpose I need to fit this points with best cumulative distribution function (CDF) of gamma distribution. Is there any way how to do this numerically in python or R? I am using winpython so i can import R code pretty straightfoward.

PS: I found this post but I dont understant it.


Solution

  • library(MASS)
    gammafit <- fitdistr(a, "gamma")   
    #    shape        rate   
    #    17.552961    2.902459 
    #    ( 5.366214) ( 0.900112)
    

    So apparently, the gamma-parameters 17.55 (for the shape) and 2.90 (for the rate) fit your data best.

    plot(a, b, col = "red")
    lines(a, pgamma(a, gammafit$estimate[1], gammafit$estimate[2]))
    

    like this