타이타닉 남,녀 주인공은 원래 그렇게 될 운명이었다.

인터넷에서 구한 타이타닉 승객 데이터를 이용해 몇 가지 분석을 해봤다.

사용데이터는 여기에서 받을 수 있다.

알고싶은 부분은 승객등급, 나이, 성별에 따른 생존률의 차이가 어떻게 나는지 인데…

타이타닉 남녀 주인공이 모두 승객 등급이 1,3 등급으로 등급의 격차가 컸다는 것을 염두에 두자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(data.table)
library(ggplot2)
 
titanic <- read.csv("http://dl.dropboxusercontent.com/u/8686172/titanic.csv")
 
#분석 편의상 data.table로 변환한다. 
titanic.dt <- as.data.table(titanic)
 
names(titanic.dt)
head(titanic.dt)
 
titanic.dt$survived <- as.factor(titanic.dt$survived)
 
#15세 미만을 아이로 본다. 
titanic.dt[,isminor:="adult"]
titanic.dt[age < 15,isminor:="child"]
titanic.dt$isminor <- as.factor(titanic.dt$isminor)
 
# 등급별 생존률 
titanic.dt[, length(which(survived == 1))/nrow(.SD), by=pclass]
 
# 성별 생존률
titanic.dt[, length(which(survived == 1))/nrow(.SD), by=sex]
 
#등급/성별 생존률 
survived_pclass_sex <- titanic.dt[, list(cntsurv=length(which(survived == 1)), cntdie=length(which(survived == 0))), 
           by=list(pclass, sex)][,list(psurvived=cntsurv/(cntsurv + cntdie)),by=list(pclass, sex)]
 
ggplot(survived_pclass_sex, aes(pclass, sex)) +
  geom_tile(aes(fill=psurvived)) + scale_fill_gradient2("생존률") + xlab("승객등급") + ylab("성별")
 
#성별, 등급, 나이별 생존률 
survived_pclass_sex_isminor<- titanic.dt[,list(cntsurv=length(which(survived == 1)), cntdie=length(which(survived == 0))),by=list(pclass, sex, isminor)][,list(psurvived=cntsurv/(cntsurv + cntdie)),by=list(pclass, sex, isminor)]
 
survived_pclass_sex_isminor$sex_age <- apply(survived_pclass_sex_isminor[,list(sex,isminor)], 1, paste, collapse="_")
 
library(scales)
ggplot(survived_pclass_sex_isminor, aes(pclass, sex_age)) +
  geom_tile(aes(fill=psurvived)) + scale_fill_gradient2("생존률", low=muted("white"), high=muted("blue")) + xlab("승객등급") + ylab("성별과 나이")

 

아래 플로팅은 마지막 플로팅 결과를 넣어본 것이다. 생존률에 대한 값을 discreate하게 만들어 주면 chi-square test를 통해 유의성 검정을 해볼 수도 있을 것이다.

image

 

여자주인공은 1등급 승객에다가 여자인데, 97%의 생존률을 보여준다. 반면에 남자주인공은 3등급 승객인데, 남자이며,  겨우 14%의 생존률을 보여준다.

그래프에서 보는 바와 같이, 등급이 높을 수록 색이 진한 경향이 있다는 것을 알 수 있으나, 성별이나 나이에 영향은 받는 측면도 있음을 알 수 있다.

이런 상호작용을 보기 위해 아래와 같은 conditional inference tree를 그려봤다.

여자의 경우 나이는 크게 영향을 미치지 못하고 등급에 따른 생존률이 차이가 컸다. 그러나 남자의 경우는 나이에 따른 생존률 영향이 있었는데, 그마저도 3등급의 경우 그다지 높지 않음을 알 수 있다.

 

 

image

 

타이타닉 영화가 해피엔딩으로 끝나게 만들고자 했다면 남자를 1등급 소년으로 할당하고 여자를 최소 2등급으로 할당했었어야 된다. 하지만 그렇게 되었다면 감동은 반감되고 스토리도 그다지 흥미롭지 않아 흥행에는 실패했을 것이다.

CC BY-NC 4.0 타이타닉 남,녀 주인공은 원래 그렇게 될 운명이었다. by from __future__ import dream is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.