2
$\begingroup$

I am trying to model a dataset of mine with a lognormal distribution using Matlab. I estimated the parameters via 'lognfit' and my generated datapoints with the fitted distribution look quite good compared to the observed data.

To further verify, I wanted to use chi2gof to test the goodness-of-fit. Everything seems to work fine and the null hypothesis is not rejected but the p-value is NaN.

I am using the approach applied to the the Weibull distribution under http://www.mathworks.de/de/help/stats/chi2gof.html

So you can reproduce my result, this is an example that does not work.

>> test = lognrnd(-3.6299, 1.30985, 100, 1);
>> b = fitdist(test, 'Lognormal')

b = 

LognormalDistribution

Lognormal distribution
mu = -3.62425   [-3.86888, -3.37962]
sigma =   1.2329   [1.08249, 1.43223]

>> [h, p, st] = chi2gof(test,'CDF', b)

h = 0

p = NaN

st = 

chi2stat: 0.0183
      df: 0
   edges: [0.0015 0.2151 2.1372]
       O: [94 6]
       E: [93.6703 6.3297]

Also transforming the data to normal distributed datapoints and checking for a normal distribution results in a p-value of NaN.

I am thankful for any help!

Best regards

EDIT:

I was just able to make the transforming-to-normal approach to work:

Would still be nice to know if this approach is valid and why the original approach does not work.

>> test = lognrnd(-3.6299, 1.30985, 100, 1);
>> test = log(test);
>> b = fitdist(test, 'normal')

b = 

NormalDistribution

Normal distribution
mu = -3.68114   [-3.92861, -3.43368]
sigma =  1.24717   [1.09502, 1.44881]

>> [h, p, st] = chi2gof(test, 'CDF', b)

h = 0


p = 0.5330


st = 

chi2stat: 4.1149
df: 5
edges: [-6.1930 -5.5713 -4.9497 -4.3281 -3.7064 -3.0848 -2.4632 -1.8415 0.0234]
O: [8 7 18 16 17 20 8 6]
E: [6.4814 8.9730 14.7440 18.9931 19.1820 15.1883 9.4282 7.0101]
$\endgroup$

1 Answer 1

1
$\begingroup$

It looks like in the first case you supplied a variable "changes" to chi2gof, with only 15 observations. You wound up with only 2 bins. MATLAB approximates the number of degrees of freedom as the number of bins minus the number of estimated parameters (2) for a total of 0.

In the second case you supplied the same data as used in the fit, with 100 observations.

If you intended to use a different dataset for the fitting and testing, you could tell chi2gof that you did not estimate the parameters, since you didn't estimate them from the data being tested.

[ADDED]

With the updated question, the test data looks very skewed, and chi2gof struggles to find good equal-sized bins to use. So it still winds up with just two bins. You can specify bin edges yourself, for example:

[h, p, st] = chi2gof(test,'CDF', b, 'edges', [0 .008 .02 .04 .1 100])

Working on the log scale is a perfectly reasonable idea, though.

$\endgroup$
1
  • $\begingroup$ Thank you for pointing that out! That was a mistake from my side, using the wrong array as an input. I edited my post above now and the error still prevails even after using the right input. $\endgroup$
    – user113685
    Mar 26, 2014 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.