# Testing of Independence – Chi Square test – Manual, LibreOffice, R

Hi,

I have written about testing of hypothesis in my earlier posts

Statisticians recommended right testing approaches for different type of data.

When we have –

• both data as categorical, we shall use Chi Square Test
• Continuous and Continuous data, we shall use correlation
• Categorical and Continuous data, we shall use t test or anova.

In this post, I’d be using the below given data set.

   id gender      educ  Designation Level Salary Last.drawn.salary Pre..Exp Ratings.by.interviewer
1 female        UG  Jr Engineer   JLM  10000              1000        3                      4
2   male DOCTORATE     Chairman   TLM 100000            100000       20                      4
3   male   DIPLOMA        Jr HR   JLM   6000              6000        1                      3
4   male        PG     Engineer   MLM  15000             15000        7                      2
5 female        PG  Sr Engineer   MLM  25000             25000       12                      4
6   male   DIPLOMA  Jr Engineer   JLM   6000              8000        1                      1
7   male   DIPLOMA Jr Associate   JLM   8000              8000        2                      4
8 female        PG     Engineer   MLM  13000             13000        7                      3
9 female        PG     Engineer   MLM  14000             14000        7                      2
10 female        PG     Engineer   MLM  16000             16000        8                      4
11 female        UG  Jr Engineer   JLM  10000              1000        3                      4
12   male DOCTORATE     Chairman   TLM 100000            100000       20                      4
13   male   DIPLOMA        Jr HR   JLM   6000              6000        1                      3
14   male        PG     Engineer   MLM  15000             15000        7                      2
15 female        PG  Sr Engineer   MLM  25000             25000       12                      4
16   male   DIPLOMA  Jr Engineer   JLM   6000              8000        1                      1
17   male   DIPLOMA Jr Associate   JLM   8000              8000        2                      4
18 female        PG     Engineer   MLM  13000             13000        7                      3
19 female        PG     Engineer   MLM  14000             14000        7                      2
20 female        PG     Engineer   MLM  16000             16000        8                      4
21 female        PG  Sr Engineer   MLM  25000             25000       12                      4
22   male   DIPLOMA  Jr Engineer   JLM   6000              8000        1                      1
23   male   DIPLOMA Jr Associate   JLM   8000              8000        2                      4
24 female        PG     Engineer   MLM  13000             13000        7                      3
25 female        PG     Engineer   MLM  14000             14000        7                      2
26 female        PG     Engineer   MLM  16000             16000        8                      4
27 female        UG  Jr Engineer   JLM  10000              1000        3                      4
28   male DOCTORATE     Chairman   TLM 100000            100000       20                      4
29   male   DIPLOMA        Jr HR   JLM   6000              6000        1                      3
30   male        PG     Engineer   MLM  15000             15000        7                      2
31 female        PG  Sr Engineer   MLM  25000             25000       12                      4
32 female        PG  Sr Engineer   MLM  25000             25000       12                      4
33   male   DIPLOMA  Jr Engineer   JLM   6000              8000        1                      1
34   male   DIPLOMA Jr Associate   JLM   8000              8000        2                      4
35 female        PG     Engineer   MLM  13000             13000        7                      3
36 female        PG     Engineer   MLM  14000             14000        7                      2
37 female        PG     Engineer   MLM  16000             16000        8                      4
38 female        UG  Jr Engineer   JLM  10000              1000        3                      4
39   male DOCTORATE     Chairman   TLM 100000            100000       20                      4
40   male   DIPLOMA        Jr HR   JLM   6000              6000        1                      3
41   male        PG     Engineer   MLM  15000             15000        7                      2
42 female        PG  Sr Engineer   MLM  25000             25000       12                      4
43   male   DIPLOMA  Jr Engineer   JLM   6000              8000        1                      1
44   male   DIPLOMA Jr Associate   JLM   8000              8000        2                      4
45 female        PG     Engineer   MLM  13000             13000        7                      3
46 female        PG     Engineer   MLM  16000             16000        8                      4
47 female        UG  Jr Engineer   JLM  10000              1000        3                      4
48   male DOCTORATE     Chairman   TLM 100000            100000       20                      4
49   male   DIPLOMA        Jr HR   JLM   6000              6000        1                      3
50   male        PG     Engineer   MLM  15000             15000        7                      2


