|
| 1 | +library(caret) |
| 2 | +library(randomForest) |
| 3 | +library(ggplot2) |
| 4 | +library(Amelia) |
| 5 | + |
| 6 | +setwd("E:/data analytics/kaggle/titanic/data") |
| 7 | + |
| 8 | +trainSet = read.csv("train.csv", header = TRUE, na.strings=c("NA","")) |
| 9 | +dim(trainSet) |
| 10 | +str(trainSet) |
| 11 | +head(trainSet) |
| 12 | +trainSet$Survived = factor(trainSet$Survived) |
| 13 | +trainSet$Pclass = factor(trainSet$Pclass) |
| 14 | +summary(trainSet) |
| 15 | + |
| 16 | +missmap(trainSet, main="Titanic Training Data - Missings Map", |
| 17 | + col=c("yellow", "black"), legend=FALSE) |
| 18 | + |
| 19 | +table(trainSet$Survived) |
| 20 | +ggplot(trainSet, aes(x = Survived)) + geom_bar() |
| 21 | + |
| 22 | +#Comparing Survived and passenger class using table and histograms |
| 23 | +summary(trainSet$Pclass) |
| 24 | +xtabs(~Survived + Pclass, data=trainSet) |
| 25 | +ggplot(trainSet, aes(x = Survived, fill = Pclass)) + geom_bar() |
| 26 | + |
| 27 | +#Comparing Survived and Sex using table and histograms |
| 28 | +summary(trainSet$Sex) |
| 29 | +xtabs(~Survived + Sex, data=trainSet) |
| 30 | +ggplot(trainSet, aes(x = Survived, fill = Sex)) + geom_bar() |
| 31 | + |
| 32 | + |
| 33 | +#Comparing Survived and Embarked using table and histograms |
| 34 | +summary(trainSet$Embarked) |
| 35 | +xtabs(~Survived + Embarked, data=trainSet) |
| 36 | +ggplot(trainSet, aes(x = Survived, fill = Embarked)) + geom_bar() |
| 37 | + |
| 38 | +# Comparing Age and Survived: The boxplots are very similar between Age |
| 39 | +# for survivors and those who died. |
| 40 | +xtabs(~Survived + Age, data=trainSet) |
| 41 | +ggplot(trainSet, aes(x = Survived, y = Age)) + geom_boxplot() |
| 42 | +# Also, there are lots of NA's. Exclude this variable |
| 43 | +summary(trainSet$Age) |
| 44 | + |
| 45 | +# Comparing Survived and Fare: The boxplots are much different between |
| 46 | +# fare for survivors and those who died. |
| 47 | +ggplot(trainSet, aes(x = Survived, y = Fare)) + geom_boxplot() |
| 48 | +# Also, there are no NA's. Include this variable. |
| 49 | +summary(trainSet$Fare) |
| 50 | + |
| 51 | +# Comparing Survived and Parch |
| 52 | +ggplot(trainSet, aes(x = Survived, y = Parch)) + geom_boxplot() |
| 53 | +summary(trainSet$Parch) |
| 54 | + |
| 55 | +# Set a random seed |
| 56 | +set.seed(42) |
| 57 | + |
| 58 | +#model tuning strategy |
| 59 | +ctrl = trainControl(method = "cv", # Use cross-validation |
| 60 | + number = 10) # Use 10 folds for cross-validation |
| 61 | + |
| 62 | +# Train the model using a "random forest" algorithm |
| 63 | +model_rf = train(Survived ~ Pclass + Sex + Age + Embarked + SibSp + Fare, |
| 64 | + data = trainSet, |
| 65 | + method = "rpart", |
| 66 | + trControl = ctrl) |
| 67 | +model_rf |
| 68 | + |
| 69 | +testSet = read.table("test.csv", sep = ",", header = TRUE) |
| 70 | +dim(testSet) |
| 71 | +str(testSet) |
| 72 | +head(testSet) |
| 73 | +testSet$Pclass = factor(testSet$Pclass) |
| 74 | +summary(testSet) |
| 75 | +testSet$Fare = ifelse(is.na(testSet$Fare), mean(testSet$Fare, na.rm = TRUE), testSet$Fare) |
| 76 | + |
| 77 | + |
| 78 | +testSet$Survived = predict(model_logit, newdata = testSet) |
| 79 | + |
| 80 | +submission = testSet[,c("PassengerId", "Survived")] |
| 81 | + |
| 82 | +write.table(submission, file = "submission.csv", col.names = TRUE, row.names = FALSE, sep = ",") |
0 commit comments