先删去病人的序号
> df <- mydata[, -1]
将death的1,0变成alive, dead
df$death <- factor(df$death,levels = c(1,0),labels = c("alive","dead"))
设定训练集和验证集
> train <- sample(nrow(df), 0.8*nrow(df))
> df.train <- df[train,]
> df.validate <- df[-train,]
使用支持向量机SVM
> library(e1071)
> set.seed(100)
> fit.svm <- svm(death~., data = df.train)
> fit.svm
Call:
svm(formula = death ~ ., data = df.train)
Parameters:
SVM-Type: C-classification
SVM-Kernel: radial
cost: 1
Number of Support Vectors: 305
进行验证
> svm.pred <- predict(fit.svm, na.omit(df.validate))
> svm.perf <- table(na.omit(df.validate)$death, svm.pred, dnn=c("Actual", "Predicted"))
> svm.perf
Predicted
Actual alive dead
alive 22 25
dead 12 49<