1
$\begingroup$

I am trying to calculate chi-squared value for my fitted data using:

$$ \chi^2 = \sum_i^n{\frac{(y-f(x))^2}{f(x)}} $$

where $f(x)$ are theoretical values from fitted function and $y$ are observations.

However, if expected values are too small, I receive (logically) very large numbers. For example if I calculate chi-squared for values that are listed below, I'll receive (in C++): -inf as a result. But statistical software which I use as a reference outputs chi-squared as 0.39.

Please can you suggest what I am doing wrong?

Example data follows:

y:  | f(x):
 0  |  0.000233516 
 0  |  0.000748074  
 0  |  0.00226688 
 1  |  0.00649784 
 1  |  0.0176183 
 1  |  0.0451873 
 1  |  0.109628 
 0  |  0.251586 
 0  |  0.546141 
 0  |  1.12145 
 0  |  2.17825 
 1  |  4.00215 
 3  |  6.9556 
 6  | 11.4349 
17  | 17.7821 
22  | 26.1572 
42  | 36.3961 
41  | 47.9043 
61  | 59.6417 
79  | 70.2394 
83  | 78.2468 
82  | 82.4535 
74  | 82.1877 
81  | 77.4925 
58  | 69.1145 
73  | 58.3087 
39  | 46.5322 
34  | 35.1261 
14  | 25.082 
24  | 16.9414 
19  | 10.8241 
16  |  6.5417 
11  |  3.73977 
 4  |  2.02234 
 4  |  1.03448 
 4  |  0.500544 
 4  |  0.229097 
 2  |  0.0991863 
 2  |  0.04062  
 2  |  0.0157356 
 1  |  0.00576612 
 2  |  0.00199866 
 1  |  0.000655315 
 0  |  0.000203244 
 0  |  5.96265e-05 
 0  |  1.65469e-05 
 0  |  4.34361e-06 
 0  |  1.07855e-06 
 0  |  2.53329e-07 
 1  |  5.62841e-08  
$\endgroup$
1
  • $\begingroup$ Neither answer is a correct implementation of your formula, since it's obvious from just the last cell alone that the actual chi-square statistic must exceed $1.776 \times 10^{7}$! It's a little difficult to guess what's wrong with either calculation without seeing what you did in both of them. $\endgroup$
    – Glen_b
    Jan 29, 2014 at 13:21

1 Answer 1

4
$\begingroup$

The original setting of a chi-squared statistic is that you are comparing observed and expected counts over a set of categories. The statistic is the sum of (observed count $-$ expected count)$^2$ / expected count. In this case, the expected values (whether equivalent to counts or not) are all positive and so the statistic will be positive and finite. However, your result will be dominated by the last line, 1 5.62841e-08: I get the contribution from that to be 17767005.02 and the overall statistic to be 17771474.65, presumably with 50 $-$ 1 $=$ 49 df.

A small discrepancy is that your $y$ and $f(x)$ values don't have the same totals. That would be a requirement for chi-square testing.

An eyeball comparison of the two curves suggests that the fit is quite good, so the chi-square statistic is here at least misleading and at worst inappropriate, being necessarily but unfortunately over-sensitive to extremely small expected counts. Further advice depends on knowing exactly how these numbers arise and whether they do really fit the requirements for chi-square testing.

$\endgroup$
3
  • $\begingroup$ Thank you very much for response. I must confess I had a mistake in implementation, so the result is not -inf, but 17771474.65, as you mentioned, thank you. Is there any way how remove/normalize such "dominating" values? $\endgroup$
    – Michal
    Jan 29, 2014 at 13:21
  • $\begingroup$ There is no acceptable fudge or kludge here. Better advice depends on knowing whether you really are dealing with counts. As said, observed and expected must have the same total, so that's one discrepancy to fix. What is happening beyond the first and last cells? $\endgroup$
    – Nick Cox
    Jan 29, 2014 at 13:28
  • $\begingroup$ Aha, this should be my problem, because I did not split my data into bins/cells and I am trying to calculate chi-squared directly from function values. I am going to read more carefully about this topic, thank you for help. $\endgroup$
    – Michal
    Jan 29, 2014 at 13:33

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.