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:

  1. Understand the purpose of the Chi-Square test.
  2. Formulate null and alternative hypotheses.
  3. Calculate expected frequencies.
  4. Calculate the Chi-Square statistic manually.
  5. Perform a Chi-Square test using R.
  6. Interpret the Chi-Square statistic and p-value.
  7. Make a statistical decision based on the test result.

4. Theory

4.1 What is a Chi-Square Test?

The Chi-Square (χ2\chi^2) test is a non-parametric statistical test commonly used to analyze categorical data.

It compares:

  • Observed frequencies (OO) – the actual frequencies obtained from data.
  • Expected frequencies (EE) – 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 (H0H_0)

H0:H_0:

There is no significant difference between the observed and expected frequencies.

The observed data follow the expected distribution.

Alternative Hypothesis (H1H_1)

H1:H_1:

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
18
212
39
411
510
610

For a fair die, each face has an equal probability of occurring.

Therefore, the expected frequency for each face is:

E=606=10E=\frac{60}{6}=10

Using a significance level of:

α=0.05\alpha=0.05

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 differ

For this problem:E=10E=10

for every category.

Step 1: Create the Calculation Table

Die FaceObserved OOExpected EEO−EO-E(O−E)2(O-E)^2(O−E)2E\frac{(O-E)^2}{E}
1810-240.40
21210240.40
3910-110.10
41110110.10
51010000.00
61010000.00

Therefore:

χ2=0.40+0.40+0.10+0.10+0+0\chi^2=0.40+0.40+0.10+0.10+0+0 χ2=1.00\boxed{\chi^2=1.00}

Step 2: Calculate Degrees of Freedom

For a goodness-of-fit test:

df=k−1df=k-1

where kk is the number of categories.

Here:

k=6k=6

Therefore:

df=6−1=5df=6-1=\boxed{5}

Step 3: Determine the Decision

The calculated Chi-Square statistic is:

χ2=1.00\chi^2=1.00

Degrees of freedom:

df=5df=5

At the 5% significance level, the result can be evaluated using the p-value obtained from R.

If:

  • p-value < 0.05 → Reject H0H_0
  • p-value ≥ 0.05 → Fail to reject H0H_0

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:

P(1)=P(2)=P(3)=P(4)=P(5)=P(6)=16P(1)=P(2)=P(3)=P(4)=P(5)=P(6)=\frac{1}{6}

and the expected frequency for each category is:

606=10\frac{60}{6}=10

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:

MeasureValue
Chi-Square statistic    1.0
Degrees of freedom    5
Significance level    0.05
p-value    Approximately 0.96

Since:

p-value>0.05p\text{-value}>0.05

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

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3