train <- read.csv("~/Desktop/Kaggle/Titanic/train (3).csv")
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)
## 
## Attaching package: 'purrr'
## The following object is masked from 'package:dplyr':
## 
##     order_by
train$Survived.lgl<- map_lgl(train$Survived, function(x) {x == 1})
train$Outcome[train$Survived.lgl == TRUE] <- "Survived"
train$Outcome[train$Survived.lgl == FALSE] <- "Died"

train$Embarked2[train$Embarked == "C"] <- "Cherbourg"
train$Embarked2[train$Embarked == "Q"] <- "Queenstown"
train$Embarked2[train$Embarked == "S"] <- "Southhampton"

Exploratory Graphs

The following section includes some exploratory graphs to get an idea of the datasets and set some possible hypothesis on factor causality on survival.

outcomes.gender <- train %>% group_by(Sex, Outcome) %>% summarise(Totalnum = n())

ggplot(outcomes.gender, aes(x = Sex, y = Totalnum, fill = Outcome)) +
        geom_bar(stat = 'identity') + labs(y = "Total Individuals", x = "Gender", title = "Gender")

outcomes.class <- train %>% group_by(Pclass, Outcome) %>% summarise(Totalnum = n())

ggplot(outcomes.class, aes(x = Pclass, y = Totalnum, fill = Outcome)) +
        geom_bar(stat = 'identity') + labs(y = "Total Individuals", x = "Passenger Class", title = "Class")

outcomes.siblings <- train %>% group_by(SibSp, Outcome) %>% summarise(Totalnum = n())

ggplot(outcomes.siblings, aes(x = SibSp, y = Totalnum, fill = Outcome)) +
        geom_bar(stat = 'identity') + labs(y = "Total Individuals", x = "Number of Siblings or Spouses Aboard", title = "Sibling & Spouse")

outcomes.parch <- train %>% group_by(Parch, Outcome) %>% summarise(Totalnum = n())

ggplot(outcomes.parch, aes(x = Parch, y = Totalnum, fill = Outcome)) +
        geom_bar(stat = 'identity') + labs(y = "Total Individuals", x = "Number of Parents or Children Aboard", title = "Parents & Children")

outcomes.Embarked2 <- train %>% group_by(Embarked2, Outcome) %>% summarise(Totalnum = n())

ggplot(outcomes.Embarked2, aes(x = Embarked2, y = Totalnum, fill = Outcome)) +
        geom_bar(stat = 'identity') + labs(y = "Total Individuals", x = "Location Where Boarded Titanic", title = "Board Location")
## Warning: Removed 1 rows containing missing values (position_stack).

ggplot(train, aes(x = Outcome, y = Age)) +
               geom_violin(aes(fill = Outcome)) +
               geom_boxplot(width = 0.1, aes(fill = Outcome)) 
## Warning: Removed 177 rows containing non-finite values (stat_ydensity).
## Warning: Removed 177 rows containing non-finite values (stat_boxplot).

ggplot(train, aes(x = Outcome, y = Age)) +
               geom_violin(aes(fill = Outcome)) +
               geom_boxplot(width = 0.1, aes(fill = Outcome)) + 
        facet_wrap(~ Sex)
## Warning: Removed 177 rows containing non-finite values (stat_ydensity).
## Warning: Removed 177 rows containing non-finite values (stat_boxplot).

ggplot(train, aes(x = Outcome, y = Fare)) +
               geom_violin(aes(fill = Outcome)) +
               geom_boxplot(width = 0.1, aes(fill = Outcome))

ggplot(train, aes(x = Outcome, y = Fare)) +
               geom_violin(aes(fill = Outcome)) +
               geom_boxplot(width = 0.1, aes(fill = Outcome)) +
        facet_wrap(~Sex)

Models (rough rough draft)

Classification Tree Model

test <- read.csv("~/Desktop/Kaggle/Titanic/test (3).csv")
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
## 
##     lift
library(rpart)
library(rattle)
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
train.trim <- train[,c("Survived", "Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked")]


set.seed(121212)
inTrain = createDataPartition(train.trim$Survived, p = 0.60, list=FALSE)
train2 = train.trim[inTrain,]
train2.test = train.trim[-inTrain,]

set.seed(12321123)
modFit <- rpart(Survived ~., data = train2, method = "class")
fancyRpartPlot(modFit)

