-
Notifications
You must be signed in to change notification settings - Fork 4
/
lm.campbell.r
138 lines (111 loc) · 3.24 KB
/
lm.campbell.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
setwd('D:/Development/RScripts/Titanic/')
# extract deck name from Cabin number
cabin_to_deck <- function(data) {
for(i in seq(along=data)) {
if (is.na(data[i])) {
data[i] = '?'
next
}
data[i] <- substr(data[i], 1, 1)
}
return (data)
}
# extract Title from Name
extract_title <- function(data) {
for(i in seq(along=data)) {
if (is.na(data[i]))
next
a <- unlist(strsplit(data[i], ', '))[2]
b <- unlist(strsplit(a, ' '))[1]
if (b == 'the') {
b = 'Countess.'
}
data[i] <- b
}
return (data)
}
rooms <- function(data) {
result = numeric()
for(i in seq(along=data)) {
if (is.na(data[i])) {
result[i] = 0
next
}
parts <- strsplit(data[i], ' ')
result[i] <- length(unlist(parts))
}
return (result)
}
name = 'train'
filename <- paste('data/', '.csv', sep=name)
data <- read.csv(filename, sep=',', na.strings=c(''))
# convert some columns to factors
data$Sex <- factor(data$Sex)
data$Embarked <- factor(data$Embarked)
data$Pclass <- factor(data$Pclass)
# extract deck letter from cabin
cabins <- as.character(data$Cabin)
data$Cabin <- cabin_to_deck(cabins)
data$Cabin <- factor(data$Cabin)
data$Rooms <- rooms(cabins)
# extract title from name
data$Title <- extract_title(as.character(data$Name))
data$Title <- factor(data$Title)
# drop some columns
data = subset(data, select = -c(Name, Ticket))
# impute age
models.age <- lm(Age ~ Pclass + SibSp + Parch + Sex, data=data)
for(i in 1:nrow(data)) {
if (is.na(data[i, 'Age'])) {
data[i, 'Age'] <- max(0, predict(models.age, newdata=data[i,]))
}
}
# impute embarked
data$Embarked[which(is.na(data$Embarked))] = 'S'
# convert into factor here because test set wont have this column
data$Survived <- factor(data$Survived)
# train model
models.glm = glm(
Survived ~ Pclass + SibSp + Parch +
Sex + Age + Cabin + Rooms + Pclass:Age +
Age:Sex + SibSp:Sex,
family=binomial(link='logit'),
data=data
)
p = predict(models.glm, newdata=data, type='response')
survived = round(p)
library(caret)
confusionMatrix(factor(survived), data$Survived)
# make prediction
name = 'test'
filename <- paste('data/', '.csv', sep=name)
test <- read.csv(filename, sep=',', na.strings=c(''))
# convert some columns to factors
test$Sex <- factor(test$Sex)
test$Embarked <- factor(test$Embarked)
test$Pclass <- factor(test$Pclass)
# extract deck letter from cabin
cabins <- as.character(test$Cabin)
test$Cabin <- cabin_to_deck(cabins)
test$Cabin <- factor(test$Cabin)
test$Rooms <- rooms(cabins)
# extract title from name
test$Title <- extract_title(as.character(test$Name))
test$Title <- factor(test$Title)
# drop some columns
test = subset(test, select = -c(Name, Ticket))
# impute missing fare
test$Fare[153] <- mean(
with(test, subset(Fare, Pclass == 3)),
na.rm=TRUE
)
# impute age
models.age2 <- lm(Age ~ Pclass + SibSp + Parch + Sex, data=test)
for(i in 1:nrow(test)) {
if (is.na(test[i, 'Age'])) {
test[i, 'Age'] <- max(0, predict(models.age2, newdata=test[i,]))
}
}
p = predict(models.glm, newdata=test, type='response')
p = data.frame(PassengerId = test$PassengerId, survived = round(p))
write.csv(p, 'predictions.csv', row.names = FALSE)