The chi-square goodness of fit test is a non-parametric test to determine if sample data are consistent with a hypothesized distribution. The null hypothesis is that there is no significant difference between the observed and expected (or hypothesized) distributions. You may have used this test to examine if genotype frequencies are in Hardy-Weinberg equilibrium. In that example, the observed frequencies are tested against those expected under Hardy-Weinberg equilibrium. If there are significant differences between the observed and expected frequencies, it indicates that the population is out of Hardy-Weinberg equilibrium for that gene.
A chi-square goodness of fit test is appropriate when the expected frequencies of each cell in the contingency table is at least 5.
Chi-square goodness of fit tests analyze whether the observed distribution is different from the expected one. The chi-square test statistic is calculated using the formula below. This value is then compared to the chi-square critical value in a table.
Let’s imagine you were examining the number of colored tulips in a population and found that 81 were red, 50 were yellow and 27 were white. You could use a Chi-square goodness of fit test to examine whether the colors were equally common or whether what you measured was different from expected given the population.
In order to make your RStudio script file organized, you will want to include some information at the top of the file. You can use the hashtag (#) to include things in your RStudio file that R can’t read.
For each test, you should include the following lines at the top of each RStudio file:
#question:
#response variable:
#explanatory variable:
#test name:
Below is what it would look like for the tulip example:
#question: are the tulip colors equally common? Are they different from expected proportions?
#response variable: number of tulips of each color
#explanatory variable: tulip color (categorical)
#test name: chi-square goodness of fit test
You can directly enter your data into RStudio to run a chi-square goodness of fit test. To answer the first question, you need to figure out the expected proportions of the flower colors. If the colors are equally common, you would expect the proportions to be equal (1/3 each).
To answer the second question, you need to know the expected proportions of each tulip color. Based on previous research in the region where you collected the data, the ratio of red, yellow, and white tulips is 3:2:1 (3+2+1 = 6). This means that the expected proportion is:
3/6 (= 1/2) for red 2/6 (= 1/3) for yellow 1/6 (= 1/6) for white
To run the chi-square goodness of fit test to answer the first question on the tulip data, use the code below.
#code to enter the counts of each tulip color
tulip <- c(81, 50, 27)
#code to run the chi-square test showing equal proportions of each tulip color
res <- chisq.test(tulip, p = c(1/3, 1/3, 1/3))
res
##
## Chi-squared test for given probabilities
##
## data: tulip
## X-squared = 27.886, df = 2, p-value = 8.803e-07
The output shows that the chi-square test statistic (X-squared) was 27.89, the degrees of freedom (df) was 2, and the p-value was < 0.05. The df are calculated as (# groups - 1) so in this case it was (3 - 1), which is 2. Because the p-value was < 0.05, the colors of tulips are not equally common. You can get the expected values using the code below.
#code to get expected values
res$expected
## [1] 52.66667 52.66667 52.66667
To run the chi-square goodness of fit test to answer the second question on the tulip data, use the code below. The expected proportions were calculated above.
#code to enter tulip data
tulip <- c(81, 50, 27)
#code to run the chi-square goodness of fit test with the expected proportions of each tulip color
res <- chisq.test(tulip, p = c(1/2, 1/3, 1/6))
res
##
## Chi-squared test for given probabilities
##
## data: tulip
## X-squared = 0.20253, df = 2, p-value = 0.9037
The output shows that the chi-square test statistic (X-squared) was 0.20, the degrees of freedom (df) was 2, and the p-value was 0.904. The df are calculated as (# groups - 1) so in this case it was (3 - 1), which is 2. Because the p-value was > 0.05, the observed proportions of the colors of tulips are not significantly different from the expected proportions.You can get the expected values using the code below.
#code to get expected values
res$expected
## [1] 79.00000 52.66667 26.33333
The results section of your paper should begin with a narrative of your results statements. These statements should be quantitative in nature and include
the biological significance.
Statistical Significance: Your first sentence should list the results of the statistical test (in this case whether the proportions of tulip colors were equal and significantly different from expected proportions). You include statistical data in parentheses at the end of the sentence only. You should never write “The p-value was…” or “The chi-square was, which means…”. You don’t write about your statistics, you just write the biological results and include your statistics in parentheses. This satisfies whether your results were statistically significant.
Biological Significance: For the quantitative statements, you can include quantitative comparisons of the counts (e.g., one is X% higher than the other).
The first results statement that includes the statistical results in parentheses should also reference the figure you are referring to. Remember to always -refer to figures in the order in which they appear, and -include your results narrative before the figure.
A bar chart with the group counts or percentages is the most appropriate way to present these data. You can use Excel to create a bar chart. Use the link below to watch a video on how to make a bar chart in Excel: https://www.screencast.com/t/Dv6xg1yXJT
A caption must be included below each figure.
The caption for an Chi-square Test Goodness of Fit test must include:
1. A short descriptive title following the figure number
2. A description of what you plotted
3. Your sample size (e.g., # transects/site)
4. The p-value for overall test.
The proportions of each color of tulip were significantly different from one another (chi-square goodness of fit test, X2 = 27.89, df = 2, p < 0.05, Figure 1). There were 1.6 times more red tulips than yellow and 3 times more red tulips than white. These proportions, however, were not significantly different from those expected in the population (chi-square goodness of fit test, X2 = 0.20, df = 2, p = 0.904, Figure 1).
Figure 1. Number of different colored tulips in a population (n = 158). The tulips were not equally common (p < 0.05) but were not significantly different from those expected in the population (p = 0.904).
Here is all of the code you need to quickly run the chi-square goodness of fit test.
#code to enter the counts of each tulip color
tulip <- c(81, 50, 27)
#code to run the chi-square test showing equal proportions of each tulip color
res <- chisq.test(tulip, p = c(1/3, 1/3, 1/3))
res
#code to get expected values
res$expected
#code to enter tulip data
tulip <- c(81, 50, 27)
#code to run the chi-square goodness of fit test with the expected proportions of each tulip color
res <- chisq.test(tulip, p = c(1/2, 1/3, 1/6))
res
#code to get expected values
res$expected
*Written by Carrie L. Woods, August 2019. Modified from http://stats.pugetsound.edu/ecology/
References http://www.sthda.com/english/wiki/chi-square-goodness-of-fit-test-in-r (accessed 9/3/2019)