set.seed(12321312)
pred.mod <- predict(modFit, train2, type = "class")
confusionMatrix(pred.mod, train2$Survived)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 309  69
##          1  29 128
##                                           
##                Accuracy : 0.8168          
##                  95% CI : (0.7814, 0.8487)
##     No Information Rate : 0.6318          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.5889          
##  Mcnemar's Test P-Value : 8.162e-05       
##                                           
##             Sensitivity : 0.9142          
##             Specificity : 0.6497          
##          Pos Pred Value : 0.8175          
##          Neg Pred Value : 0.8153          
##              Prevalence : 0.6318          
##          Detection Rate : 0.5776          
##    Detection Prevalence : 0.7065          
##       Balanced Accuracy : 0.7820          
##                                           
##        'Positive' Class : 0               
## 
set.seed(12321312)
pred.mod <- predict(modFit, train2.test, type = "class")
confusionMatrix(pred.mod, train2.test$Survived)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 194  46
##          1  17  99
##                                           
##                Accuracy : 0.823           
##                  95% CI : (0.7793, 0.8613)
##     No Information Rate : 0.5927          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6216          
##  Mcnemar's Test P-Value : 0.0004192       
##                                           
##             Sensitivity : 0.9194          
##             Specificity : 0.6828          
##          Pos Pred Value : 0.8083          
##          Neg Pred Value : 0.8534          
##              Prevalence : 0.5927          
##          Detection Rate : 0.5449          
##    Detection Prevalence : 0.6742          
##       Balanced Accuracy : 0.8011          
##                                           
##        'Positive' Class : 0               
## 
test.pred <- predict(modFit, test) 
test.pred
##             0          1
## 1   0.8096591 0.19034091
## 2   0.3333333 0.66666667
## 3   0.8096591 0.19034091
## 4   0.8096591 0.19034091
## 5   0.4444444 0.55555556
## 6   0.8096591 0.19034091
## 7   0.3333333 0.66666667
## 8   0.8096591 0.19034091
## 9   0.3333333 0.66666667
## 10  0.8096591 0.19034091
## 11  0.8096591 0.19034091
## 12  0.8096591 0.19034091
## 13  0.0700000 0.93000000
## 14  0.8096591 0.19034091
## 15  0.0700000 0.93000000
## 16  0.0700000 0.93000000
## 17  0.8096591 0.19034091
## 18  0.8096591 0.19034091
## 19  0.3333333 0.66666667
## 20  0.3333333 0.66666667
## 21  0.8096591 0.19034091
## 22  0.8096591 0.19034091
## 23  0.0700000 0.93000000
## 24  0.8096591 0.19034091
## 25  0.0700000 0.93000000
## 26  0.8096591 0.19034091
## 27  0.0700000 0.93000000
## 28  0.8096591 0.19034091
## 29  0.8096591 0.19034091
## 30  0.8096591 0.19034091
## 31  0.8096591 0.19034091
## 32  0.8096591 0.19034091
## 33  0.4444444 0.55555556
## 34  0.4444444 0.55555556
## 35  0.8096591 0.19034091
## 36  0.8096591 0.19034091
## 37  0.9166667 0.08333333
## 38  0.9166667 0.08333333
## 39  0.8096591 0.19034091
## 40  0.8096591 0.19034091
## 41  0.8096591 0.19034091
## 42  0.8096591 0.19034091
## 43  0.8096591 0.19034091
## 44  0.0700000 0.93000000
## 45  0.0700000 0.93000000
## 46  0.8096591 0.19034091
## 47  0.8096591 0.19034091
## 48  0.8096591 0.19034091
## 49  0.0700000 0.93000000
## 50  0.4444444 0.55555556
## 51  0.8096591 0.19034091
## 52  0.8096591 0.19034091
## 53  0.0700000 0.93000000
## 54  0.0700000 0.93000000
## 55  0.8096591 0.19034091
## 56  0.8096591 0.19034091
## 57  0.8096591 0.19034091
## 58  0.8096591 0.19034091
## 59  0.8096591 0.19034091
## 60  0.0700000 0.93000000
## 61  0.8096591 0.19034091
## 62  0.8096591 0.19034091
## 63  0.8096591 0.19034091
## 64  0.3333333 0.66666667
## 65  0.8096591 0.19034091
## 66  0.0700000 0.93000000
## 67  0.3333333 0.66666667
## 68  0.8096591 0.19034091
## 69  0.8096591 0.19034091
## 70  0.0700000 0.93000000
## 71  0.3333333 0.66666667
## 72  0.8096591 0.19034091
## 73  0.3333333 0.66666667
## 74  0.8096591 0.19034091
## 75  0.0700000 0.93000000
## 76  0.8096591 0.19034091
## 77  0.8096591 0.19034091
## 78  0.0700000 0.93000000
## 79  0.8096591 0.19034091
## 80  0.3333333 0.66666667
## 81  0.8096591 0.19034091
## 82  0.8096591 0.19034091
## 83  0.8096591 0.19034091
## 84  0.8096591 0.19034091
## 85  0.8096591 0.19034091
## 86  0.8096591 0.19034091
## 87  0.3333333 0.66666667
## 88  0.9166667 0.08333333
## 89  0.3333333 0.66666667
## 90  0.8096591 0.19034091
## 91  0.4444444 0.55555556
## 92  0.8096591 0.19034091
## 93  0.0700000 0.93000000
## 94  0.8096591 0.19034091
## 95  0.8096591 0.19034091
## 96  0.8096591 0.19034091
## 97  0.0700000 0.93000000
## 98  0.8096591 0.19034091
## 99  0.3333333 0.66666667
## 100 0.8096591 0.19034091
## 101 0.0700000 0.93000000
## 102 0.8096591 0.19034091
## 103 0.8096591 0.19034091
## 104 0.8096591 0.19034091
## 105 0.9166667 0.08333333
## 106 0.8096591 0.19034091
## 107 0.8096591 0.19034091
## 108 0.8096591 0.19034091
## 109 0.8096591 0.19034091
## 110 0.8096591 0.19034091
## 111 0.8096591 0.19034091
## 112 0.3333333 0.66666667
## 113 0.0700000 0.93000000
## 114 0.3333333 0.66666667
## 115 0.0700000 0.93000000
## 116 0.8096591 0.19034091
## 117 0.8096591 0.19034091
## 118 0.4444444 0.55555556
## 119 0.8096591 0.19034091
## 120 0.0700000 0.93000000
## 121 0.0700000 0.93000000
## 122 0.8096591 0.19034091
## 123 0.0700000 0.93000000
## 124 0.8096591 0.19034091
## 125 0.8096591 0.19034091
## 126 0.4444444 0.55555556
## 127 0.8096591 0.19034091
## 128 0.4444444 0.55555556
## 129 0.8096591 0.19034091
## 130 0.8096591 0.19034091
## 131 0.8096591 0.19034091
## 132 0.8096591 0.19034091
## 133 0.9285714 0.07142857
## 134 0.8096591 0.19034091
## 135 0.8096591 0.19034091
## 136 0.8096591 0.19034091
## 137 0.8096591 0.19034091
## 138 0.8096591 0.19034091
## 139 0.9166667 0.08333333
## 140 0.8096591 0.19034091
## 141 0.9285714 0.07142857
## 142 0.0700000 0.93000000
## 143 0.8096591 0.19034091
## 144 0.8096591 0.19034091
## 145 0.8096591 0.19034091
## 146 0.8096591 0.19034091
## 147 0.8096591 0.19034091
## 148 0.8096591 0.19034091
## 149 0.8096591 0.19034091
## 150 0.8096591 0.19034091
## 151 0.0700000 0.93000000
## 152 0.8096591 0.19034091
## 153 0.8096591 0.19034091
## 154 0.4444444 0.55555556
## 155 0.8096591 0.19034091
## 156 0.8096591 0.19034091
## 157 0.0700000 0.93000000
## 158 0.3333333 0.66666667
## 159 0.8096591 0.19034091
## 160 0.4444444 0.55555556
## 161 0.3333333 0.66666667
## 162 0.8096591 0.19034091
## 163 0.0700000 0.93000000
## 164 0.8096591 0.19034091
## 165 0.8096591 0.19034091
## 166 0.4444444 0.55555556
## 167 0.8096591 0.19034091
## 168 0.8096591 0.19034091
## 169 0.0700000 0.93000000
## 170 0.9166667 0.08333333
## 171 0.8096591 0.19034091
## 172 0.8096591 0.19034091
## 173 0.8096591 0.19034091
## 174 0.8096591 0.19034091
## 175 0.8096591 0.19034091
## 176 0.0700000 0.93000000
## 177 0.0700000 0.93000000
## 178 0.8096591 0.19034091
## 179 0.0700000 0.93000000
## 180 0.0700000 0.93000000
## 181 0.8096591 0.19034091
## 182 0.8096591 0.19034091
## 183 0.0700000 0.93000000
## 184 0.8096591 0.19034091
## 185 0.0700000 0.93000000
## 186 0.8096591 0.19034091
## 187 0.0700000 0.93000000
## 188 0.8096591 0.19034091
## 189 0.9285714 0.07142857
## 190 0.8096591 0.19034091
## 191 0.8096591 0.19034091
## 192 0.8096591 0.19034091
## 193 0.8096591 0.19034091
## 194 0.8096591 0.19034091
## 195 0.8096591 0.19034091
## 196 0.8096591 0.19034091
## 197 0.8096591 0.19034091
## 198 0.3333333 0.66666667
## 199 0.8096591 0.19034091
## 200 0.9166667 0.08333333
## 201 0.4444444 0.55555556
## 202 0.8096591 0.19034091
## 203 0.8096591 0.19034091
## 204 0.0700000 0.93000000
## 205 0.8096591 0.19034091
## 206 0.8096591 0.19034091
## 207 0.3333333 0.66666667
## 208 0.8096591 0.19034091
## 209 0.0700000 0.93000000
## 210 0.8096591 0.19034091
## 211 0.8096591 0.19034091
## 212 0.8096591 0.19034091
## 213 0.8096591 0.19034091
## 214 0.0700000 0.93000000
## 215 0.3333333 0.66666667
## 216 0.8096591 0.19034091
## 217 0.3333333 0.66666667
## 218 0.8096591 0.19034091
## 219 0.0700000 0.93000000
## 220 0.8096591 0.19034091
## 221 0.0700000 0.93000000
## 222 0.8096591 0.19034091
## 223 0.0700000 0.93000000
## 224 0.8096591 0.19034091
## 225 0.0700000 0.93000000
## 226 0.4444444 0.55555556
## 227 0.8096591 0.19034091
## 228 0.3333333 0.66666667
## 229 0.8096591 0.19034091
## 230 0.8096591 0.19034091
## 231 0.8096591 0.19034091
## 232 0.0700000 0.93000000
## 233 0.8096591 0.19034091
## 234 0.8096591 0.19034091
## 235 0.8096591 0.19034091
## 236 0.8096591 0.19034091
## 237 0.8096591 0.19034091
## 238 0.8096591 0.19034091
## 239 0.0700000 0.93000000
## 240 0.0700000 0.93000000
## 241 0.0700000 0.93000000
## 242 0.0700000 0.93000000
## 243 0.8096591 0.19034091
## 244 0.8096591 0.19034091
## 245 0.8096591 0.19034091
## 246 0.8096591 0.19034091
## 247 0.0700000 0.93000000
## 248 0.8096591 0.19034091
## 249 0.0700000 0.93000000
## 250 0.4444444 0.55555556
## 251 0.0700000 0.93000000
## 252 0.8096591 0.19034091
## 253 0.8096591 0.19034091
## 254 0.8096591 0.19034091
## 255 0.8096591 0.19034091
## 256 0.8096591 0.19034091
## 257 0.8096591 0.19034091
## 258 0.8096591 0.19034091
## 259 0.0700000 0.93000000
## 260 0.8096591 0.19034091
## 261 0.8096591 0.19034091
## 262 0.8096591 0.19034091
## 263 0.0700000 0.93000000
## 264 0.4444444 0.55555556
## 265 0.8096591 0.19034091
## 266 0.8096591 0.19034091
## 267 0.8096591 0.19034091
## 268 0.8096591 0.19034091
## 269 0.9166667 0.08333333
## 270 0.8096591 0.19034091
## 271 0.8096591 0.19034091
## 272 0.8096591 0.19034091
## 273 0.0700000 0.93000000
## 274 0.4444444 0.55555556
## 275 0.8096591 0.19034091
## 276 0.0700000 0.93000000
## 277 0.8096591 0.19034091
## 278 0.8096591 0.19034091
## 279 0.8096591 0.19034091
## 280 0.8096591 0.19034091
## 281 0.9166667 0.08333333
## 282 0.8096591 0.19034091
## 283 0.3333333 0.66666667
## 284 0.4444444 0.55555556
## 285 0.4444444 0.55555556
## 286 0.8096591 0.19034091
## 287 0.8096591 0.19034091
## 288 0.8096591 0.19034091
## 289 0.8096591 0.19034091
## 290 0.8096591 0.19034091
## 291 0.8096591 0.19034091
## 292 0.3333333 0.66666667
## 293 0.8096591 0.19034091
## 294 0.8096591 0.19034091
## 295 0.8096591 0.19034091
## 296 0.8096591 0.19034091
## 297 0.0700000 0.93000000
## 298 0.8096591 0.19034091
## 299 0.8096591 0.19034091
## 300 0.8096591 0.19034091
## 301 0.8096591 0.19034091
## 302 0.8096591 0.19034091
## 303 0.8096591 0.19034091
## 304 0.8096591 0.19034091
## 305 0.3333333 0.66666667
## 306 0.0700000 0.93000000
## 307 0.8096591 0.19034091
## 308 0.8096591 0.19034091
## 309 0.8096591 0.19034091
## 310 0.4444444 0.55555556
## 311 0.8096591 0.19034091
## 312 0.8096591 0.19034091
## 313 0.8096591 0.19034091
## 314 0.3333333 0.66666667
## 315 0.0700000 0.93000000
## 316 0.3333333 0.66666667
## 317 0.8096591 0.19034091
## 318 0.8096591 0.19034091
## 319 0.8096591 0.19034091
## 320 0.8096591 0.19034091
## 321 0.8096591 0.19034091
## 322 0.8096591 0.19034091
## 323 0.8096591 0.19034091
## 324 0.8096591 0.19034091
## 325 0.0700000 0.93000000
## 326 0.8096591 0.19034091
## 327 0.0700000 0.93000000
## 328 0.8096591 0.19034091
## 329 0.8096591 0.19034091
## 330 0.8096591 0.19034091
## 331 0.0700000 0.93000000
## 332 0.8096591 0.19034091
## 333 0.8096591 0.19034091
## 334 0.4444444 0.55555556
## 335 0.8096591 0.19034091
## 336 0.8096591 0.19034091
## 337 0.8096591 0.19034091
## 338 0.8096591 0.19034091
## 339 0.8096591 0.19034091
## 340 0.8096591 0.19034091
## 341 0.8096591 0.19034091
## 342 0.8096591 0.19034091
## 343 0.8096591 0.19034091
## 344 0.0700000 0.93000000
## 345 0.8096591 0.19034091
## 346 0.3333333 0.66666667
## 347 0.8096591 0.19034091
## 348 0.3333333 0.66666667
## 349 0.8096591 0.19034091
## 350 0.0700000 0.93000000
## 351 0.0700000 0.93000000
## 352 0.8096591 0.19034091
## 353 0.8096591 0.19034091
## 354 0.8096591 0.19034091
## 355 0.4444444 0.55555556
## 356 0.8096591 0.19034091
## 357 0.0700000 0.93000000
## 358 0.8096591 0.19034091
## 359 0.8096591 0.19034091
## 360 0.4444444 0.55555556
## 361 0.8096591 0.19034091
## 362 0.0700000 0.93000000
## 363 0.0700000 0.93000000
## 364 0.8096591 0.19034091
## 365 0.0700000 0.93000000
## 366 0.9285714 0.07142857
## 367 0.8096591 0.19034091
## 368 0.9285714 0.07142857
## 369 0.0700000 0.93000000
## 370 0.8096591 0.19034091
## 371 0.8096591 0.19034091
## 372 0.0700000 0.93000000
## 373 0.8096591 0.19034091
## 374 0.8096591 0.19034091
## 375 0.0700000 0.93000000
## 376 0.0700000 0.93000000
## 377 0.9166667 0.08333333
## 378 0.8096591 0.19034091
## 379 0.8096591 0.19034091
## 380 0.8096591 0.19034091
## 381 0.8096591 0.19034091
## 382 0.8096591 0.19034091
## 383 0.4444444 0.55555556
## 384 0.4444444 0.55555556
## 385 0.8096591 0.19034091
## 386 0.0700000 0.93000000
## 387 0.8096591 0.19034091
## 388 0.8096591 0.19034091
## 389 0.8096591 0.19034091
## 390 0.8096591 0.19034091
## 391 0.8096591 0.19034091
## 392 0.0700000 0.93000000
## 393 0.8096591 0.19034091
## 394 0.8096591 0.19034091
## 395 0.8096591 0.19034091
## 396 0.0700000 0.93000000
## 397 0.8096591 0.19034091
## 398 0.0700000 0.93000000
## 399 0.8096591 0.19034091
## 400 0.8096591 0.19034091
## 401 0.0700000 0.93000000
## 402 0.8096591 0.19034091
## 403 0.0700000 0.93000000
## 404 0.8096591 0.19034091
## 405 0.8096591 0.19034091
## 406 0.8096591 0.19034091
## 407 0.8096591 0.19034091
## 408 0.8096591 0.19034091
## 409 0.3333333 0.66666667
## 410 0.4444444 0.55555556
## 411 0.3333333 0.66666667
## 412 0.0700000 0.93000000
## 413 0.3333333 0.66666667
## 414 0.8096591 0.19034091
## 415 0.0700000 0.93000000
## 416 0.8096591 0.19034091
## 417 0.8096591 0.19034091
## 418 0.8096591 0.19034091
test2 <- cbind.data.frame(test, test.pred)

