encouraging academics to share statistics support resources
All stcp resources are released under a Creative Commons licence
stcp-karadimitriou-chisqR
The following resources are associated:
Summarising Categorical Data in R, Logistic Regression in R and the Excel dataset ‘Titanic.csv’
Chi-squared test for association in R
Research Question Type: Association of two categorical variables
What kind of variables: Categorical (nominal or ordinal with a few categories)
Common Applications: Association between two categorical variables.
The chi-squared test tests the hypothesis that there is no relationship between two categorical
variables. It compares the observed frequencies from the data with frequencies which would be
expected if there was no relationship between the variables.
Data: On April 14th 1912 the ship the Titanic sank. Only 705 passengers and crew out of the total
2228 population on board survived. Information on 1309 of those on board will be used to
demonstrate summarizing categorical variables.
After saving the ‘Titanic.csv’ file somewhere on your computer, open the data, call it TitanicR and
define it as a data frame. Attach the data so variables can be referred to by their column name.
TitanicR<-data.frame(read.csv('...\\Titanic.csv',header=T,sep=','))
attach(TitanicR)
R needs to know which variables are categorical variables and the labels for each value which can
be specified using the factor command.
variable<-factor(variable,c(category numbers),labels=c(category names)).
The values are as follows: survival (0=died, 1=survived), Gender (0 = male, 1 = female), Country of
Residence (Residence=American, British, Other).
survived<-factor(survived,c(0,1),labels=c(‘Died','Survived'))
Residence<-
factor(Residence,levels=c(0,1,2),labels=c('American','British','Other'))
Gender<-factor(Gender,levels=c(0,1),labels=c('Male','Female'))
© Sofia Maria Karadimitriou and Ellen Marshall Reviewer: Paul Wilson
University of Sheffield University of Wolverhampton
Based on material provided by Mollie Gilchrist and Peter Samuels of Birmingham City University