#Homework 1: TitanicMachine Learning through Disaster
Setup
getwd()
## [1] "/Users/jakegreenstein/Documents/Grad School/BC/ADEC7301_Data Analysis/Week 1/HW 1"
list.files()
## [1] "HW1_Titanic.html" "HW1_Titanic.R" "HW1_Titanic.Rmd" "train.csv"
mydata <- read.csv("train.csv")
library(psych)
## Warning: package 'psych' was built under R version 4.5.2
Q1A: What are the types of variable & Levels of Measurement for PassengerId & Age?
str(mydata)
## 'data.frame': 891 obs. of 12 variables:
## $ PassengerId: int 1 2 3 4 5 6 7 8 9 10 ...
## $ Survived : int 0 1 1 1 0 0 0 0 1 1 ...
## $ Pclass : int 3 1 3 1 3 3 1 3 3 2 ...
## $ Name : chr "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ...
## $ Sex : chr "male" "female" "female" "female" ...
## $ Age : num 22 38 26 35 35 NA 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 ...
## $ Ticket : chr "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
## $ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
## $ Cabin : chr "" "C85" "" "C123" ...
## $ Embarked : chr "S" "C" "S" "S" ...
names(mydata)[sapply(mydata, is.numeric)]
## [1] "PassengerId" "Survived" "Pclass" "Age" "SibSp"
## [6] "Parch" "Fare"
names(mydata)[sapply(mydata, is.factor)]
## character(0)
names(mydata)[sapply(mydata, is.character)]
## [1] "Name" "Sex" "Ticket" "Cabin" "Embarked"
PassengerId: Even though it comes up as a numeric varialbe, it would be better classified as a qualitative variable since yes they are numbers, but the numbers act more of unique labels than a random string of numbers. The numbers themselves hold no significance or convey any relation to another Id. Since PassengerId are labels, the variable is Nominal
Age: This variable is quantitative & ordinal. The values themselves hold value and have a relation to other Age numbers. It’s ordinal since Age is an ever growing sequnce. Where it starts at 0 and can go up indefinitely.
Q1B: Which variable has the most missing observations?
sum(is.na(mydata))
## [1] 177
length((mydata$Age))
## [1] 891
sum(is.na(mydata$Age))
## [1] 177
sum(!is.na(mydata$Age))
## [1] 714
891-177
## [1] 714
Age is the only variable with missing values. 117 of the 891 cases are missing.
Q2 Impute missing observations for Age, SibSp, and Parch with the column median (ordinal, interval, or ratio), or the column mode.
mydataNoNA <- mydata
mydataNoNA$Age[is.na(mydataNoNA$Age)] <- median(mydataNoNA$Age, na.rm = TRUE)
psych::describe(mydata$Age)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 714 29.7 14.53 28 29.27 13.34 0.42 80 79.58 0.39 0.16 0.54
sum(is.na(mydataNoNA$Age))
## [1] 0
From Q1B we can ignore SibSp & Parch since neither came up with missing cases. So we only have to adjust Age.
I created a duplicate of the original data set to make sure not to overwrite the original, called mydataNoNA.
Then I used the had the Age column take out the NA values and replace it with the Age Column’s median value (28).
Q3 Install the psych package. Invoke the package using library(psych). Then provide descriptive statistics for Age, SibSp, and Parch. Please comment on what you observe from the summary statistics.
describe(mydataNoNA$Age)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 891 29.36 13.02 28 28.83 8.9 0.42 80 79.58 0.51 0.97 0.44
describe(mydataNoNA$SibSp)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 891 0.52 1.1 0 0.27 0 0 8 8 3.68 17.73 0.04
describe(mydataNoNA$Parch)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 891 0.38 0.81 0 0.18 0 0 6 6 2.74 9.69 0.03
The typical age of the passengers was 28, 29. With a standard deviation of 13.02 years (15 : 41). Most of the passangers were adults with the oldest passenger at 80 years old adn the youngest was only a few months old.
Since the variable names SibSp & Parch on their own aren’t clear on what they represen, I went back to the Kraggle dataset page and found the following:
sibsp: # of siblings / spouses aboard the Titanic
Sibling = brother, sister, stepbrother, stepsister
Spouse = husband, wife (mistresses and fiancés were ignored)
parch: # of parents / children aboard the Titanic
Parent = mother, father
Child = daughter, son, stepdaughter, stepson
Some children travelled only with a nanny, therefore parch=0 for them.
With that context, we can take away that the passengers where mostly the only of their family’s generation on the ship since both the mean and median for SibSp was respectively 0 & 0.52. And with a SD of 1.1 makes us think that accounts for the Spouse component.
For Parch, its important to keep Age in mind when considering the descriptive stats. We’ve already established that bulk of the passangers’ age is about 28, 29 so it makes sense that the number of accompanying kids and/or parents would be low. As seen with a mean of 0.38, median of 0 & a SD of 0.81. Telling us that the makeup of the passengers were solo adult travelers or couples.
Both SibSp & Parch’s skews are 3.68 & 2.74, positive and skewing to the right. which makes sense since the vaues are centered near 0 and can only go up.
Q4: Provide a cross-tabulation of Survived and Sex
table(mydataNoNA$Survived, mydataNoNA$Sex)
##
## female male
## 0 81 468
## 1 233 109
With the given info:
Survival: 0 = No, 1 = Yes
We can clearly see that far more females survived the Titanic then their fellow male passengers.
sum(mydataNoNA$Sex== "female")
## [1] 314
SurvivalRateFemale <-sum(mydataNoNA$Sex == 'female' & mydataNoNA$Survived == 1) / sum(mydataNoNA$Sex == 'female')
sum(mydataNoNA$Sex== 'male')
## [1] 577
SurvivalRateMale <-sum(mydataNoNA$Sex == 'male' & mydataNoNA$Survived == 1) / sum(mydataNoNA$Sex == 'male')
SurvivalRateFemale
## [1] 0.7420382
SurvivalRateMale
## [1] 0.1889081
For Females: Out of the 314, 233 survived and only 88 perished.
For Males: Out of the 577, just 109 survived while 468 didn’t survive.
Females had about a 74.2% survival rate and the males had a 18.9% survival rate. This lines up with what is commonly known about the evacuation proceedings of the Titanic. Lifeboats were prioritized for women and children while the majority of the men had to endure the freezing waters.
Q5 Provide notched boxplots for Survived and Age. What do you notice?
boxplot(mydataNoNA$Age~mydataNoNA$Survived, notch=TRUE, horizontal = T)
table(mydataNoNA$Survived, mydataNoNA$Age)
##
## 0.42 0.67 0.75 0.83 0.92 1 2 3 4 5 6 7 8 9 10 11 12
## 0 0 0 0 0 0 2 7 1 3 0 1 2 2 6 2 3 0
## 1 1 1 2 2 1 5 3 5 7 4 2 1 2 2 0 1 1
##
## 13 14 14.5 15 16 17 18 19 20 20.5 21 22 23 23.5 24 24.5 25 26
## 0 0 3 1 1 11 7 17 16 12 1 19 16 10 1 15 1 17 12
## 1 2 3 0 4 6 6 9 9 3 0 5 11 5 0 15 0 6 6
##
## 27 28 28.5 29 30 30.5 31 32 32.5 33 34 34.5 35 36 36.5 37 38
## 0 7 143 2 12 15 2 9 9 1 9 9 1 7 11 1 5 6
## 1 11 59 0 8 10 0 8 9 1 6 6 0 11 11 0 1 5
##
## 39 40 40.5 41 42 43 44 45 45.5 46 47 48 49 50 51 52 53 54
## 0 9 7 2 4 7 4 6 7 2 3 8 3 2 5 5 3 0 5
## 1 5 6 0 2 6 1 3 5 0 0 1 6 4 5 2 3 1 3
##
## 55 55.5 56 57 58 59 60 61 62 63 64 65 66 70 70.5 71 74 80
## 0 1 1 2 2 2 2 2 3 2 0 2 3 1 2 1 2 1 0
## 1 1 0 2 0 3 0 2 0 2 2 0 0 0 0 0 0 0 1
These show that age in terms of survival or not, infact share a similar distribution. Both of the groups roughly have the same median (28/29 years) and the middle 50% of the passengers are clustered from early 20-somethings, up to the mid/late 30 year olds.
The survivor group’s IRQ does extend slightly wider on the younger side.
Both groups contain some outliers , but the non-survivor group holds the majority of them. There are a few on the very young side, but the bulk of them are in the 50’s and up range. Perhaps the number of elderly deaths is from the fact that they would have had a harder time maneuvering the chaos of reaching a lifeboat, but further analysis would be needed to make that claim.
With such a similar distribution, age alone doesn’t seem to be a strong indicator on whether or not a passenger survived. So far, sex has given much more insight on survival rate.