setwd(“C:/Users/Taiyyab Ali/Desktop/R language”)
#reading data
Titanic <-read.csv("Titanic Data.csv")
View(Titanic)
Total no. of passengers in titanic
length(Titanic$Survived)
## [1] 889
Total no. of passengers = 889
no.of passengers survived equal value under 1
table(Titanic$Survived)
##
## 0 1
## 549 340
no. of passenger survived= 340
prop.test(340,889)
##
## 1-sample proportions test with continuity correction
##
## data: 340 out of 889, null probability 0.5
## X-squared = 48.666, df = 1, p-value = 3.035e-12
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.3505253 0.4154084
## sample estimates:
## p
## 0.3824522
percentage of passengers survived = 38.24%
aggregate(Survived~Pclass,data=Titanic,sum)
## Pclass Survived
## 1 1 134
## 2 2 87
## 3 3 119
no.of 1st class passenger survived= 134 # sum of not survived is “0” for all class #task 3e
prop.test(134,889)
##
## 1-sample proportions test with continuity correction
##
## data: 134 out of 889, null probability 0.5
## X-squared = 432.4, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.1281932 0.1763462
## sample estimates:
## p
## 0.1507312
persentage of 1st class passenger survived = 15.07%
ftable(Titanic$Survived,Titanic$Pclass,Titanic$Sex)
## female male
##
## 0 1 3 77
## 2 6 91
## 3 72 300
## 1 1 89 45
## 2 70 17
## 3 72 47
no. female passenger from 1st class survived=89
table(Titanic$Survived,Titanic$Sex)
##
## female male
## 0 81 468
## 1 231 109
prop.test(231,340)
##
## 1-sample proportions test with continuity correction
##
## data: 231 out of 340, null probability 0.5
## X-squared = 43.062, df = 1, p-value = 5.304e-11
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.6265239 0.7281727
## sample estimates:
## p
## 0.6794118
persentage of female- survived passenger=67.94% :P quite high #task 3h
prop.test(231,312)
##
## 1-sample proportions test with continuity correction
##
## data: 231 out of 312, null probability 0.5
## X-squared = 71.157, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.6873432 0.7873876
## sample estimates:
## p
## 0.7403846
persentage of female passenger that survived = 74.04%
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.
library(gmodels) # for funtion crossTable
CrossTable(Titanic$Survived,Titanic$Sex,chisq = TRUE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | Chi-square contribution |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 889
##
##
## | Titanic$Sex
## Titanic$Survived | female | male | Row Total |
## -----------------|-----------|-----------|-----------|
## 0 | 81 | 468 | 549 |
## | 64.727 | 35.000 | |
## | 0.148 | 0.852 | 0.618 |
## | 0.260 | 0.811 | |
## | 0.091 | 0.526 | |
## -----------------|-----------|-----------|-----------|
## 1 | 231 | 109 | 340 |
## | 104.515 | 56.514 | |
## | 0.679 | 0.321 | 0.382 |
## | 0.740 | 0.189 | |
## | 0.260 | 0.123 | |
## -----------------|-----------|-----------|-----------|
## Column Total | 312 | 577 | 889 |
## | 0.351 | 0.649 | |
## -----------------|-----------|-----------|-----------|
##
##
## Statistics for All Table Factors
##
##
## Pearson's Chi-squared test
## ------------------------------------------------------------
## Chi^2 = 260.7563 d.f. = 1 p = 1.173958e-58
##
## Pearson's Chi-squared test with Yates' continuity correction
## ------------------------------------------------------------
## Chi^2 = 258.4266 d.f. = 1 p = 3.77991e-58
##
##
Based on p-value we can neglect the null hypothesis that female and male survived almost equally.