# 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. – David Jan 6 '12 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? – user8363 Jan 6 '12 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. – gung Nov 18 '12 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.text(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? – Elvis Jan 6 '12 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 – user8363 Jan 6 '12 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? – Elvis Jan 6 '12 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. – Xi'an Jan 6 '12 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. – Xi'an Jan 6 '12 at 15:03