-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
207 lines (207 loc) · 7.81 KB
/
.Rhistory
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#Load data
df.train <- read.csv('titanic_train.csv')
head(df.train)
head(df.train)
#Exploration
ggplot(df.train, aes(Survived)) + geom_bar()
#Libraries
library(ggplot2)
#Exploration
ggplot(df.train, aes(Survived)) + geom_bar()
?xlim
#Exploration
ggplot(df.train, aes(Survived)) + geom_bar()
gglot(df.train, aes(Pclass)) + geom_bar(aes)fill = factor(Pclass))
gglot(df.train, aes(Pclass)) + geom_bar(aes(fill = factor(Pclass)))
ggplot(df.train, aes(Pclass)) + geom_bar(aes(fill = factor(Pclass)))
ggplot(df.train, aes(Pclass)) + geom_bar(aes(fill = factor(sex)))
ggplot(df.train, aes(Pclass)) + geom_bar(aes(fill = factor(Sex)))
ggplot(df.train, aes(Sex)) + geom_bar(aes(fill = factor(Sex)))
ggplot(df.train, aes(Sex)) + geom_bar(aes(fill = factor(Sex))) + ylab = 'Gender'
ggplot(df.train, aes(Sex)) + geom_bar(aes(fill = factor(Sex) + ylab = 'Gender')
ggplot(df.train, aes(Sex)) + geom_bar(aes(fill = factor(Sex))
ggplot(sf.train, aes(Age)) + geom_histogram(bins = 20, alpha = 0.5, fill = 'blue')
ggplot(df.train, aes(Age)) + geom_histogram(bins = 20, alpha = 0.5, fill = 'blue')
ggplot(df.train, aes(SibSp)) + geom_bar()
ggplot(df.train, aes(Fare)) + geom_histogram()
ggplot(df.train, aes(Fare)) + geom_histogram(fill = 'green', color = 'black', alpha = 0.5)
ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2))
ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
title(main = 'Variance of Age by Class', xlab = 'Passgenger Class')
ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
title(main = 'Variance of Age by Class')
?title
ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
Title(main = "Variance of Age by Class")
ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
title(main = "Variance of Age by Class")
pl <- ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2))
pl <- ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2))
pl
pl + title(main = "Variance of Age by Class")
pl <- ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
ggtitle("Variance of Age by Class") +
ylab("Passenger Class")
pl
pl <- ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
ggtitle("Variance of Age by Class") +
xlab("Passenger Class")
pl
pl <- ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
ggtitle("Variance of Age by Class") +
xlab("Passenger Class") +
theme_bw()
pl
library(Amelia)
install.packages("Amelia")
library(Amelia)
#Imputation of (average) age by class
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
impute_age <- function(age,class){
out <- age
for (i in 1:length(age)){
if (is.na(age[i])){
if (class[i] == 1){
out[i] <- 37
}else if (class[i] == 2){
out[i] <- 29
}else{
out[i] <- 24
}
}else{
out[i]<-age[i]
}
}
return(out)
}
fixed.ages <- impute_age(df.train$Age, df.train$Pclass)
missmap(df.train, main = 'Imputation Check', col = c('yellow', 'black'), legend = F)
print(missmap(df.train, main = 'Imputation Check', col = c('yellow', 'black'), legend = F))
print(missmap(df.train, main = 'Imputation Check', col = c('yellow', 'black'), legend = F)))
print(missmap(df.train, main = 'Imputation Check', col = c('yellow', 'black'), legend = FALSE)))
print(missmap(df.train, main = 'Imputation Check', col = c('yellow', 'black'), legend = FALSE))
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
print(missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE))
fixed.ages <- impute_age(df.train$Age, df.train$Pclass)
print(missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE))
any(is.na(df.train$Age))
#Imputation of (average) age by classes 1, 2 and 3
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
#Imputation of (average) age by classes 1, 2 and 3
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
impute_age <- function(age,class){
out <- age
for (i in 1:length(age)){
if (is.na(age[i])){
if (class[i] == 1){
out[i] <- 37
}else if (class[i] == 2){
out[i] <- 29
}else{
out[i] <- 24
}
}else{
out[i]<-age[i]
}
}
return(out)
}
fixed.ages <- impute_age(df.train$Age, df.train$Pclass)
print(missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE))
print(missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE))
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
print(missmap(df.train, main="Titanic Training Data: Missings Map",
col=c("yellow", "black"), legend=FALSE))
ggplot(df.train,aes(Pclass, Age)) +
geom_boxplot(aes(group = Pclass, fill = factor(Pclass), alpha = 0.4)) +
scale_y_continuous(breaks =seq(min(0), max(80), by = 2)) +
ggtitle("Variance of Age by Class") +
xlab("Passenger Class") +
theme_bw()
#Imputation of (average) age by classes 1, 2 and 3
missmap(df.train, main="Titanic Training Data: Missings Map",
col=c("yellow", "black"), legend=FALSE)
impute_age <- function(age,class){
out <- age
for (i in 1:length(age)){
if (is.na(age[i])){
if (class[i] == 1){
out[i] <- 37
}else if (class[i] == 2){
out[i] <- 29
}else{
out[i] <- 24
}
}else{
out[i]<-age[i]
}
}
return(out)
}
fixed.ages <- impute_age(df.train$Age, df.train$Pclass)
df.train$Age <- fixed.ages
df.train$Age <- fixed.ages
print(missmap(df.train, main="Titanic Training Data: Missings Map",
col=c("yellow", "black"), legend=FALSE))
df.train <- select(df.train,-PassengerId,-Name,-Ticket,-Cabin)
df.train$Survived <- factor(df.train$Survived)
df.train$Pclass <- factor(df.train$Pclass)
df.train$Parch <- factor(df.train$Parch)
df.train$SibSp <- factor(df.train$SibSp)
library(dplyr)
df.train <- select(df.train,-PassengerId,-Name,-Ticket,-Cabin)
df.train$Survived <- factor(df.train$Survived)
df.train$Pclass <- factor(df.train$Pclass)
df.train$Parch <- factor(df.train$Parch)
df.train$SibSp <- factor(df.train$SibSp)
log.model <- glm(formula = Survived ~ . , family = binomial(link='logit'),data = df.train)
summary(log.model)
library(caTools)
install.packages("caTools")
library(caTools)
set.seed(101)
split <- sample.split(df.train$Survived, SplitRatio = 0.7)
final.train = subset(df.train, split == TRUE)
final.test = subset(df.train, split == FALSE)
final.log.model <- glm(Survived ~ . , family = binomial(link = 'logit'), data = final.train)
summary(final.log.model)
Accuracy <- 1 - Error
fitted.probabilities <- predict(final.log.model, final.test, type = 'reponse')
fitted.probabilities <- predict(final.log.model, final.test, type = 'response')
fitted.results <- ifelse(fitted.probabilities > 0.5, 1, 0)
#Metrics
Error <- mean(fitted.results != final.test$Survived)
Error
Accuracy <- 1 - Error
Accuracy