ibrary(ipred)
library(rpart)
library(rpart.plot)
library(RColorBrewer)
library(rattle)
library(rpart.plot)
library(RColorBrewer)
library(mvtnorm)
library(modeltools)
library(stats4)
library(strucchange)
library(zoo)
library(ipred)
library(randomForest)
#读取数据
train1<-read.csv(“train.csv”,stringsAsFactors=FALSE)
test1<-read.csv(“test.csv”,stringsAsFactors=FALSE)
train1KaTeX parse error: Expected 'EOF', got '#' at position 9: Name[1] #̲设置测试数据中Survived…Survived <- NA
#将训练数据的和测试数据整合到一起
combi <- rbind(train1,test1)
#将combi中名字转成字符串格式
combi
N
a
m
e
<
−
a
s
.
c
h
a
r
a
c
t
e
r
(
c
o
m
b
i
Name <- as.character(combi
Name<−as.character(combiName)
#读取第一个名字
combiKaTeX parse error: Expected 'EOF', got '#' at position 9: Name[1] #̲对名字进行拆分 strspli…Name[1], split=’[,.]’)
#索引1
strsplit(combiKaTeX parse error: Expected 'EOF', got '#' at position 29: …t='[,.]')[[1]] #̲索引2,挖出姓氏 strspl…Name[1], split=’[,.]’)[[1]][2]
#对所有的乘客姓名进行划分,并挖出姓氏
combi
T
i
t
l
e
<
−
s
a
p
p
l
y
(
c
o
m
b
i
Title <- sapply(combi
Title<−sapply(combiName, FUN=function(x) {strsplit(x, split=’[,.]’)[[1]][2]})
combiKaTeX parse error: Double superscript at position 16: Title <- sub(' '̲, '', combiTitle)
#列出划分后的数据
table(combiKaTeX parse error: Expected 'EOF', got '#' at position 8: Title) #̲创建新的组合 combiTitle[combi
T
i
t
l
e
c
o
m
b
i
Title %in% c('Mme', 'Mlle')] <- 'Mlle' combi
TitlecombiTitle[combi
T
i
t
l
e
c
o
m
b
i
Title %in% c('Capt', 'Don', 'Major', 'Sir')] <- 'Sir' combi
TitlecombiTitle[combiKaTeX parse error: Expected 'EOF', got '#' at position 69: …r')] <- 'Lady' #̲创建因子 combiTitle <- factor(combi$Title)
#建立一个家庭组合变量
combi
F
a
m
i
l
y
S
i
z
e
<
−
c
o
m
b
i
FamilySize <- combi
FamilySize<−combiSibSp + combiKaTeX parse error: Expected 'EOF', got '#' at position 11: Parch + 1 #̲对字符串进行处理 combiSurname <- sapply(combiKaTeX parse error: Expected 'EOF', got '#' at position 59: ….]')[[1]][1]}) #̲创建新的变量 combiFamilyID <- paste(as.character(combi
F
a
m
i
l
y
S
i
z
e
)
,
c
o
m
b
i
FamilySize), combi
FamilySize),combiSurname, sep="")
combi
F
a
m
i
l
y
I
D
[
c
o
m
b
i
FamilyID[combi
FamilyID[combiFamilySize <= 2] <- ‘Small’
#查看家族ID中的分布个数
table(combiKaTeX parse error: Expected 'EOF', got '#' at position 11: FamilyID) #̲转化为数据框 famIDs <…FamilyID))
famIDs <- famIDs[famIDs
F
r
e
q
<
=
2
,
]
c
o
m
b
i
Freq <= 2,] combi
Freq<=2,]combiFamilyID[combi
F
a
m
i
l
y
I
D
FamilyID %in% famIDs
FamilyIDVar1] <- ‘Small’
combi
F
a
m
i
l
y
I
D
<
−
f
a
c
t
o
r
(
c
o
m
b
i
FamilyID <- factor(combi
FamilyID<−factor(combiFamilyID)
#创建决策树
Agefit <- rpart(Age ~ Pclass + Sex + SibSp + Parch + Fare + Embarked + Title + FamilySize,
data=combi[!is.na(combiKaTeX parse error: Expected 'EOF', got '#' at position 25: …ethod="anova") #̲画出决策树图形 fancyRp…Age[is.na(combi
A
g
e
)
]
<
−
p
r
e
d
i
c
t
(
A
g
e
f
i
t
,
c
o
m
b
i
[
i
s
.
n
a
(
c
o
m
b
i
Age)] <- predict(Agefit, combi[is.na(combi
Age)]<−predict(Agefit,combi[is.na(combiAge),])
#创建一个新变量
combi
F
a
m
i
l
y
I
D
2
<
−
c
o
m
b
i
FamilyID2 <- combi
FamilyID2<−combiFamilyID
combi
F
a
m
i
l
y
I
D
2
<
−
a
s
.
c
h
a
r
a
c
t
e
r
(
c
o
m
b
i
FamilyID2 <- as.character(combi
FamilyID2<−as.character(combiFamilyID2)
combi
F
a
m
i
l
y
I
D
2
[
c
o
m
b
i
FamilyID2[combi
FamilyID2[combiFamilySize <= 3] <- ‘Small’
combi
F
a
m
i
l
y
I
D
2
<
−
f
a
c
t
o
r
(
c
o
m
b
i
FamilyID2 <- factor(combi
FamilyID2<−factor(combiFamilyID2)
#将合并后的数组进行分开
train_data <- data.frame(combi[1:891,])
test_data <- data.frame(combi[892:1309,])
#利用装袋技术进行建模
BAG=bagging((Survived) ~ Pclass+Age+Sex+SibSp+Parch + Fare + Embarked + Title + FamilySize + FamilyID,data = train_data,mfinal=900000)
#对于预测数据做回归预测
Predictio <- predict(BAG, test_data)
sub<- data.frame(PassengerId = test_data$PassengerId, Survive = Predictio)
sub
S
u
r
v
i
v
e
d
[
s
u
b
Survived[sub
Survived[subSurvive<0.5]<-0
sub
S
u
r
v
i
v
e
d
[
s
u
b
Survived[sub
Survived[subSurvive>=0.5]<-1
sub1=sub[,-2]
summary(sub1)
write.csv(sub1, file = “best.csv”, row.names = FALSE