# How to get the expected counts when computing a chi-squared test?

I just have 1500 data points in excel. However, I want to do a chi squared test to prove if the data is normaly distributed.

My question is: How to calculate the expected range for such a large data set?

UPDATE

How to calculate the relative frequencies for such a large data set?

• Um--don't you just subtract the minimum of the 1500 values from the maximum? What information are you really after here?
– whuber
Mar 14 '13 at 16:05
• @whuber what do you mean by substracting the min from the max? I thought when doing a chi squared test I should calculate the expected range by the formula of the chi squared test the relative frequencies: $e_i=n*p_i$ and the absolute frequencies. Mar 15 '13 at 8:06
• By definition, the "range" of a dataset is the difference between its largest and smallest values. Your edit suggests you want "relative frequencies," which is different: thank you for the clarification.
– whuber
Mar 15 '13 at 15:30

Using a spreadsheet for a quick-and-dirty check of goodness of fit to a distribution is not a bad idea, especially if somebody has handed you a batch of data in a spreadsheet or you are doing other spreadsheet analyses with these data (or if you want to check up on other software to confirm its accuracy or your understanding of its calculations). Here is what a chi-squared test might look like in a spreadsheet:

The chi-square statistic in this example is $10.37$ with a p-value of $0.409$. The data are singled out with a separate (blue) color; important values are highlighted; and auxiliary values (whose columns I usually hide) are shown in light gray text. (A few calculations are done as cross-checks, such as the values in the "Total/max" line (row D14:H14).)

To accomplish the test,

1. Before inspecting the data, set up a list of cutpoints (column D) that will define the "bins" into which the data will fall. For instance, if you expect the data to lie between $0$ and $10$ and you have $1500$ values, you might choose to divide this range into bins of widths somewhere between $0.2$ and $1$. (The smaller width of $0.2$ would give an average of $30$ data per bin, but likely some extreme bins would have much smaller counts; the larger width of $1$ gives only $10$ bins, which might only coarsely distinguish the distribution of data.) You do not have to put equal gaps between the bins. Often, for instance, the most extreme bins are made wider to accommodate expected sparse data in the tails of the distribution.

2. Compute the data counts in each bin (column F). In Excel, use COUNTIF to count data below or equal to each cutpoint (column E) and subtract each pair of successive counts to obtain counts in each bin.

3. Estimate the parameters of the distribution you are fitting, using Maximum Likelihood (cells B3:C4). (There's some fudging going on here; for more about this, see https://stats.stackexchange.com/a/17148.) For a Normal distribution, the Maximum Likelihood estimates are the mean (AVERAGE) and the uncorrected ("population") standard deviation (STDEVP).

4. Use the cumulative distribution function to obtain expected values in each bin. In Excel the CDF is NORMINV. In any platform, the arguments to the CDF are the distribution parameters and the upper bin endpoint. This gives expected total counts for all values up to and including the endpoint (column G). Therefore, subtract the values of the CDF at successive endpoints to obtain expected values in bins (column H). Notice how the calculation of the CDF parallels the use of COUNTIF to find bin counts: the bin counts are the empirical distribution while the CDF gives the reference distribution to which the empirical distribution will be compared.

5. Apply the chi-squared formula: subtract expected values from bin counts (the residuals); square them; divide by the expectations; add everything up (cell C6). (It is convenient, actually, to modify this a bit in practice: divide the residuals by the square roots of the bin counts (column I): these are interesting in their own right, because very positive or very negative ones indicate precisely how the data deviate from the reference distribution. The chi-squared statistic is the sum of squares of the residuals.)

6. Compute the chi-squared p-value (using CHIDIST) (cell C7).

7. Inspect the counts and the residuals. If a sizable proportion of counts are nonzero but less than $5$, the p-value is untrustworthy. If the residuals show a clear pattern, such as progressing steadily from negative to positive back to negative, then you might have evidence of lack of fit, regardless of the chi-squared result. (Plotting the residuals is a good idea, but is not shown here.)

Here are the formulas used in the example:

In this case all residuals are small in size; there is no pattern to them; and the chi-squared p-value is large. We conclude these data look Normally distributed. (In fact, they were generated using draws from a Normal distribution with mean $5$ and standard deviation $2$ via the formula NORMSINV(RAND())*2 + 5.)

Notice that the formulas make extensive use of named ranges. The names correspond systematically to the labels shown in the spreadsheet; for instance, Data is the range A:A headed by "Data" (column A). I warmly recommend always using this technique, because it creates readable formulas and readable formulas are formulas that tend to be correct.

Be careful when reproducing these formulas: although most of the formulas in columns E:I have just been copied down, often the very first and very last formulas in each column are a little different.

1) No test will prove your data is normally distributed. In fact I bet that it isn't.

(Why would any distribution be exactly normal? Can you name anything that actually is?)

2) When considering the distributional form, usually, hypothesis tests answer the wrong question. What's a good reason to use a hypothesis test for checking normality?

I thought that the chi squared test or the Kolmogorov–Smirnov test provide a good indication about the distribution.

However, if not, what are some usecases they are good for?

I can think of a few cases where it makes some sense to formally test a distribution. One common use is in testing some random number generating algorithm for generating a uniform or a normal.

3) If you want to test normality, a chi-squared test is a really bad way to do it. Why not, say, a Shapiro-Francia test or say an Anderson-Darling adjusted for estimation? You'll have far more power.

4) What do you mean by 'expected range', specifically?

by expected range I mean in the formula of the chi squared test the relative frequencies: ei=n∗pi

To have expected counts you need to split it up into subranges, and multiply the total count by the probability in the subrange.

• Thx for your reply! 1) why do you think that it isnt`t? I am really curious about this;). 2) I thought that the chi squared test or the Kolmogorov–Smirnov test provide a good indication about the distribution. However, if not, what are some usecases they are good for? 4) by expected range I mean in the formula of the chi squared test the relative frequencies: $e_i=n*p_i$ Mar 15 '13 at 8:04
• see the updates to my answers. Mar 15 '13 at 8:27
• +1 to all of this. I'd argue that the best "test" (meaning check) of normality is graphical, through a normal probability plot. If a dataset is not normal, it helps you see why much more clearly than any significance test. Also, as is common, if a dataset is declared not normal, it may help you see that the non-normality is nevertheless mild, although such judgement comes partly from experience. In either case, learning that a distribution is not normal is only a first step: the challenge is then to say what kind(s) of distribution are a better approximation. May 23 '13 at 2:10