# Normality Versus Goodness-of-Fit Tests

In many cases, in statistical modeling, we would like to test whether the underlying distribution from an i.i.d. sample lies in a given (parametric) family, e.g. the Gaussian family

whereConsider a sample

> library(nortest) > X=rnorm(n)

Then a natural idea is to use goodness of fit tests (natural is not necessarily correct, we'll get back on that later on), i.e.

for some and . But since those two parameters are unknown, it is not uncommon to see people substituting estimators to those two unknown parameters, i.e.

Using Kolmogorov-Smirnov test, we get

> pn=function(x){pnorm(x,mean(X),sd(X))}; > P.KS.Norm.estimated.param= + ks.test(X,pn)$p.value

But since we choose parameters based on the sample we use to run a goodness of fit test, we should expect to have troubles, somewhere. So another natural idea is to split the sample: the first half will be used to estimate the parameters, and then, we use the second half to run a goodness of fit test (e.g. using Kolmogorov-Smirnov test)

> pn=function(x){pnorm(x,mean(X[1:(n/2)]), + sd(X[1:(n/2)]))} > P.KS.Norm.out.of.sample= + ks.test(X[(n/2+1):n],pn)$p.value>.05)

As a benchmark, we can use Lilliefors test, where the distribution of Kolmogorov-Smirnov statistics is corrected to take into account the fact that we use estimators of parameters

> P.Lilliefors.Norm= + lillie.test(X)$p.value

Here, let us consider i.i.d. samples of size 200 (100,000 samples were generated here). The distribution of the -value of the test is shown below,

In red, the Lilliefors test, where we see that the correction works well: the -value is uniformly distributed on the unit inteval. There is 95% chance to accept the normality assumption if we accept it when the -value exceeds 5%. On the other hand,

- with Kolmogorv-Smirnov test, on the overall sample, we always accept the normal assumption (almost), with a lot of extremely large -values
- with Kolmogorv-Smirnov test, with out of sample estimation, we actually observe the opposite: in a lot of simulation, the -value is lower then 5% (with the sample was from a sample).

The cumulative distribution function of the -value is

I.e., the proportion of samples with -value exceeding 5% is 95% for Lilliefors test (as expected), while it is 85% for the out-of-sample estimator, and 99.99% for Kolmogorov-Smirnov with estimated parameters,

> mean(P.KS.Norm.out.of.sample>.05) [1] 0.85563 > mean(P.KS.Norm.estimated.param>.05) [1] 0.99984 > mean(P.Lilliefors.Norm>.05) [1] 0.9489

So using Kolmogorov-Smirnov with estimated parameters is not good, since we might accept too often. On the other hand, if we use this technique with two samples (one to estimate parameter, one to run goodness of fit tests), it looks much better ! even if we reject too often. For one test, the rate of first type error is rather large, but for the other, it is the rate of second type error...

*(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)*