
Statistics Notes Continued:
Chi Squared Test:
With two categorical variables:
Create a table of counts.
Create a table of proportions.
Create bar plots from tables.
Categorical bivariate data.
Data where we have two categorical measurements form each individual.
Table of counts:
• used to display two categorical variables.
Example for table of counts code:
itch.table= table(duck$Infected, duck$Lake)
itch.table
itch.prop = prop.table(itch.table, 2)
barplot(itch.prop, legend=TRUE)
If the variables were not related, the proportions would be the same.
Chi-squared test continued:
Use RStudio to do a Chi-squared test.
A Chi-squared test:
• Is used with bivariate categorical data.
• Determines whether the two variables are related.
• Expected frequencies are calculated based on the null hypothesis being true.
Write the null and alternative hypotheses. Null is always 'not related' and alternative is always 'is
related'.
Code on RStudio:
chisq.test(itch.table)
Check p-value. If this is lower than 0.05, the alternative is favoured.
Conclude that there is or is not evidence.
Conditions met?
1. Sample is representative (random). Or assume, if it is reasonable to.
2. All expected counts are at least 5.
Expected counts:
Calculate expected counts.
Use RStudio to produce expected counts.
Check sample size condition of chi-squared test.