Random Forest Model

library(randomForest)
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:dplyr':
## 
##     combine
## The following object is masked from 'package:ggplot2':
## 
##     margin
library(party)
## Loading required package: grid
## Loading required package: mvtnorm
## Loading required package: modeltools
## Loading required package: stats4
## Loading required package: strucchange
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
train22 <- train2[complete.cases(train2),]

str(train22)
## 'data.frame':    423 obs. of  8 variables:
##  $ Survived: int  1 1 1 0 0 1 0 0 1 1 ...
##  $ Pclass  : int  1 3 1 3 3 2 3 2 2 3 ...
##  $ Sex     : Factor w/ 2 levels "female","male": 1 1 1 2 1 1 2 2 2 1 ...
##  $ Age     : num  38 26 58 20 14 55 2 35 34 15 ...
##  $ SibSp   : int  1 0 0 0 0 0 4 0 0 0 ...
##  $ Parch   : int  0 0 0 0 0 0 1 0 0 0 ...
##  $ Fare    : num  71.28 7.92 26.55 8.05 7.85 ...
##  $ Embarked: Factor w/ 4 levels "","C","Q","S": 2 4 4 4 4 4 3 4 4 3 ...
train22$Survived <- as.factor(train22$Survived)
train22$Pclass <- as.factor(train22$Pclass)
train22$SibSp <- as.factor(train22$SibSp)
train22$Parch <- as.factor(train22$Parch)