We shall use chi square test for two types of hypothesis testing

• test of independence of variables
• test goodness of fit

### Testing of independence

We can find out the association between two (at least) categorical variables. Higher the chi square value, better the result is. We shall use this to test our hypothesis.

### Goodness of fit

When we use chi square test to find the goodness of fit, we shall use 2 categorical variables. higher the chi square value, better the result is. We shall use this to test BLR, SEM tests.

### Example for Testing of independence

This post talks about testing of independence. We have employee data given above. Following are my hypothesis.

H0 = Number of female employees and level of management are not related.

H1 = Number of female employees and level of management are related.

We would solve this using three methods

1. Manual way of chi square test
2. Chi square test with LibreOffice Calc
3. Chi square test with R

#### Manual way of chi square test

We prepare the count of female employees in each level as given below. I have used COUNTIFS() function of LibreOffice.

Calculate the row (highlighted in pink colour) and column sums (blue colour) and summation of all row sums (saffron colour).

The values are called observed values. We shall find out the expected values as well easily as given below.

Expected value = column sum x row sum/sum of rowsum

=J15*N12/N15 = 25 x 20/50 = 10

Finally our table looks like this.

All the observed values (O), Expected values (E) are substituted in the below table. We calculate the Chi square value χ2 which is 19.

 O E O-E (O-E)2 (O-E)2/E 5 10 -5 25 2.5 20 12.5 7.5 56.25 4.5 0 2.5 -2.5 6.25 2.5 15 10 5 25 2.5 5 12.5 -7.5 56.25 4.5 5 2.5 2.5 6.25 2.5 χ2 19

Level of significance or Type 1 error = 5%, which is 0.05

Degrees of freedom = (row count – 1) x (column count – 1) = 2

Critical value of χ2 is 5.991, which is looked up using the level of significance and degrees of freedom in the below given table.

Make a decision

To accept our null hypothesis H0, calculated χ2 < critical χ2.
Our calculated χ2 = 19
Our critical χ2 = 5.991

Hence, we reject null hypothesis and accept alternate hypothesis.

You may watch the following video to understand the above calculation.

#### Chi square test with LibreOffice Calc

We have already found out the frequency distribution of females and males per each management level. Let’s use the same.

Select Data>Statistics>Chi-square Test

Choose the input cells

Select the Output Cell

Finally my selections are given as below

After pressing OK, We get the following result

Make a decision

If pα reject the null hypothesis. If p>α fail to reject the null hypothesis.

Our p 0.00007485 is lesser than alpha 0.05. So null hypothesis is rejected and alternate hypothesis is accepted.

#### Chi square test with R

I have the data set stored as sal.csv file. I’m importing it and store to sal object.

> setwd("d:/gandhari/videos/Advanced Business Analytics/")
id gender      educ Designation Level Salary Last.drawn.salary Pre..Exp Ratings.by.interviewer
1  1 female        UG Jr Engineer   JLM  10000              1000        3                      4
2  2   male DOCTORATE    Chairman   TLM 100000            100000       20                      4
3  3   male   DIPLOMA       Jr HR   JLM   6000              6000        1                      3
4  4   male        PG    Engineer   MLM  15000             15000        7                      2
5  5 female        PG Sr Engineer   MLM  25000             25000       12                      4
6  6   male   DIPLOMA Jr Engineer   JLM   6000              8000        1                      1


As I wrote in Exploring data files with R I create a Frequency Distribution table using table() function.

> gender_level_table <- table(sal$Level, sal$gender)
> gender_level_table

female male
JLM      5   15
MLM     20    5
TLM      0    5


