-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data Exploration, Visualization and Feature Engineering.R
193 lines (151 loc) · 5.19 KB
/
Data Exploration, Visualization and Feature Engineering.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Read train and test files
train <- read.csv("C:/Users/ADitya/Desktop/job/titanic dataset kaggle/train.csv", na.strings = "")
test <- read.csv("C:/Users/ADitya/Desktop/job/titanic dataset kaggle/test.csv", na.strings = "")
# Creating survived in test
test$Survived <- NA
# Merging as a single df
full <- rbind(train, test)
# summary ( mean,median, NA's)
summary(full)
# Missing value by varibles
sapply(full, function(x) sum(is.na(x)))
# Structure (Class)
str(full)
# Creating survived and Pclass as factor
full$Survived <- as.factor(full$Survived)
full$Pclass <- as.factor(full$Pclass)
# Checking out Missing value of age by pclass : class 3 has max age missing values
aggregate(Age ~ Pclass, data=full, function(x) {sum(is.na(x))}, na.action = NULL)
#summmary of Pclass : for comparing missing values to total age values by Pclass
summary(full$Pclass)
# Missing values in Embarked are only 2, replacing them by mode
full$Embarked[is.na(full$Embarked)] <- "S"
# Visualization
library(ggplot2)
library(ggthemes)
# Age vs Survived
ggplot(full[1:891,], aes(Age, fill = factor(Survived))) +
geom_histogram(bins=30) +
theme_few() +
xlab("Age") +
scale_fill_discrete(name = "Survived") +
ggtitle("Age vs Survived")
# Sex vs Survived
ggplot(full[1:891,], aes(Sex, fill = factor(Survived))) +
geom_bar(stat = "count", position = 'dodge')+
theme_few() +
xlab("Sex") +
ylab("Count") +
scale_fill_discrete(name = "Survived") +
ggtitle("Sex vs Survived")
#Sex vs Survived vs Age
ggplot(full[1:891,], aes(Age, fill = factor(Survived))) +
geom_histogram(bins=30) +
theme_few() +
xlab("Age") +
ylab("Count") +
facet_grid(.~Sex)+
scale_fill_discrete(name = "Survived") +
theme_few()+
ggtitle("Age vs Sex vs Survived")
# Pclass vs Survived
ggplot(full[1:891,], aes(Pclass, fill = factor(Survived))) +
geom_bar(stat = "count")+
theme_few() +
xlab("Pclass") +
facet_grid(.~Sex)+
ylab("Count") +
scale_fill_discrete(name = "Survived") +
ggtitle("Pclass vs Sex vs Survived")
# Extracting the title from name using gsub
full$Title <- gsub("(.*, )|(\\..*)", "", full$Name)
# Otherwise it can be done as...
# full$Title <- gsub(".*, ", "", full$Name)
# full$Title <- gsub(". .*", "", full$Title)
# str of title
str(full$Title)
# table of titles
table(full$Title)
# Titles by Sex
table(full$Sex, full$Title)
# Reassign rare titles
Officer <- c('Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev')
Royalty <- c('Dona', 'Lady', 'the Countess','Sir', 'Jonkheer')
# Reassign mlle, ms, and mme, and rare
full$Title[full$Title == 'Mlle'] <- 'Miss'
full$Title[full$Title == 'Ms'] <- 'Miss'
full$Title[full$Title == 'Mme'] <- 'Mrs'
full$Title[full$Title %in% Royalty] <- 'Royalty'
full$Title[full$Title %in% Officer] <- 'Officer'
# Selecting the surname
full$Surname <- gsub(",.*","",full$Name)
# Family size including the person aboard
full$Fsize <- full$SibSp + full$Parch + 1
# family size vs survived
ggplot(full[1:891,], aes(x = Fsize, fill = factor(Survived))) +
geom_bar(stat='count', position='dodge') +
scale_x_continuous(breaks=c(1:11)) +
xlab('Family Size') +
ylab("Count") +
theme_few()+
scale_fill_discrete(name = "Survived") +
ggtitle("Family Size vs Survived")
# Assigning family size from numbers to type
full$Fsize[full$Fsize > 4] <- "Big"
full$Fsize[full$Fsize == 1] <- "Alone"
full$Fsize[full$Fsize < 5 & full$Fsize > 1] <- "Small"
# log transformation of Fair
full$Fare <- log(full$Fare + .01)
# one NA value in Fare
full$Fare[is.na(full$Fare)] <- 2
# Creating Fare Range
full$FareRange <- ifelse(full$Fare > 3.344, "High",
ifelse(full$Fare > 2.851, "Med-High",
ifelse(full$Fare > 2.068, "Med", "Low"
)))
#graph title vs survived
ggplot(full[1:891,], aes(Title,fill = factor(Survived))) +
geom_bar(stat = "count")+
xlab('Title') +
ylab("Count") +
scale_fill_discrete(name = " Survived") +
ggtitle("Title vs Survived")+
theme_few()
# Embarked vs Pclass vs Survived
ggplot(full[1:891,], aes(Pclass, fill = factor(Survived))) +
geom_bar(stat = "count")+
theme_few() +
xlab("Pclass") +
ylab("Count") +
facet_wrap(~Embarked) +
scale_fill_discrete(name = "Survived") +
ggtitle("Embarked vs Pclass vs Survived")
# Missing value imputation by kNN
library(VIM)
fullclone <- kNN(full)
full$Age <- fullclone$Age
# new variable child and adults
full$Child[full$Age < 15] <- 'Child'
full$Child[full$Age >= 15] <- 'Adult'
# remember this code
#Child vs Sex vs Pclass vs Survived
ggplot(full[1:891,][full[1:891,]$Child == 'Child', ], aes(Sex, fill = factor(Survived))) +
geom_bar(stat = "count") +
xlab("Sex") +
ylab("Count") +
facet_wrap(~Pclass)+
scale_fill_discrete(name = "Survived") +
ggtitle("Child vs Sex vs Pclass vs Survived")+
theme_few()
# Factorization
str(full)
full$Child <- factor(full$Child)
full$Title <- factor(full$Title)
full$Surname <- factor(full$Surname)
full$Fsize <- factor(full$Fsize)
full$FareRange <- factor(full$FareRange)
# Split data again
train <- full[1:891, c(-4, -9, -11)]
test <- full[892:1309, c(-4, -9, -11)]
# Deleting survived from test
test$Survived <- NULL