This is my RMarkdown document to analyze on the Titanic Case.

titanic.df <- read.csv(paste("TitanicData.csv", sep=""))
View(titanic.df)

Use R to count the total number of passengers on board the Titanic.

mytable <- with(titanic.df, table(Survived))  
mytable  
## Survived
##   0   1 
## 549 340
addmargins(mytable)  
## Survived
##   0   1 Sum 
## 549 340 889

Use R to count the number of passengers who survived the sinking of the Titanic.

mytable <- with(titanic.df, table(Survived))  
mytable
## Survived
##   0   1 
## 549 340

Use R to measure the percentage of passengers who survived the sinking of the Titanic.

prop.table(mytable)*100   
## Survived
##        0        1 
## 61.75478 38.24522

Use R to count the number of first-class passengers who survived the sinking of the Titanic.

mytable <- xtabs(~ Pclass+Survived, data=titanic.df)
mytable  
##       Survived
## Pclass   0   1
##      1  80 134
##      2  97  87
##      3 372 119

Use R to measure the percentage of first-class passengers who survived the sinking of the Titanic.

margin.table(mytable,1)   
## Pclass
##   1   2   3 
## 214 184 491
prop.table(mytable, 1)*100  
##       Survived
## Pclass        0        1
##      1 37.38318 62.61682
##      2 52.71739 47.28261
##      3 75.76375 24.23625

Use R to count the number of females from First-Class who survived the sinking of the Titanic

mytable <- xtabs(~ Pclass+Sex+Survived, data=titanic.df)  
mytable  
## , , Survived = 0
## 
##       Sex
## Pclass female male
##      1      3   77
##      2      6   91
##      3     72  300
## 
## , , Survived = 1
## 
##       Sex
## Pclass female male
##      1     89   45
##      2     70   17
##      3     72   47

Use R to measure the percentage of survivors who were female

mytable <- xtabs(~ Survived+Sex, data=titanic.df)  
mytable  
##         Sex
## Survived female male
##        0     81  468
##        1    231  109
margin.table(mytable,1)   
## Survived
##   0   1 
## 549 340
prop.table(mytable, 1)*100
##         Sex
## Survived   female     male
##        0 14.75410 85.24590
##        1 67.94118 32.05882

Use R to measure the percentage of females on board the Titanic who survived

mytable <- xtabs(~ Sex+Survived, data=titanic.df)  
mytable  
##         Survived
## Sex        0   1
##   female  81 231
##   male   468 109
margin.table(mytable,1)   
## Sex
## female   male 
##    312    577
prop.table(mytable, 1)*100
##         Survived
## Sex             0        1
##   female 25.96154 74.03846
##   male   81.10919 18.89081

Run a Pearson’s Chi-squared test to test the following hypothesis:

Hypothesis: The proportion of females onboard who survived the sinking of the Titanic was higher than the proportion of males onboard who survived the sinking of the Titanic.

mytable <- xtabs(~Sex+Survived, data=titanic.df)    
addmargins(mytable)  
##         Survived
## Sex        0   1 Sum
##   female  81 231 312
##   male   468 109 577
##   Sum    549 340 889
chisq.test(mytable)  
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mytable
## X-squared = 258.43, df = 1, p-value < 2.2e-16

Since the probability is small (p < 0.01), we reject the Null hypothesis.