Use chisq.test() function with gender_level_table as its input, to run the chi square test

> chisq.test(gender_level_table)

Pearson's Chi-squared test

data:  gender_level_table
X-squared = 19, df = 2, p-value = 7.485e-05

Warning message:
In chisq.test(gender_level_table) :
Chi-squared approximation may be incorrect


Make a decision

If pα reject the null hypothesis. If p>α fail to reject the null hypothesis.

Our p 7.485e-05 is lesser than alpha 0.05. So null hypothesis is rejected and alternate hypothesis is accepted.

See you in another interesting post. Happy Sunday.

# Calculating Anova with LibreOffice Calc

I have written about the manual way of calculating one-way anova in One way analysis of variance. We shall use LibreOffice (or MS Excel) to calculate it quickly. I’d explain with Libre Office below.

### 1. Define the NULL (H0) and alternative (HA) hypothesis.

H0: there is no difference between three conditions with loan scheme.

μ ₹10,000.00 = μ ₹15,000.00 = μ ₹20,000.00

H1: There is a difference. Not all μs are equal.

### 2. Define the alpha value for type I error.

α = 5% = 0.05.

Select Data>Statistics>Anova

### 4. And Here is your output

 Groups Count Sum Mean Variance Column 1 7 57 8.1429 0.4762 Column 2 7 47 6.7143 0.5714 Column 3 7 21 3 0.6667
 Source of Variation SS df MS F P-value F critical Between Groups 98.6667 2 49.3333 86.3333 5.9563E-10 3.5546 Within Groups 10.2857 18 0.5714 Total Err:508 20

86.33 3.5546, hence the null hypothesis is rejected, alternate hypothesis is accepted.

# One way analysis of variance

I’d be writing about ANOVA in this post, after my previous post on Skew & kurtosis. ANOVA is a technique to perform statistical intervention on one or more than two populations at same time to analyze the data effectively.

Though there are several other types in anova, I’d discuss about One-way anova in this post.

### What is Hypothesis?

A Hypothesis is a tentative statement about relationship between two or more variables. It is a specific, testable prediction about what do you expect to happen in your investigation.

### Types of Hypothesis

Following is the major types of statistical hypothesis.

H0: Null Hypothesis: It is usually hypothesis that sample the observations based on chance.

H1: Alternate Hypothesis: It is the hypothesis that sample observations are influenced by some non-random cause.

When we collect the air quality of Mumbai in 2016, a null hypothesis may be like this – there is no change in quality between second and third quarters of 2016. An alternate hypothesis H1 may be, the quality is poorer in third quarter of 2016.

### What is a Hypothesis testing?

Hypothesis testing is a process to prove or disprove the research question. By allowing an error of 5% or 1% (α alpha values), the researcher can conclude that result may be real if chance alone could produce the same result only 5% or 1% of the time or less.

Let’s take a research question..

Is the mean salary of an IT family in Bangalore equal to ₹ 40000?

we need to write this question in terms of null (H0) and alternate (HA) hypothesis.

The null hypothesis is, μ = ₹ 40000.

H0: μ = ₹ 40000

The alternative hypothesis is μ ₹ 40000

HA: μ ₹ 40000

HA suggests us that the salary may be lesser than 40000, or greater than 40000. We call this phenomenon as two-tailed tests.

### Type I, Type II errors

Generally we may ignore certain percentage of measurements, usually these are peak measurements. We call them as Type I error represented by α (alpha). Generally it would be 5% (0.05) or 1% (0.01)

A Type II error is used to identify the causes to reject a false null hypothesis. It is generally good not to ignore Type II errors.

### Calculating ANOVA – manual approach

Let’s take a simple data. Banks gives different loan amounts to entrepreneurs of three different investment slabs. It gives ₹10000, ₹15000 and ₹20000 respectively.

Following is the returns obtained by each slabs.

 ₹10,000.00 ₹15,000.00 ₹20,000.00 9 7 4 8 6 3 7 6 2 8 7 3 8 8 4 9 7 3 8 6 2

