TITANIC SURVIVAL ANALYSIS

1 Read the Titanic dataset and save it inside a data frame called “titanic”

titanic <- read.csv(paste("Titanic Data.csv", sep=""))

2 Summary Statistics of the data

summary(titanic)
##     Survived          Pclass          Sex           Age       
##  Min.   :0.0000   Min.   :1.000   female:312   Min.   : 0.40  
##  1st Qu.:0.0000   1st Qu.:2.000   male  :577   1st Qu.:22.00  
##  Median :0.0000   Median :3.000                Median :29.70  
##  Mean   :0.3825   Mean   :2.312                Mean   :29.65  
##  3rd Qu.:1.0000   3rd Qu.:3.000                3rd Qu.:35.00  
##  Max.   :1.0000   Max.   :3.000                Max.   :80.00  
##      SibSp            Parch             Fare         Embarked
##  Min.   :0.0000   Min.   :0.0000   Min.   :  0.000   C:168   
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:  7.896   Q: 77   
##  Median :0.0000   Median :0.0000   Median : 14.454   S:644   
##  Mean   :0.5242   Mean   :0.3825   Mean   : 32.097           
##  3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.: 31.000           
##  Max.   :8.0000   Max.   :6.0000   Max.   :512.329
library(psych)
## Warning: package 'psych' was built under R version 3.4.3
describe(titanic)
##           vars   n  mean    sd median trimmed   mad min    max  range
## Survived     1 889  0.38  0.49   0.00    0.35  0.00 0.0   1.00   1.00
## Pclass       2 889  2.31  0.83   3.00    2.39  0.00 1.0   3.00   2.00
## Sex*         3 889  1.65  0.48   2.00    1.69  0.00 1.0   2.00   1.00
## Age          4 889 29.65 12.97  29.70   29.22  9.34 0.4  80.00  79.60
## SibSp        5 889  0.52  1.10   0.00    0.27  0.00 0.0   8.00   8.00
## Parch        6 889  0.38  0.81   0.00    0.19  0.00 0.0   6.00   6.00
## Fare         7 889 32.10 49.70  14.45   21.28 10.24 0.0 512.33 512.33
## Embarked*    8 889  2.54  0.79   3.00    2.67  0.00 1.0   3.00   2.00
##            skew kurtosis   se
## Survived   0.48    -1.77 0.02
## Pclass    -0.63    -1.27 0.03
## Sex*      -0.62    -1.61 0.02
## Age        0.43     0.96 0.43
## SibSp      3.68    17.69 0.04
## Parch      2.74     9.66 0.03
## Fare       4.79    33.23 1.67
## Embarked* -1.26    -0.23 0.03
attach(titanic)

str(titanic)
## 'data.frame':    889 obs. of  8 variables:
##  $ Survived: int  0 1 1 1 0 0 0 0 1 1 ...
##  $ Pclass  : int  3 1 3 1 3 3 1 3 3 2 ...
##  $ Sex     : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
##  $ Age     : num  22 38 26 35 35 29.7 54 2 27 14 ...
##  $ SibSp   : int  1 1 0 1 0 0 0 3 0 1 ...
##  $ Parch   : int  0 0 0 0 0 0 0 1 2 0 ...
##  $ Fare    : num  7.25 71.28 7.92 53.1 8.05 ...
##  $ Embarked: Factor w/ 3 levels "C","Q","S": 3 1 3 3 3 2 3 3 3 1 ...

3 ANALYZE WHO AND HOW MANY SURVIVED

3a. Total Number of Passengers

dim(titanic)
## [1] 889   8

3b. Number of Passengers who survived

survivedTable <- table(titanic$Survived)
survivedTable
## 
##   0   1 
## 549 340

3c. Percentage of Passengers who surivied

3c. Alternate soluton

100*prop.table(survivedTable) # proportions
## 
##        0        1 
## 61.75478 38.24522
summary(titanic$Survived)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.0000  0.0000  0.3825  1.0000  1.0000

3d. Number of 1st Class Passengers Who Survived?

surviversByClass <- xtabs(~ Survived+Pclass, data=titanic)
surviversByClass # frequencies
##         Pclass
## Survived   1   2   3
##        0  80  97 372
##        1 134  87 119
addmargins(surviversByClass)
##         Pclass
## Survived   1   2   3 Sum
##      0    80  97 372 549
##      1   134  87 119 340
##      Sum 214 184 491 889

3e. Percentage of 1st Class Passengers Who Survived?

prop.table(surviversByClass, 2) # column proportions
##         Pclass
## Survived         1         2         3
##        0 0.3738318 0.5271739 0.7576375
##        1 0.6261682 0.4728261 0.2423625

3f. Number of Females from 1st Class who survived

myt <- xtabs(~ Survived+Pclass+Sex, data=titanic)
addmargins(myt)
## , , Sex = female
## 
##         Pclass
## Survived   1   2   3 Sum
##      0     3   6  72  81
##      1    89  70  72 231
##      Sum  92  76 144 312
## 
## , , Sex = male
## 
##         Pclass
## Survived   1   2   3 Sum
##      0    77  91 300 468
##      1    45  17  47 109
##      Sum 122 108 347 577
## 
## , , Sex = Sum
## 
##         Pclass
## Survived   1   2   3 Sum
##      0    80  97 372 549
##      1   134  87 119 340
##      Sum 214 184 491 889
ftable(myt) 
##                 Sex female male
## Survived Pclass                
## 0        1               3   77
##          2               6   91
##          3              72  300
## 1        1              89   45
##          2              70   17
##          3              72   47
surviversBySex <- xtabs(~ Survived+Sex, data=titanic)
surviversBySex # frequencies
##         Sex
## Survived female male
##        0     81  468
##        1    231  109
addmargins(surviversBySex)
##         Sex
## Survived female male Sum
##      0       81  468 549
##      1      231  109 340
##      Sum    312  577 889

3g. Percentage of Surivers who were Female

prop.table(surviversBySex,1)
##         Sex
## Survived    female      male
##        0 0.1475410 0.8524590
##        1 0.6794118 0.3205882

3h. Percentage of total females on the Titanic who survived

prop.table(surviversBySex,2)
##         Sex
## Survived    female      male
##        0 0.2596154 0.8110919
##        1 0.7403846 0.1889081

3i. Chi Square Test : percentage of female survivers was higher than percentage of male survivers

chisq.test(surviversBySex)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  surviversBySex
## X-squared = 258.43, df = 1, p-value < 2.2e-16