-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject 3-prep data.R
More file actions
122 lines (77 loc) · 3.4 KB
/
Copy pathProject 3-prep data.R
File metadata and controls
122 lines (77 loc) · 3.4 KB
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
# Project 3: Preparing the dataset for analysis
rm(list=ls())
# Libraries
library(DescTools)
library(stringr)
library(scales)
library(dplyr)
library(rpart)
library(rpart.plot)
library(randomForest)
library(caret)
library(kernlab)
library(pracma)
# Set working directory
setwd('C:/Users/conno/Documents/School work/STA 5900/Titanic Survival/Data')
titanic <- read.csv('train.csv')
titanic.test <- read.csv('test.csv')
titanic.test$Survived <- rep('NA',nrow(titanic.test))
titanicPlus <- rbind(titanic, titanic.test)
# ---------- Preparing Variables For Models ----------
# Make Variables categorical
titanicPlus$Sex[titanicPlus$Sex=='female'] <- 1
titanicPlus$Sex[titanicPlus$Sex=='male'] <- 2
titanicPlus$Sex <- as.numeric(titanicPlus$Sex)
titanicPlus$Survived <- as.factor(titanicPlus$Survived)
levels(titanicPlus$Survived) <- c('No', 'Yes', 'NA')
titanicPlus$Embarked <- as.factor(titanicPlus$Embarked)
titanicPlus$Pclass <- as.factor(titanicPlus$Pclass)
# Fare price for Passenger 1044 is blank
titanicPlus$Fare[titanicPlus$PassengerId==1044] <- 7.5
# Split Name into First and Last
split.name <- strsplit(titanicPlus$Name, ',')
Lastname <- sapply(split.name, function(x) x[1])
Firstname <- sapply(split.name, function(x) x[2])
titanicPlus$Lastname <- Lastname
# Adding Number of Occurance of Last Name to each person
Lastname.Occur <- Freq(titanicPlus$Lastname)
titanicPlus$Occur <- rep(1,length(Lastname))
for (i in Lastname.Occur$level) {
titanicPlus$Occur[which(Lastname==i)] <- Lastname.Occur[Lastname.Occur$level==i,]$freq
}
# Split Prefix of First Name
split.prefix <- strsplit(Firstname, '. ')
titanicPlus$Prefix <- sapply(split.prefix, function(x) x[1])
titanicPlus$Prefix[which(titanicPlus$Prefix %in% c(' Capt',' Col',
' Don',
' Jonkheer',
' Major',' Rev'
))] <- ' Mr'
titanicPlus$Prefix[which(titanicPlus$Prefix==' Dr' &
titanicPlus$Sex=='male')] <- ' Mr'
titanicPlus$Prefix[which(titanicPlus$Prefix %in% c(' Dona',' Mme'
))] <- ' Mrs'
titanicPlus$Prefix[which(titanicPlus$Prefix %in% c(' Ms',' Mlle'
))] <- ' Miss'
titanicPlus$Prefix[which(titanicPlus$Prefix==' Dr' &
titanicPlus$Sex=='female')] <- ' Mrs'
titanicPlus$Prefix <- as.factor(titanicPlus$Prefix)
levels(titanicPlus$Prefix)
# Separate Cabin Letter
split.cabin <- strsplit(titanicPlus$Cabin, '')
titanicPlus$Letter <- sapply(split.cabin, function(x) x[1])
# Adding Cabin Letter to Family members
Letters.notNA <- titanicPlus[-which(is.na(titanicPlus$Letter)),]
for (i in Letters.notNA$Lastname[Letters.notNA$Occur>1]) {
titanicPlus$Letter[which(Lastname==i)] <- unique(Letters.notNA$Letter[Letters.notNA$Lastname==i])[1]
}
titanicPlus$Letter <- as.factor(titanicPlus$Letter)
# Adding a Family Size Column
titanicPlus$Family <- titanicPlus$Parch + titanicPlus$SibSp
# Adding a Fare per Person Column
titanicPlus$FPP <- titanicPlus$Fare / (titanicPlus$Family+1)
# Creating an Age.na column: represents with passengers had incomplete Age
titanicPlus$Age.Na <- 0
titanicPlus$Age.Na[which(is.na(titanicPlus$Age))] <- 1
# ------ Creating CSV for the new dataset ------
write.csv(titanicPlus,'new_train.csv',row.names=FALSE)