1. Define the NULL (H0) and alternative (HA) hypothesis.

H0: there is no difference between three conditions with loan scheme.

μ ₹10,000.00 = μ ₹15,000.00 = μ ₹20,000.00

H1: There is a difference. Not all μs are equal.

2. Define the alpha value for type I error.

α = 5% = 0.05.

3. Determine the degree of freedom (DF)

Number of samples N = 21

Number of groups/columns/levels/contitions a  = 3

Number of rows n = 7

Degree of freedom between columns dfbetween = a – 1 = 3 – 1  =2.

Degree of freedom between rows dfbetween = N – a = 21 – 3 = 18.

Degree of freedom for all the data (total) dftotal = N – 1 = 21 – 1 = 20.

4. Decision rules

To look up the critical value, two df values need to be used.

Look at the F table for the critical value using v1, v2, ie., (2, 18) (alpha = 0.05)

Next, Look at the statistical table for 2 in v1 column and 18 in V2 row. We find 3.555. —-(0)

So the rule is, if F (calculated value) is greater than 3.555 (F table value), reject the null hypothesis or else, accept the null hypothesis.

5. Calculate the statistics

 ₹10,000.00 ₹15,000.00 ₹20,000.00 Sample x Sample x ² Sample i Sample i² Sample j Sample j² 9 81 7 49 4 16 8 64 6 36 3 9 7 49 6 36 2 4 8 64 7 49 3 9 8 64 8 64 4 16 9 81 7 49 3 9 8 64 6 36 2 4 ΣTi = 57 ΣTi = 47 ΣTi = 21 Σx² = 467 Σi² = 319 Σj² = 67

Sum of all samples T = ΣTi = 57 + 47 + 21 = 125 ————- (1)

So 125 is the total sum of all samples.

ΣΣxij²= 467 + 319 + 67 = 853 ———– (2)

Using (1) and (2),

Q = ΣΣxij²-T²/N

Q = 853 – 125²/21

Q = 853 – 15625/21

Q = 853 – 744.04761904761904761904761904762

Q = 108.952380952381 ———————-(3)

Q1 = Σ(Ti²/ni)-T²/N

Q1 = (Σ(57 + 47 + 21)/7) – (125²/21)

Q1 = 98.6666666666666 —————–(4)

Q2 = Q – Q1

Q2 = (3) – (4)

Q2 = 10.2857142857143 —————-(5)

### Anova Table

 Source of variations (SV) Sum of squares (SS) Degrees of freedom (df) Mean Square (MS) Variance Ratio (F) Between classes Q1 h-1 Q1/h-1 MSbetween/MSwithin Within Classes Q2 N-h Q2/N-h Total Q N-1 —

Lets substitute the values in the above table.

We already calculated the values for Q1, Q2 and Q above.

h is the number of columns, which is 3.

h = 3 —————(6)

So,

h-1 = 2 ———–(7)

N-h = 21-3 = 18 ———–(8)

N – 1 = 20 —————-(9)

MSbetween = Q1/h1 = 98.67/2 = 49.335 ————(10)

MSwithin = Q2/N-h = 10.29/18 = 0.57167 ————-(11)

Finally our F value would be,

F = MSbetween/MSwithin

F = 49.335/0.57167 = 86.2998 —————–(12)

 Source of variations (SV) Sum of squares (SS) Degrees of freedom (df) Mean Square (MS) Variance Ratio (F) Between classes 98.67 2 49.335 86.2998 Within Classes 10.29 18 0.57167 Total 10.28 20 —

### Conclusion

Our calculated F value (variance ratio) is 86.2998

Statistical value is 3.55 as per (0)

Hence calculated F value is greater than statistical value.

F(2, 18) = 86.2998 3.55. Hence our null hypothesis is rejected and alternate hypothesis is accepted. There is a difference between the loan schemes. Not all μs are equal.