Main idea

Statistics can help us examine whether survival on the Titanic was related to different passenger characteristics.

For this example, I decided to use the Titanic data set in built into R.

  • Response variable: survival status
  • Explanatory variables: gender, passenger class, and age group
  • Main question: were the survival outcomes independent or dependent on these categories?

Data

##   Class    Sex   Age Survived Freq ClassNumber AgeNumber
## 1   1st   Male Child       No    0           1         1
## 2   2nd   Male Child       No    0           2         1
## 3   3rd   Male Child       No   35           3         1
## 4  Crew   Male Child       No    0           4         1
## 5   1st Female Child       No    0           1         1
## 6   2nd Female Child       No    0           2         1

Each row represents a group of passengers who shared the same class, sex, age group, and survival outcome.

Survival by gender

This plot suggests that survival was much more common among female passengers than male passengers.

Relationship between gender and survival

A chi square test can check whether gender and survival appear to be independent.

\[ H_0: \text{Gender and survival are independent} \]

\[ H_a: \text{Gender and survival are not independent} \]

The null hypothesis says that survival rates do not depend on gender.

R code for the chi square test

titanic_data = as.data.frame(Titanic)
sex_table = xtabs(Freq ~ Sex + Survived, data = titanic_data)
chisq.test(sex_table)

The xtabs() function creates a contingency table, and chisq.test() tests whether the two variables are independent.

Test result

## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  sex_table
## X-squared = 454.5, df = 1, p-value < 2.2e-16

The p-value is very small, so there is strong evidence that gender and survival were not independent.

Understanding the p-value

A p value measures how surprising the observed data would be if the null hypothesis were true.

\[ p = P(\text{results at least this extreme} \mid H_0 \text{ is true}) \]

A very small p value shows evidence against the null hypothesis.

Survival by passenger class

This plot suggests that passengers in higher classes generally had better survival rates.

Chi square statistic

The chi square test statistic compares observed counts to expected counts.

\[ \chi^2 = \sum \frac{(O - E)^2}{E} \]

Here, \(O\) represents an observed count and \(E\) represents an expected count under the assumption of independence.

Class and survival test

## 
##  Pearson's Chi-squared test
## 
## data:  class_table
## X-squared = 190.4, df = 3, p-value < 2.2e-16

The p value is very small, so there is strong evidence that passenger class and survival were not independent.

Plotly visualization

This Plotly chart displays survival counts by passenger class.

Main takeaways

  • The Titanic data is a good example of categorical data analysis.
  • Gender had a strong relationship with survival.
  • Passenger class also appeared to influence survival outcomes.
  • Chi square tests can be used to test whether categorical variables are independent.