|
| 1 | +setwd('D:/Development/RScripts/Titanic/') |
| 2 | + |
| 3 | +data = read.csv('data/train.csv', sep=',', na.strings=c('')) |
| 4 | + |
| 5 | +data$Survived <- factor(data$Survived) |
| 6 | +#data$Sex <- factor(data$Sex) |
| 7 | +#data$Embarked <- factor(data$Embarked) |
| 8 | +#data$Pclass <- factor(data$Pclass) |
| 9 | + |
| 10 | +# extract deck name from Cabin number |
| 11 | +cabin_to_deck <- function(data) { |
| 12 | + data = as.character(data) |
| 13 | + for(i in seq(along=data)) { |
| 14 | + if (is.na(data[i])) |
| 15 | + next |
| 16 | + data[i] <- substr(data[i], 1, 1) |
| 17 | + } |
| 18 | + return (data) |
| 19 | +} |
| 20 | + |
| 21 | +data$Cabin <- cabin_to_deck(data$Cabin) |
| 22 | +data$Cabin <- factor(data$Cabin, levels=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'T')) |
| 23 | + |
| 24 | +# extract Title from Name |
| 25 | +extract_title <- function(data) { |
| 26 | + for(i in seq(along=data)) { |
| 27 | + if (is.na(data[i])) |
| 28 | + next |
| 29 | + a <- unlist(strsplit(data[i], ', '))[2] |
| 30 | + b <- unlist(strsplit(a, '. '))[1] |
| 31 | + data[i] <- b |
| 32 | + } |
| 33 | + return (data) |
| 34 | +} |
| 35 | + |
| 36 | +data$Title <- extract_title(as.character(data$Name)) |
| 37 | +data$Title <- factor(data$Title) |
| 38 | + |
| 39 | +# impute age |
| 40 | +models.age <- lm(Age ~ Fare + Title + SibSp + Parch, data=data) |
| 41 | +for(i in 1:nrow(data)) { |
| 42 | + if (is.na(data[i, 'Age'])) { |
| 43 | + data[i, 'Age'] <- predict(models.age, newdata=data[i,]) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +models.glm = glm(Survived ~ Pclass + Fare + SibSp + Parch + Sex + Age + Pclass:Age + Age:Sex + SibSp:Sex, family=binomial(link='logit'), data=data) |
| 49 | + |
| 50 | +p = predict(models.glm, newdata=data, type='response') |
| 51 | +survived = round(p) |
| 52 | + |
| 53 | +library(caret) |
| 54 | +confusionMatrix(factor(survived), data$Survived) |
| 55 | + |
| 56 | +# make prediction |
| 57 | + |
| 58 | +test = read.csv('data/test.csv', sep=',', na.strings=c('')) |
| 59 | + |
| 60 | +# extract deck name from Cabin number |
| 61 | +cabin_to_deck <- function(data) { |
| 62 | + data = as.character(data) |
| 63 | + for(i in seq(along=data)) { |
| 64 | + if (is.na(data[i])) |
| 65 | + next |
| 66 | + data[i] <- substr(data[i], 1, 1) |
| 67 | + } |
| 68 | + return (data) |
| 69 | +} |
| 70 | + |
| 71 | +test$Cabin <- cabin_to_deck(test$Cabin) |
| 72 | +test$Cabin <- factor(test$Cabin, levels=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'T')) |
| 73 | + |
| 74 | +# extract Title from Name |
| 75 | +extract_title <- function(data) { |
| 76 | + for(i in seq(along=data)) { |
| 77 | + if (is.na(data[i])) |
| 78 | + next |
| 79 | + a <- unlist(strsplit(data[i], ', '))[2] |
| 80 | + b <- unlist(strsplit(a, '. '))[1] |
| 81 | + data[i] <- b |
| 82 | + } |
| 83 | + return (data) |
| 84 | +} |
| 85 | + |
| 86 | +test$Title <- extract_title(as.character(test$Name)) |
| 87 | +test$Title <- factor(test$Title) |
| 88 | + |
| 89 | +# impute age |
| 90 | +models.age <- lm(Age ~ Fare + Title + SibSp + Parch, data=data) |
| 91 | +for(i in 1:nrow(test)) { |
| 92 | + if (is.na(test[i, 'Age'])) { |
| 93 | + test[i, 'Age'] <- predict(models.age, newdata=test[i,]) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +test$Fare[153] <- mean( |
| 98 | + with(test, subset(Fare, Pclass == 3)), |
| 99 | + na.rm=TRUE |
| 100 | +) |
| 101 | + |
| 102 | +summary(test) |
| 103 | + |
| 104 | +p = predict(models.glm, newdata=test, type='response') |
| 105 | + |
| 106 | +data = data.frame(PassengerId = test$PassengerId, survived = round(p)) |
| 107 | +write.csv(data, 'predictions.csv', row.names = FALSE) |
0 commit comments