set.seed(123123)
modFit2 <- randomForest(Survived ~ ., data = train22, ntree = 400, importance=TRUE)

set.seed(12321312)
trainingPred <- predict(modFit2, train22)
confusionMatrix(trainingPred, train22$Survived)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 255  30
##          1   4 134
##                                           
##                Accuracy : 0.9196          
##                  95% CI : (0.8895, 0.9437)
##     No Information Rate : 0.6123          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.8256          
##  Mcnemar's Test P-Value : 1.807e-05       
##                                           
##             Sensitivity : 0.9846          
##             Specificity : 0.8171          
##          Pos Pred Value : 0.8947          
##          Neg Pred Value : 0.9710          
##              Prevalence : 0.6123          
##          Detection Rate : 0.6028          
##    Detection Prevalence : 0.6738          
##       Balanced Accuracy : 0.9008          
##                                           
##        'Positive' Class : 0               
## 
varImpPlot(modFit2)

plot(modFit2, log="y")

getTree(modFit2)
##     left daughter right daughter split var split point status prediction
## 1               2              3         7     2.00000      1          0
## 2               4              5         2     1.00000      1          0
## 3               6              7         6    10.82500      1          0
## 4               8              9         6    36.08750      1          0
## 5              10             11         3    34.00000      1          0
## 6              12             13         3    15.50000      1          0
## 7              14             15         2     1.00000      1          0
## 8              16             17         3    11.50000      1          0
## 9               0              0         0     0.00000     -1          2
## 10             18             19         6    16.62085      1          0
## 11              0              0         0     0.00000     -1          2
## 12             20             21         5     1.00000      1          0
## 13             22             23         3    17.50000      1          0
## 14             24             25         4    19.00000      1          0
## 15             26             27         4    22.00000      1          0
## 16              0              0         0     0.00000     -1          1
## 17             28             29         6    12.84795      1          0
## 18              0              0         0     0.00000     -1          1
## 19             30             31         5     1.00000      1          0
## 20              0              0         0     0.00000     -1          2
## 21              0              0         0     0.00000     -1          1
## 22              0              0         0     0.00000     -1          1
## 23             32             33         2     1.00000      1          0
## 24             34             35         4     3.00000      1          0
## 25             36             37         5     1.00000      1          0
## 26             38             39         1     1.00000      1          0
## 27             40             41         4     1.00000      1          0
## 28              0              0         0     0.00000     -1          2
## 29             42             43         4     1.00000      1          0
## 30             44             45         1     1.00000      1          0
## 31              0              0         0     0.00000     -1          1
## 32             46             47         4     1.00000      1          0
## 33             48             49         6     7.91040      1          0
## 34             50             51         6    22.03750      1          0
## 35              0              0         0     0.00000     -1          2
## 36              0              0         0     0.00000     -1          2
## 37             52             53         6   154.95000      1          0
## 38             54             55         3    60.00000      1          0
## 39             56             57         3    11.50000      1          0
## 40             58             59         6    18.31250      1          0
## 41              0              0         0     0.00000     -1          1
## 42              0              0         0     0.00000     -1          1
## 43              0              0         0     0.00000     -1          2
## 44              0              0         0     0.00000     -1          1
## 45              0              0         0     0.00000     -1          1
## 46              0              0         0     0.00000     -1          1
## 47              0              0         0     0.00000     -1          1
## 48             60             61         1     1.00000      1          0
## 49             62             63         5     1.00000      1          0
## 50             64             65         5     7.00000      1          0
## 51             66             67         4     1.00000      1          0
## 52              0              0         0     0.00000     -1          1
## 53              0              0         0     0.00000     -1          2
## 54             68             69         6   101.73750      1          0
## 55              0              0         0     0.00000     -1          1
## 56             70             71         3     2.50000      1          0
## 57              0              0         0     0.00000     -1          1
## 58             72             73         6    13.25000      1          0
## 59             74             75         1     2.00000      1          0
## 60              0              0         0     0.00000     -1          1
## 61             76             77         4     1.00000      1          0
## 62             78             79         4     1.00000      1          0
## 63              0              0         0     0.00000     -1          1
## 64             80             81         6    12.82500      1          0
## 65              0              0         0     0.00000     -1          1
## 66              0              0         0     0.00000     -1          2
## 67             82             83         5     3.00000      1          0
## 68              0              0         0     0.00000     -1          2
## 69              0              0         0     0.00000     -1          2
## 70              0              0         0     0.00000     -1          1
## 71              0              0         0     0.00000     -1          2
## 72              0              0         0     0.00000     -1          1
## 73              0              0         0     0.00000     -1          1
## 74              0              0         0     0.00000     -1          1
## 75             84             85         5     3.00000      1          0
## 76              0              0         0     0.00000     -1          1
## 77             86             87         3    27.00000      1          0
## 78              0              0         0     0.00000     -1          1
## 79              0              0         0     0.00000     -1          1
## 80              0              0         0     0.00000     -1          2
## 81             88             89         4     1.00000      1          0
## 82              0              0         0     0.00000     -1          2
## 83             90             91         3     3.50000      1          0
## 84             92             93         1     1.00000      1          0
## 85              0              0         0     0.00000     -1          2
## 86             94             95         6     7.81460      1          0
## 87              0              0         0     0.00000     -1          1
## 88             96             97         5     1.00000      1          0
## 89             98             99         6    18.17500      1          0
## 90              0              0         0     0.00000     -1          1
## 91              0              0         0     0.00000     -1          2
## 92            100            101         5     1.00000      1          0
## 93              0              0         0     0.00000     -1          1
## 94              0              0         0     0.00000     -1          2
## 95              0              0         0     0.00000     -1          1
## 96              0              0         0     0.00000     -1          2
## 97              0              0         0     0.00000     -1          2
## 98              0              0         0     0.00000     -1          1
## 99              0              0         0     0.00000     -1          2
## 100           102            103         6    26.46875      1          0
## 101             0              0         0     0.00000     -1          1
## 102             0              0         0     0.00000     -1          2
## 103             0              0         0     0.00000     -1          1