# How to test whether a sample of data fits the family of Gamma distribution?

I have a sample of data which was generated from a continuous random variable X. And from the histogram I draw using R, I guess that maybe the distribution of X obeys a certain Gamma distribution. But I do not know the exact parameters of this Gamma distribution.

My question is how to test whether the distribution of X belongs to a family of Gamma distribution? There exists some goodness of fit tests such as Kolmogorov-Smirnov test, Anderson-Darling test, and so on, but one of the restriction when using these tests is that the parameters of the theoretical distribution should be known in advance. Would anyone please tell me how to solve this problem?

• Perhaps I am missing something, but if you already know of a test for testing the fit of the distribution and all you need to know are the values of the theoretical distribution, then you could simply use the maximum likelihood estimators of the parameters of the gamma distribution on your data to get estimates of the parameters. You could then use those estimates to define the theoretical distribution in your test. Jan 6, 2012 at 0:22
• David, thank you for your answer. The answer is also what i have been thinking about, but i am not sure whether there is some theories which can support this idea, could you answer it for me? Jan 6, 2012 at 10:48
• If you use R, you may be interested in taking a look at the fitdistrplus package, which has facilities for doing this sort of thing. Nov 18, 2012 at 14:28

I think the question asks for a precise statistical test, not for an histogram comparison. When using the Kolmogorov-Smirnov test with estimated parameters, the distribution of the test statistics under the null depends on the tested distribution, as opposed to the case with no estimated parameter. For instance, using (in R)

x <- rnorm(100)
ks.test(x, "pnorm", mean=mean(x), sd=sd(x))


        One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0701, p-value = 0.7096
alternative hypothesis: two-sided


while we get

> ks.test(x, "pnorm")

One-sample Kolmogorov-Smirnov test

data:  x
D = 0.1294, p-value = 0.07022
alternative hypothesis: two-sided


for the same sample x. The significance level or the p-value thus have to be determined by Monte Carlo simulation under the null, producing the distribution of the Kolmogorov-Smirnov statistics from samples simulated under the estimated distribution (with a slight approximation in the result given that the observed sample comes from another distribution, even under the null).

• (+1) I don’t get well why it is correct to simulate samples under the estimated distribution. I would have think that we needed a prior for the parameters, and sample from all the possible distributions... can you explain a little bit more? Jan 6, 2012 at 9:38
• Xi'an, your answer is exactly what i worried about. You mean that "When using the Kolmogorov-Smirnov test with estimated parameters, the distribution of the test statistics under the null depends on the tested distribution". However, we don't know the distribution of X, more precisely, we don't know the parameter of the distribution of X under the null hypothesis, hence the distribution of test statistic,therefore,we use monte carlo. Would you have some other ways of solving it by not using monte carlo to get the P value? Thank you Jan 6, 2012 at 11:05
• To take into account the fact that "the observed sample comes from another distribution even under the null", wouldn’t it be appropriate to bootstrap the sample, re-estimating the parameters at each replicate? Jan 6, 2012 at 13:19
• @Elvis (1): this is classical statistics, not a Bayesian resolution of the goodness of fit problem. For distributions with location-scale parameters, the choice of the parameters used to simulate the simulated samples does not matter. Jan 6, 2012 at 14:58
• @Elvis (2): Again something I just discussed with my students! Bootstrap would help in assessing the behaviour of the Kolmogorov-Smirnov distance under the true distribution of the data, not under the null! The Fisher-Neyman-Pearson principle is that what matters is the behaviour of the Kolmogorov-Smirnov distance under the null, so that it is rejected if the observed distance is too extreme wrt this distribution under the null. Jan 6, 2012 at 15:03

Compute MLEs of the parameters assuming a gamma distribution for your data and compare the theoretical density with the histogram of your data. If the two are very different the gamma dstribution is a poor approximation of your data. For a formal test you could compute, for example, the Kolmogorov-Smirnoff test statistic comparing the best fitting gamma distribution with the empirical distribution and test for significance.

• +1, this is a solid answer. However, I would suggest examining a qq-plot against the theoretical gamma rather than a histogram--it will be easier to assess for deviations. Jan 6, 2012 at 3:30
• The problem is that KS test assumes the theoretical distribution to be given beforehand, not estimated from the data. Xi'an (partially) answered to that point... Jan 6, 2012 at 9:35
• you mean that we firstly use this sample of data to obtain an MLS estimater and use the value of MLS estimater in Gamma distribution, and then compare the data with Gamma distribution(with estimated parameter) by using KS test? Jan 6, 2012 at 10:45
• Elvis, would you please tell me how to solve the problem that when the parameter of the theoretical distribution is unknow and need to be estimated. In this cas, how can one use KS test to get a relatively accurate judgement of the hypothesis, thank you! Jan 6, 2012 at 10:57
• @Elvis: I do not think the exact derivation is possible in the case of the gamma distribution. The cdf itself is not available in closed form. Further, the fact that the shape parameter is neither scale nor location means that there is a different distribution for each value of the shape parameter... Jan 6, 2012 at 20:26