CMSE11624 Statistics for Analytics

Session 8: Chi-Square Tests and ANOVA

Dr Zexun Chen

Table of Contents

Test for Association

Travel Preferences Survey

A survey has been conducted among subscribers to the newsletter by Visit Scotland about their travel plans next summer.

  • A random sample of 250 subscribers was asked - what was more important when deciding if to visit: Safety or Price.

  • The results are grouped by region.

Is there a relationship between Region and Perceived Importance of Safety vs Price (Perception)?

image

Contingency Table

image
  • If the Totals and just two numbers in the middle are known, one can fill in the rest of the table

  • This table has 2 degrees of freedom (df) \[\text{df = (\# rows - 1)} \times \text{(\# columns - 1)}\]

Hypotheses

If we are interested in proving that there is a relationship, this should be the alternative hypothesis.

  • \(H_0\): Region and Perception are Independent (Independent)

  • \(H_1\): Region and Perception are Dependent (Associated)

Statistical Independence

  • statistically independent \[\mathbb{P}(A \cap B) = \mathbb{P}(A) \times \mathbb{P}(B)\]

  • statistically dependent \[\mathbb{P}(A \cap B) \neq \mathbb{P}(A) \times \mathbb{P}(B)\]

Independence

image

Expected Values

image

What use is Expected Value

Can Compare the Expected Value \(e_{ij}\) with Observed Value \(o_{ij}\)

  • If \(e_{ij}\) is close to \(o_{ij}\), then \(H_0\) is likely to be true.

  • If \(e_{ij}\) is NOT close to \(o_{ij}\), then \(H_1\) is likely to be true.

How Close? What Measure?

Pearson Chi Square Statistic - \(\chi^2\) \[\chi^2 = \sum_{i=1}^{r}\sum_{j=1}^{c} \frac{(o_{ij} - \hat{e}_{ij})^2}{\hat{e}_{ij}}\]

Observed/Expected Values

image

Calculation

image

Why Square?

  • Remove Sign: Squaring ensures all differences are positive, focusing on the magnitude of deviation.

  • Emphasise Larger Discrepancies: Squaring gives more weight to larger differences, which are more indicative of a true discrepancy from the null hypothesis.

  • Distribution: The sum of squared standardised differences follows a Chi-Square distribution if the null hypothesis is true, allowing for theoretical comparison and p-value calculation.

  • Variances and Standard Deviations: Squaring differences relate to the concept of variance, which measures dispersion, making the Chi-Square test a form of variance measure.

The Chi-Square statistic is used to determine the probability of observing a test statistic as extreme as the one calculated, under the assumption of the null hypothesis.

Chi-Square Distribution

  • In order to determine if \(\chi^2\) statistic we calculated is considered as "unusually" high or not we need to first describe its distribution

  • The chi-square distribution has just one parameter called degrees of freedom (df), which influences the shape, centre, and spread of the distribution.

  • The chi-squared distribution is the sum of squared standardised normal random variables such as \((Z_1)^2 + (Z_2)^2 + (Z_3)^2\) and so on.

image

Chi-Square Distribution and Table

image

Chi-Square Table & Software

image
> scipy.stats.chi2.ppf(1-0.05, 2) # critical value, 1-alpha
[1] 5.991464547107979
> scipy.stats.chi2.isf(0.05, 2) # critical value, alpha
[1] 5.991464547107983
> scipy.stats.chi2.sf(29.28, 2) # p-value
[2] 4.3845879395753135e-07
> scipy.stats.chi2.cdf(29.28, 2) # If we need p-value, 1- cdf
[3] 0.9999995615412061

Finishing Off

image

Steps: Chi-Square Test for Association

  1. Formulate \(H_0\) and \(H_1\).

  2. Specify the level of significance and test statistic.

  3. Calculate Test Statistic using Observed Table:

    • Calculate Expected Table using \(\hat{e}_{ij} = \frac{R_i \times C_j}{n}\)

    • Calculate Pearson Chi Squared Statistic \(\chi^2 = \sum_{i=1}^{r} \sum_{j=1}^{c} \frac{(o_{ij} - \hat{e}_{ij})^2}{\hat{e}_{ij}}\)

  4. Critical region and critical value \(\chi^2\): (r-1)(c-1), \(\alpha\).

  5. Decide on Hypotheses and Interpret Decision.

A Few Remarks

  • Only one-tail case, No two-tail case

  • Chi-Square statistic needs to use Chi-Square distribution, thus \(e_{ij}\) usually needs to be at least 5.

  • Chi-squared test of association can be applied to any type of data (nominal, ordinal, interval, ratio)

  • Chi-squared test of association is extremely useful for the analysis of surveys

  • But it only tests whether the relationship exists, to measure the strength of association use other statistics

Measures of Association Nominal Data

image

Measures of Association Nominal Data

image

Test for Goodness of Fit

Test of Goodness of Fit

image image

Account Type Preferences Among Bank Customers

Background: The bank offers three types of accounts and has expectations for the distribution of customer preferences.

Objective: To determine if the observed preferences fit the expected distribution using a Chi-square test of goodness of fit.

Data Collection: A survey of 300 customers yielded the following preferences:

  • Savings: 140 customers

  • Checking: 90 customers

  • Investments: 70 customers

Expected Distribution:

  • Savings: 50%

  • Checking: 30%

  • Investments: 20%

Hypotheses: \[H_0: \text{The distribution of preferences matches the expected.}\] \[H_1: \text{The distribution of preferences does not match the expected.}\]

Table and Figure

Customer Distribution Across Account Types
Account Type Number of Customers Expected Distribution
Savings 140 50%
Checking 90 30%
Investments 70 20%

image

Solution

Expected Frequencies:

  • Savings: \(E_{\text{Savings}} = 300 \times 0.50 = 150\)

  • Checking: \(E_{\text{Checking}} = 300 \times 0.30 = 90\)

  • Investments: \(E_{\text{Investments}} = 300 \times 0.20 = 60\)

Chi-Square Statistic Calculation: \[\chi^2 = \frac{(140 - 150)^2}{150} + \frac{(90 - 90)^2}{90} + \frac{(70 - 60)^2}{60} = 2.33\]

Degrees of Freedom: \[df = 3 - 1 = 2\]

Critical Value: Find from Chi-square table or statistical software for \(df = 2\) at \(\alpha = 0.05\) (\(\chi^2_{2, 0.05} = 5.99\)).

Decision: Compare the calculated \(\chi^2\) to the critical value to decide on \(H_0\).

  • Insufficient evidence to reject \(H_0\) (2.33 < 5.99), match the expectation.

IQ-Computer: Normal Distribution Goodness of Fit Test

image

IQ-Computer: Normal Distribution Goodness of Fit Test

image

Hypothesis

  • \(H_0\): The population of number of units sold has a normal distribution with mean 71 and standard deviation 18.54.

  • \(H_1\): The population of number of units sold does NOT have a normal distribution with mean 71 and standard deviation 18.54.

Interval Definition

To satisfy the requirement of an expected frequency of at least 5 in each interval we will divide the normal distribution into 30/5 = 6 equal probability intervals.

image

Observed and Expected Frequencies

image

Statistic and Decision

image

A Few Remarks

The assumptions for the Chi-Square Goodness Of Fit Test include:

  • Categorical Variable: The variable must be categorical with more than two categories, and without a natural order.

  • Independence: Observations should be independent, meaning the value of one observation does not depend on the value of another.

  • Mutually Exclusive Groups: Categories must be mutually exclusive, meaning each observation can only belong to one category.

Important: If these assumptions are violated, the results of the test may not be valid.

Some issues of Normal distribution Goodness of Fit:

  • Only test, if the observation follows a normal distribution given both, mean and variance (very strong requirements for the null hypothesis).

  • It is usually a "rough" solution because of the selection of interval.

  • Other formal solutions: e.g.,Kolmogorov-Smirnov test for normality.

Test for Homogeneity

Test for Homogeneity

The Chi-square test for homogeneity is similar in structure to the test for independence, but it serves a different purpose:

  • Test for Independence: Seeks an association between two categorical variables within the same population.

  • Test for Homogeneity: Compares the distribution of a categorical variable across different populations or treatments, considering the population as a categorical variable itself.

The hypotheses for a Chi-square test of homogeneity are:

  • Null Hypothesis (\(H_0\)): There is no difference in the distribution of a categorical variable across the populations or treatments.

  • Alternative Hypothesis (\(H_1\)): There is a difference in the distribution of a categorical variable across the populations or treatments.

Take Home Messages

Take Home Messages

Chi-Square Test

  • Test for independence/Association: Two Aspects of ONE population

    • Calculate Expected Table using \(\hat{e}_{ij} = \frac{R_i \times C_j}{n}\)

    • Calculate Pearson Chi Squared Statistic \(\chi^2 = \sum_{i=1}^{r} \sum_{j=1}^{c} \frac{(o_{ij} - \hat{e}_{ij})^2}{\hat{e}_{ij}}\)

    • Critical region and critical value \(\chi^2\): (r-1)(c-1), \(\alpha\).

  • Test for Goodness of Fit: Observation fits the expectation

  • Test for Homogeneity: Two Populations

Optional: Inference for Variances

Optional: Inference for Variances

  • Inference about a Population Variance

    • Hypothesis Testing

    • Interval Estimation

  • Inferences about Two Population Variances

Hypothesis test for Variance

image

Note: here the degree of freedom in \(\chi^2\) distribution is \(n-1\).

Rejection Region

image

Interval Estimation of \(\sigma^2\)

image

Hypothesis test for Variances of Two Populations

image

Note: Here the value of \(F_{\alpha}\) is based on a F-distribution with \(n_1 - 1\) (numerator), and \(n_2 - 1\) (denominator)

  • F-distribution is NOT a symmetric distribution

  • The degree of freedom for numerator and denominator cannot change the order!

F distribution

image

ANOVA

ANOVA

Problem

  • You are a data analyst in the UK’s banking sector.

  • Management wants to know if there’s a significant difference in average loan approval times among:

    • Lloyds Bank,

    • HSBC

    • NetWest

    • Barclays

    • TSB

    • etc

image

ANOVA

  • To compare means of 2 groups we use a Z or a T statistic.

  • To compare means of 3+ groups we use a new test called ANOVA and the statistic called F.

ANOVA (Analysis of Variance ) is used to assess whether the mean of the outcome variable is different for different levels of a categorical variable.

  • \(H_0:\) The mean outcome is the same across all categories, \[\mu_1 = \mu_2 = \cdots = \mu_k,\] where \(\mu_i\) represents the mean of the outcome for observations in category \(i\).

  • \(H_1:\) At least one mean is different than others.

Conditions

  1. The observations should be independent within and between groups

    • If the data are a simple random sample from less than 10% of the population, this condition is satisfied.

    • Carefully consider whether the data may be independent (e.g. no pairing).

    • Always important, but sometimes difficult to check.

  2. The observations within each group should be nearly normal.

    • Especially important when the sample sizes are small.
  3. The variability across the groups should be about equal.

    • Especially important when the sample sizes differ between groups.

\(z\)/\(t\) test vs. ANOVA - Purpose

\[z/t\text{ test}\] Compare means from two groups to see whether they are so far apart that the observed difference cannot reasonably be attributed to sampling variability. \[H_0: \mu_1 = \mu_2\]

\[ANOVA\] Compare the means from two or more groups to see whether they are so far apart that the observed differences cannot all reasonably be attributed to sampling variability. \[H_0: \mu_1 = \mu_2 = \cdots = \mu_k\]

\(z\)/\(t\) test vs. ANOVA - Method

\[z/t\text{ test}\] Compute a test statistic (a ratio). \[z / t = \frac{(\bar{x}_1 - \bar{x}_2) - (\mu_1 - \mu_2)}{SE_{\bar{x}_1 - \bar{x}_2}}\]

\[ANOVA\] Compute a test statistic (a ratio). \[F = \frac{\text{variability bet. groups}}{\text{variability w/in groups}}\]

  • Large test statistics lead to small p-values.

  • If the p-value is small enough \(H_0\) is rejected, we conclude that the population means are not equal.

\(z\)/\(t\) test vs. ANOVA

  • With only two groups t-test and ANOVA are equivalent, but only if we use a pooled standard variance in the denominator of the test statistic.

  • With more than two groups, ANOVA compares the sample means to an overall grand mean.

Example Data

image
n mean sd
bottom 10 6.04 1.58
middepth 10 5.05 1.10
surface 10 4.20 0.66
overall 30 5.1 0 1.37

Hypotheses

What are the correct hypotheses for testing for a difference between the means among these groups?

  1. \(H_0: \mu_B = \mu_M = \mu_S\)
    \(H_1: \mu_B \ne \mu_M \ne \mu_S\)

  2. \(H_0: \mu_B \ne \mu_ M \ne \mu_S\)
    \(H_1: \mu_B = \mu_M = \mu_S\)

  3. \(H_0: \mu_B = \mu_M = \mu_S\)
    \(H_1:\) At least one mean is different.

  4. \(H_0: \mu_B = \mu_M = \mu_S = 0\)
    \(H_1:\) At least one mean is different.

  5. \(H_0: \mu_B = \mu_M = \mu_S\)
    \(H_1: \mu_B > \mu_M > \mu_S\)

Test statistic

Does there appear to be a lot of variability within groups? How about between groups?

  • Total Variability = sum up (each observation - grand mean)\(^2\)

  • Between group variability = sum up (each group’s mean - grand mean)\(^2\)

  • Within group variability = Total variability - between group variability

\[F = \frac{\text{variability bet. groups}}{\text{variability w/in groups}}\]

image

\(F\) distribution and p-value

\[F = \frac{\text{variability bet. groups}}{\text{variability w/in groups}}\]

image
  • In order to be able to reject \(H_0\), we need a small p-value, which requires a large F statistic.

  • In order to obtain a large F statistic, variability between sample means needs to be greater than variability within sample means.

Degrees of freedom associated with ANOVA

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.13 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

Degrees of freedom associated with ANOVA

  • groups: \(df_G = k - 1\), where \(k\) is the number of groups

  • total: \(df_T = n - 1\), where \(n\) is the total sample size

  • error: \(df_E = df_T - df_G\)

  • \(df_G = k - 1 = 3 - 1 = 2\)

  • \(df_T = n - 1 = 30 - 1 = 29\)

  • \(df_E = 29 - 2 = 27\)

Sum of squares between groups, SSG

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.13 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

Sum of squares between groups, SSG Measures the variability between groups \[SSG = \sum_{i = 1}^{k} n_i (\bar{x}_i - \bar{x})^2\] where \(n_i\) is each group size, \(\bar{x}_i\) is the average for each group, \(\bar{x}\) is the overall (grand) mean.

n mean
bottom 10 6.04
middepth 10 5.05
surface 10 4.2
overall 30 5.1

\[\begin{aligned} SSG &=& \left( 10 \times (6.04 - 5.1)^2 \right) \\ &+& \left( 10 \times (5.05 - 5.1)^2 \right) \\ &+& \left( 10 \times (4.2 - 5.1)^2 \right) \\ &=& 16.96 \\ \end{aligned}\]

Sum of squares total, SST

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.13 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

Sum of squares total, SST Measures the variability in total \[SST = \sum_{i = 1}^{n} (x_i - \bar{x})^2\] where \(x_i\) represent each observation in the dataset.

\[\begin{aligned} SST &=& (3.8 - 5.1)^2 + (4.8 - 5.1)^2 + (4.9 - 5.1)^2 + \cdots + (5.2 - 5.1)^2 \\ &=& (-1.3)^2 + (-0.3)^2 + (-0.2)^2 + \cdots + (0.1)^2 \\ &=& 1.69 + 0.09 + 0.04 + \cdots + 0.01 \\ &=& 54.29 \end{aligned}\]

Sum of squares error, SSE

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.13 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

Sum of squares error, SSE Measures the variability within groups: \[SSE = SST - SSG\]

\[SSE = 54.29 - 16.96 = 37.33\]

Mean square error

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.13 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

Mean square error Mean square error is calculated as sum of squares divided by the degrees of freedom.

\[\begin{aligned} MSG &=& 16.96 / 2 = 8.48 \\ MSE &=& 37.33 / 27 = 1.38 \end{aligned}\]

Test statistic, F value

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.14 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

Test statistic, F value As we discussed before, the F statistic is the ratio of the between group and within group variability. \[F = \frac{MSG}{MSE}\]

\[F = \frac{8.48}{1.38} = 6.14\]

p-value

Df Sum Sq Mean Sq F value Pr(\(>\)F)
(Group) depth 2 16.96 8.48 6.14 0.0063
(Error) Residuals 27 37.33 1.38
Total 29 54.29

p-value p-value is the probability of at least as large a ratio between the “between group" and”within group" variability, if in fact the means of all groups are equal. It’s calculated as the area under the F curve, with degrees of freedom \(df_G\) and \(df_E\), above the observed F statistic.

image

Conclusion - in context

What is the conclusion of the hypothesis test?

\(\:\)
The data provide convincing evidence that the average mean

  1. is different for all groups.

  2. on the surface is lower than the other levels.

  3. is different for at least one group.

  4. is the same for all groups.

image

Conclusion

  • If p-value is small (less than \(\alpha\)), reject \(H_0\). The data provide convincing evidence that at least one mean is different from (but we can’t tell which one).

  • If p-value is large, fail to reject \(H_0\). The data do not provide convincing evidence that at least one pair of means are different from each other, the observed differences in sample means are attributable to sampling variability (or chance).

(1) independence

Does this condition appear to be satisfied?

(2) approximately normal

Does this condition appear to be satisfied?

image

(3) constant variance

Does this condition appear to be satisfied?

image

Which means differ?

  • Earlier we concluded that at least one pair of means differ. The natural question that follows is “which ones?"

  • We can do two sample \(t\) tests for differences in each possible pair of groups.

Which means differ?

Based on the box plots below, which means would you expect to be significantly different?

image
  1. bottom & surface

  2. bottom & mid-depth

  3. mid-depth & surface

  4. bottom & mid-depth; mid-depth & surface

  5. bottom & mid-depth; bottom & surface; mid-depth & surface

Which means differ? (cont.)

If the ANOVA assumption of equal variability across groups is satisfied, we can use the data from all groups to estimate variability:

  • Estimate any within-group standard deviation with \(\sqrt{MSE}\), which is \(s_{pooled}\)

  • Use the error degrees of freedom, \(n - k\), for \(t\)-distributions

Difference in two means: after ANOVA \[SE = \sqrt{ \frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2} } \approx \sqrt{ \frac{MSE}{n_1} + \frac{MSE}{n_2} }\]

Is there a difference between the mean at the bottom and at mid-depth?

Is there a difference between the mean at the bottom and at mid-depth?

n mean sd
bottom 10 6.04 1.58
middepth 10 5.05 1.10
surface 10 4.2 0.66
overall 30 5.1 1.37
Df Sum Sq Mean Sq F value Pr(\(>\)F)
depth 2 16.96 8.48 6.13 0.0063
Residuals 27 37.33 1.38
Total 29 54.29
$$ \[\begin{aligned} T_{df_E} &=& \frac{(\bar{x}_{bottom} - \bar{x}_{middepth})}{\sqrt{ \frac{MSE}{n_{bottom}} + \frac{MSE}{n_{middepth}} }} \\ T_{27} &=& \frac{( 6.04 - 5.05 )}{\sqrt{ \frac{1.38}{10} + \frac{1.38}{10} }} = \frac{0.99}{0.53} =1.87 \\ 0.05 &<& p-value < 0.10 \qquad \text{(two-sided)} \\ % \alpha^\star &=& 0.05 / 3 = 0.0167 \end{aligned}\]

$$