File “TitanicData.csv” contains the Data of onboard passengers in Titanic accident. Storing the file in a dataframe –
titanic.df <- read.csv(paste("TitanicData.csv", sep=""))
##View(titanic.df)
##library(psych)
##describe(titanic.df)
Total number of Passengers onboard the titanic
dim(titanic.df)
## [1] 889 8
Total 889 passengers onboard the titanic
Passengers who survived the sinking of titanic
table(titanic.df$Survived)
##
## 0 1
## 549 340
340 passengers survived the sinking of titanic
In percentages–
prop.table(table(titanic.df$Survived))*100
##
## 0 1
## 61.75478 38.24522
38.24522 % passengers survived the sinking of Titanic
Total number of first-class passengers who survived the sinking of the Titanic
mytable <- xtabs(~ Survived+Pclass,data=titanic.df)
mytable
## Pclass
## Survived 1 2 3
## 0 80 97 372
## 1 134 87 119
134 first-class passengers survived the sinking of the Titanic
In percentages–
prop.table(mytable,2)*100
## Pclass
## Survived 1 2 3
## 0 37.38318 52.71739 75.76375
## 1 62.61682 47.28261 24.23625
Thus 62.61682 % of first-class passengers survived the sinking of titanic
Total number of females from First-Class who survived the sinking of the Titanic
ftable(xtabs(~ Survived+Pclass+Sex,data=titanic.df))
## 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
89 females from First-Class survived the sinking of the Titanic
Percentage of Survivors who were female
mytable <- xtabs(~ Survived+Sex,data=titanic.df)
addmargins(prop.table(mytable,1)*100,2)
## Sex
## Survived female male Sum
## 0 14.75410 85.24590 100.00000
## 1 67.94118 32.05882 100.00000
67.94118 % of survivors are female
Percentage of females on board the Titanic who survived
addmargins(prop.table(mytable,2)*100,1)
## Sex
## Survived female male
## 0 25.96154 81.10919
## 1 74.03846 18.89081
## Sum 100.00000 100.00000
74.03846 % of females survived the sinking of titanic
Pearson’s Chi-squared test for 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.
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 p-value < 0.01 we can reject the null hypothesis that Sex of the passengers is independent of those who survived the sinking of titanic. Thus our hypothesis was correct i.e. there is a strong relationship between the gender of the passenger and its chance of survival.