Chi-Square Test in Statistics Using R
Experiment
Chi-Square Test in Statistics Using R
1. Experiment Title
Hypothesis Testing Using the Chi-Square Goodness-of-Fit Test in R
2. Aim
To study and perform a Chi-Square goodness-of-fit test using R to determine whether the observed frequencies differ significantly from the expected frequencies.
3. Objectives
After completing this experiment, students should be able to:
- Understand the purpose of the Chi-Square test.
- Formulate null and alternative hypotheses.
- Calculate expected frequencies.
- Calculate the Chi-Square statistic manually.
- Perform a Chi-Square test using R.
- Interpret the Chi-Square statistic and p-value.
- Make a statistical decision based on the test result.
4. Theory
4.1 What is a Chi-Square Test?
The Chi-Square () test is a non-parametric statistical test commonly used to analyze categorical data.
It compares:
- Observed frequencies () – the actual frequencies obtained from data.
- Expected frequencies () – the frequencies expected according to a hypothesis.
The test helps us determine whether the difference between the observed and expected frequencies is due to random variation or represents a statistically significant difference.
In this experiment, we demonstrate the Chi-Square Goodness-of-Fit Test.
4.2 Chi-Square Goodness-of-Fit Test
The goodness-of-fit test determines whether an observed frequency distribution fits an expected distribution.
For example, suppose a die is assumed to be fair. If it is rolled many times, each face should occur approximately the same number of times. A Chi-Square test can determine whether the observed frequencies are significantly different from the expected frequencies.
4.3 Hypotheses
Null Hypothesis ()
There is no significant difference between the observed and expected frequencies.
The observed data follow the expected distribution.
Alternative Hypothesis ()
There is a significant difference between the observed and expected frequencies.
The observed data do not follow the expected distribution.
5. Problem Statement
A researcher wants to determine whether a six-sided die is fair.
The die is rolled 60 times, and the following frequencies are observed:
| Face of Die | Observed Frequency |
|---|---|
| 1 | 8 |
| 2 | 12 |
| 3 | 9 |
| 4 | 11 |
| 5 | 10 |
| 6 | 10 |
For a fair die, each face has an equal probability of occurring.
Therefore, the expected frequency for each face is:
Using a significance level of:
determine whether the die can be considered fair.
6. Manual Calculation
The Chi-Square statistic is calculated by comparing the observed and expected frequencies:
The sample does not provide enough evidence that the four proportions differfor every category.
Step 1: Create the Calculation Table
| Die Face | Observed | Expected | |||
|---|---|---|---|---|---|
| 1 | 8 | 10 | -2 | 4 | 0.40 |
| 2 | 12 | 10 | 2 | 4 | 0.40 |
| 3 | 9 | 10 | -1 | 1 | 0.10 |
| 4 | 11 | 10 | 1 | 1 | 0.10 |
| 5 | 10 | 10 | 0 | 0 | 0.00 |
| 6 | 10 | 10 | 0 | 0 | 0.00 |
Therefore:
Step 2: Calculate Degrees of Freedom
For a goodness-of-fit test:
where is the number of categories.
Here:
Therefore:
Step 3: Determine the Decision
The calculated Chi-Square statistic is:
Degrees of freedom:
At the 5% significance level, the result can be evaluated using the p-value obtained from R.
If:
- p-value < 0.05 → Reject
- p-value ≥ 0.05 → Fail to reject
Since the observed frequencies are very close to the expected frequencies, we expect no statistically significant difference.
7. R Program
# Chi-Square Goodness-of-Fit Test # Observed frequencies of the six faces of a die observed <- c(8, 12, 9, 11, 10, 10) # Expected frequencies for a fair die expected <- rep(sum(observed) / 6, 6) # Display observed and expected frequencies data <- data.frame( Face = 1:6, Observed = observed, Expected = expected ) print(data) # Perform Chi-Square Goodness-of-Fit Test result <- chisq.test(observed) print(result) # Significance level alpha <- 0.05 # Decision based on p-value if (result$p.value < alpha) { cat("\nDecision: Reject the Null Hypothesis\n") cat("Conclusion: The die does not appear to be fair.\n") } else { cat("\nDecision: Fail to Reject the Null Hypothesis\n") cat("Conclusion: There is not enough evidence to conclude that the die is unfair.\n") }
8. Explanation of the R Function
The main function used is:
chisq.test()
For this experiment:
chisq.test(observed)
Since no expected probabilities are specified, R assumes that all categories have equal probabilities.
Therefore, for a fair six-sided die:
and the expected frequency for each category is:
9. Expected Output
Face Observed Expected
1 1 8 10
2 2 12 10
3 3 9 10
4 4 11 10
5 5 10 10
6 6 10 10
Chi-squared test for given probabilities
data: observed
X-squared = 1, df = 5, p-value = 0.9626
Decision: Fail to Reject the Null Hypothesis
Conclusion: There is not enough evidence to conclude that the die is unfair.10. Interpretation
The exact interpretation is:
| Measure | Value |
|---|---|
| Chi-Square statistic | 1.0 |
| Degrees of freedom | 5 |
| Significance level | 0.05 |
| p-value | Approximately 0.96 |
Since:
we fail to reject the null hypothesis.
The observed frequencies of the six faces are not significantly different from the frequencies expected for a fair die.
Therefore, based on this sample:
There is insufficient evidence to conclude that the die is unfair.
It is important to say "fail to reject the null hypothesis" rather than "accept the null hypothesis."
11. Result
Thus, the Chi-Square goodness-of-fit test was performed using R to compare observed and expected frequencies. The Chi-Square statistic, degrees of freedom, and p-value were calculated. Since the p-value was greater than the significance level of 0.05, the null hypothesis was not rejected, indicating that the observed frequencies were consistent with those expected from a fair die.
Comments
Post a Comment