svm
1.準備輸入數(shù)據(jù)
load("TCGA-KIRC_sur_model.Rdata")
library(ROCR)
library(genefilter)
library(Hmisc)
library(e1071)
2.構(gòu)建支持向量機模型
2.1.切割數(shù)據(jù)
用R包caret切割數(shù)據(jù),生成的結(jié)果是一組代表列數(shù)的數(shù)字,用這些數(shù)字來給表達矩陣和meta取子集即可。
library(caret)
set.seed(12345679)
sam<- createDataPartition(meta$event, p = .5,list = FALSE)
train <- exprSet[,sam]
test <- exprSet[,-sam]
train_meta <- meta[sam,]
test_meta <- meta[-sam,]
2.2 train數(shù)據(jù)集建模
x=t(train)
y=as.factor(train_meta$event)
model = svm(x,y,kernel = "linear")
summary(model)
#>
#> Call:
#> svm.default(x = x, y = y, kernel = "linear")
#>
#>
#> Parameters:
#> SVM-Type: C-classification
#> SVM-Kernel: linear
#> cost: 1
#>
#> Number of Support Vectors: 176
#>
#> ( 108 68 )
#>
#>
#> Number of Classes: 2
#>
#> Levels:
#> 0 1
2.3.模型預測
用訓練集構(gòu)建模型,預測測試集的生死。不同于其他模型,這個預測結(jié)果是分類變量,直接預測生死,而不是prob。
x=t(test)
y=as.factor(test_meta$event)
pred = predict(model, x)
table(pred,y)
#> y
#> pred 0 1
#> 0 142 47
#> 1 41 28
*生信技能樹課程筆記