[A, SfS] Chapter 7: Chi-square tests and ANOVA: 7.1: Chi-square Goodness of Fit Test
Chi-square Goodness of Fit Test
Goodness-of-Fit TestIn this section, we will discuss how, given a categorical variable, we can check whether the proportions of the population falling into each of the categories fit a hypothesized frequency distribution.
Recall that a categorical variable has non-numerical values, i.e., categories. If the variable is ordinal then there is some logical order to the categories; otherwise it is nominal.
We typically display a summary of data collected for such a variable using a frequency table, which shows the number of subjects in a sample which fall into each category. A researcher could be interested in whether or not these frequencies (also called counts) match some hypothesized distribution. This could be investigated using a goodness-of-fit test.
HypothesesSuppose a categorical variable has distinct categories. Let denote the proportion of the population that belongs in category , for .
In this context, a goodness-of-fit test can be used to determine whether the observed frequencies are consistent with some hypothesized distribution.
The null hypothesis of a goodness-of-fit test is defined as:
where is some specified value for the proportion of the population belonging to category . Note that we must have .
The research hypothesis of a goodness-of-fit test is defined as:
for at least one , meaning that at least one proportion does not equal its hypothesized value (i.e., the frequency distribution of the sample does not fit the null distribution).
Note that the test of against which we discussed in the previous chapter is a simplified version of this test, with .
Test Statistic and Null DistributionIn a random sample of subjects we would expect
Let denote the actual number of subjects observed in category in the sample, for .
The test statistic of a goodness-of-fit test is:
Provided that for , then it has been proven that has an approximate distribution (the chi-square distribution with degrees of freedom).
If for any category, then that category can be combined with one or more other categories until the condition is met for every category (this can sometimes be done by creating a category called 'other').
Calculating the P-value in RGiven some observed value for the test statistic, the P-value is based on the distribution.
This probability is computed in using:
> pchisq(x^2,k-1,low=F)
If a significance level has been chosen, then is rejected if the computed P-value is , and is otherwise not rejected.Suppose we want to know if students are distributed evenly among three majors, i.e., we test against for at least one .
In a random sample of size we expect
students per major. Note that for each .
If in the sample we observe , and , then
with degrees of freedom.
Then the P-value is computed in in using
> pchisq(5.6,2,low=F)
to be .
If we use as our significance level, then we would not reject . But we can see that the evidence against is moderately strong.
Note that we typically do not compute any confidence intervals for the parameters in this setting, although it could be done if wished.
Using RThe goodness-of-fit test can be carried out in using the function.
You must input two vectors, one containing the observed frequencies for the categories, and another containing the null frequency distribution .
The order of the categories must be the same for both vectors. You can make the vectors separately, or within the function.
For example, suppose we have categories, with null frequency distribution
> P = c(0.15,0.20,0.25,0.30,0.10)
and observed frequencies
> X = c(67,78,81,92,60)
Then we can carry out the goodness-of-fit test using
> chisq.test(X, p=P)
This will produce the values of the test statistic, its degrees of freedom, and the P-value.
Note: You must include the "" in front of the vector of the null frequency distribution. Otherwise, the function will misinterpret your input.
You could also have combined all steps into one:
> chisq.test(c(67,78,81,92,60), p=c(0.15,0.20,0.25,0.30,0.10))
You will get an error message if the sum of the elements of the null frequency vector is not equal to one.
If you need to know the expected frequencies under , add to the end of the function, e.g.,
> chisq.test(X, p=P)$expected
> chisq.test(c(67,78,81,92,60), p=c(0.15,0.20,0.25,0.30,0.10))$expected
Or visit omptest.org if jou are taking an OMPT exam.