# Contingency Table in R

Contingency Table in R, In the test hypothesis, it is usually assumed sample drawn from a known distribution like binomial, Poisson, normal, etc…It is an assumption but good to check our assumption holds true or not.

How to check the assumption hold true or not?.

It is based on outcomes or observations. Based on the chi-square test is used for identifying the discrepancy between the observed frequencies and theoretical frequencies.

If the discrepancy is not large, it’s considered that our assumption is true otherwise false. This is also called the goodness of fit test.

## Null Hypothesis

Ho:data comes from the assumed distribution

H1:data does not come from the assumed distribution

In chi-square distribution has n-1 degrees of freedom

The decision criteria are based on the chi-square statistic, If chi-square > tabled value reject the null hypothesis and accept the alternate hypothesis.

## When we need the goodness of fit test?

If the calculated value of chi-square is less than or equal to the degrees of freedom of chi-square then the goodness of the test is not required.

What is a contingency table and what kind of role has chi-square distribution in contingency table analysis?

A contingency table is a two-way table in which the columns are classified into one attribute and rows are classified into other attributes.

Each cell contains 0ij items, i indicate the ith number of row,s and j indicate a jth number of columns. Each row or column total is known as the marginal total.

## Why we need a contingency table?

The contingency table will help us to identify the independence of two attributes. Most of the cases are used to explain the variation in one variable through the variation of other variables.

If the cell contains zero cell values then it’s called an incomplete contingency table.

In a contingency table, all rows contain zero values than it’s called structural zeros because the probability of having an observation in the cell is zero.

## Does the contingency table mean only 2 by 2 tables?

Contingency tables can have 2, 3, 4, or more dimensional tables. The Chi-square tests help us to test the independence of two attributes.

As usual chi-square value greater than the tabled value rejects the null hypothesis and accepts the alternate hypothesis.

## When we use the Fischer Exact test?

If any of the cell counts are less than 5 Fischer exact tests are more ideal compared to the chi-square test.

When the sample size is large and cell frequencies are more than 5 chi-square test is appropriate.

## Contingency Table in R

```library(compareGroups)
ans<-compareGroups(Groups~.,data=data, max.ylev=30, max.x.lev=20)
update(ans,method=3,show.all=TRUE)```

Method 3 indicates Fischer or Chi-Square test.

The script automatically executes based on cell counts Fischer or Chi-Square test to be applied.

## Conclusion

Based on compare group analysis in R easily can execute Fischer Exact or Chi-Square test and if the p-value is less than 0.05 reject the null hypothesis and accept the alternate hypothesis.

Proportion test in R