Association Rule Analysis on Titanic Survivor Data

Get the titanic data from: http://www.rdatamining.com/data/titanic.raw.rdata?attredirects=0&d=1.

The sinking of the RMS Titanic is one of the most infamous shipwrecks in history. On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew. This sensational tragedy shocked the international community and led to better safety regulations for ships.

One of the reasons that the shipwreck led to such loss of life was that there were not enough lifeboats for the passengers and crew. Although there was some element of luck involved in surviving the sinking, some groups of people were more likely to survive than others, such as women, children, and the upper-class.

In this challenge, we ask you to complete the analysis of what sorts of people were likely to survive. In particular, we ask you to apply the tools of machine learning to predict which passengers survived the tragedy.

# install.packages("titanic")
# library(titanic)
library(vcd) # for mosaic plot
## Warning: package 'vcd' was built under R version 3.3.3
## Loading required package: grid
mosaic(Titanic, gp = shading_hcl, gp_args = list(interpolate = c(1, 1.8)))

# Femal survived rate
apply(Titanic, c(2, 4), sum)
##         Survived
## Sex        No Yes
##   Male   1364 367
##   Female  126 344
load("titanic.raw.rdata")
df <- titanic.raw
require(arules)
## Loading required package: arules
## Warning: package 'arules' was built under R version 3.3.3
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
rule <- apriori(titanic.raw, 
                # min support & confidence
                parameter=list(minlen=3, supp=0.1, conf=0.7),  
                appearance = list(default="lhs", rhs=c("Survived=No","Survived=Yes")))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.7    0.1    1 none FALSE            TRUE       5     0.1      3
##  maxlen target   ext
##      10  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 220 
## 
## set item appearances ...[2 item(s)] done [0.00s].
## set transactions ...[10 item(s), 2201 transaction(s)] done [0.00s].
## sorting and recoding items ... [9 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [8 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(rule)
##     lhs                                rhs            support   confidence
## [1] {Sex=Female,Age=Adult}          => {Survived=Yes} 0.1435711 0.7435294 
## [2] {Class=3rd,Sex=Male}            => {Survived=No}  0.1917310 0.8274510 
## [3] {Class=3rd,Age=Adult}           => {Survived=No}  0.2162653 0.7591707 
## [4] {Class=Crew,Sex=Male}           => {Survived=No}  0.3044071 0.7772622 
## [5] {Class=Crew,Age=Adult}          => {Survived=No}  0.3057701 0.7604520 
## [6] {Sex=Male,Age=Adult}            => {Survived=No}  0.6038164 0.7972406 
## [7] {Class=3rd,Sex=Male,Age=Adult}  => {Survived=No}  0.1758292 0.8376623 
## [8] {Class=Crew,Sex=Male,Age=Adult} => {Survived=No}  0.3044071 0.7772622 
##     lift    
## [1] 2.301699
## [2] 1.222295
## [3] 1.121433
## [4] 1.148157
## [5] 1.123325
## [6] 1.177669
## [7] 1.237379
## [8] 1.148157
# Arrange rules according to Lift:

sort.rule <- sort(rule, by="lift")
inspect(sort.rule)
##     lhs                                rhs            support   confidence
## [1] {Sex=Female,Age=Adult}          => {Survived=Yes} 0.1435711 0.7435294 
## [2] {Class=3rd,Sex=Male,Age=Adult}  => {Survived=No}  0.1758292 0.8376623 
## [3] {Class=3rd,Sex=Male}            => {Survived=No}  0.1917310 0.8274510 
## [4] {Sex=Male,Age=Adult}            => {Survived=No}  0.6038164 0.7972406 
## [5] {Class=Crew,Sex=Male}           => {Survived=No}  0.3044071 0.7772622 
## [6] {Class=Crew,Sex=Male,Age=Adult} => {Survived=No}  0.3044071 0.7772622 
## [7] {Class=Crew,Age=Adult}          => {Survived=No}  0.3057701 0.7604520 
## [8] {Class=3rd,Age=Adult}           => {Survived=No}  0.2162653 0.7591707 
##     lift    
## [1] 2.301699
## [2] 1.237379
## [3] 1.222295
## [4] 1.177669
## [5] 1.148157
## [6] 1.148157
## [7] 1.123325
## [8] 1.121433
# Visulization

require(arulesViz)
## Loading required package: arulesViz
## Warning: package 'arulesViz' was built under R version 3.3.3
plot(sort.rule)

plot(sort.rule, method="graph",
     control=list(nodeCol="red", edgeCol="blue", type="items"))

plot(sort.rule, method="grouped", control=list(col=4))

A parallel coordinates plot rules

The width of the arrows represents support

The intensity of the color represent confidence

subrules2 <- head(sort(rule, by="lift"), 2)
plot(subrules2, method="paracoord", control = list(col=4,main="Survival Rule on Titanic"))

itemsets <- eclat(titanic.raw, parameter = list(support = 0.05, minlen=2))
## Eclat
## 
## parameter specification:
##  tidLists support minlen maxlen            target   ext
##     FALSE    0.05      2     10 frequent itemsets FALSE
## 
## algorithmic control:
##  sparse sort verbose
##       7   -2    TRUE
## 
## Absolute minimum support count: 110 
## 
## create itemset ... 
## set transactions ...[10 item(s), 2201 transaction(s)] done [0.00s].
## sorting and recoding items ... [9 item(s)] done [0.00s].
## creating bit matrix ... [9 row(s), 2201 column(s)] done [0.00s].
## writing  ... [54 set(s)] done [0.00s].
## Creating S4 object  ... done [0.00s].
plot(itemsets, method="graph")

subrules2 <- head(sort(rule, by="lift"), 2)

plot(subrules2, method="graph",
     control = list( alpha = 1,main = "Just 2 Rules"))

subrules2 <- head(sort(rule, by="lift"), 4)

plot(subrules2, method="graph",
     control = list( alpha = 1,main="Under 4 Rules"))

plot(subrules2, method="paracoord", control=list(alpha=0.8, col=rainbow(7)))