如何使用 R 中的机器学习和 Caret Package 在新数据集上测试调整后的 SVM 模型?

How to test your tuned SVM model on a new data-set using machine learning and Caret Package in R?

伙计们!

我是机器学习方法的新手,对此有疑问。我尝试在 R 中使用 Caret 包来启动此方法并使用我的数据集。

我有一个训练数据集 (Dataset1),其中包含关于我感兴趣的基因的突变信息,比方说 基因 A

数据集1中,我有关于基因A突变的信息,形式为MutNot-Mut。我使用 Dataset1SVM 模型 来预测输出(我选择 SVM 是因为它比 LVQ 或 GBM 更准确)。 因此,在我的第一步中,我将我的数据集分为训练组和测试组,因为我已经在数据集中获得了作为测试和训练集的信息。然后我完成了 10 倍的交叉验证。 我调整了我的模型并使用测试数据集(使用 ROC 曲线)评估了模型的性能。 一切顺利,直到这一步。

我有另一个数据集。 Dataset2 没有关于 Gene A 的突变信息。 我现在想做的是使用 Dataset2 上的 Dataset1 中的 tuned SVM 模型 到看看它是否能以 Mut/Not-Mut[ 的形式提供 数据集 2 中关于 基因 A 的突变信息。我已经阅读了 Caret 包指南,但我无法理解。我被困在这里,不知道该怎么办。

我不确定我是否选择了正确的 approach.Any 建议或帮助将不胜感激。

这是我从第一个数据集调整模型之前的代码。

从第一个数据集中选择训练和测试模型:

M_train <- Dataset1[Dataset1$Case=='train',-1] #creating train feature data frame

M_test <- Dataset1[Dataset1$Case=='test',-1] #creating test feature data frame

y=as.factor(M_train$Class) # Target variable for training


ctrl <- trainControl(method="repeatedcv", # 10fold cross validation
                     repeats=5, # do 5 repititions of cv
                     summaryFunction=twoClassSummary, # Use AUC to pick the best model
                     classProbs=TRUE)


#Use the expand.grid to specify the search space 
#Note that the default search grid selects 3 values of each tuning parameter

grid <- expand.grid(interaction.depth = seq(1,4,by=2), #tree depths from 1 to 4
                    n.trees=seq(10,100,by=10), # let iterations go from 10 to 100
                    shrinkage=c(0.01,0.1), # Try 2 values fornlearning rate 
                    n.minobsinnode = 20)


# Set up for parallel processing
#set.seed(1951)
registerDoParallel(4,cores=2)


#Train and Tune the SVM
svm.tune <- train(x=M_train,
                  y= M_train$Class,
                  method = "svmRadial",
                  tuneLength = 9, # 9 values of the cost function
                  preProc = c("center","scale"),
                  metric="ROC",
                  trControl=ctrl) # same as for gbm above

#Finally, assess the performance of the model using the test data set.

#Make predictions on the test data with the SVM Model
svm.pred <- predict(svm.tune,M_test)

confusionMatrix(svm.pred,M_test$Class)

svm.probs <- predict(svm.tune,M_test,type="prob") # Gen probs for ROC

svm.ROC <- roc(predictor=svm.probs$mut,
               response=as.factor(M_test$Class),
               levels=y))

plot(svm.ROC,main="ROC for SVM built with GA selected features")

所以,这就是我卡住的地方,我如何使用 svm.tune 模型来预测 基因 ADataset2[=51 中的突变=]?

提前致谢,

现在您只需使用您构建和调整的模型并使用 predict 对其进行预测:

D2.predictions <- predict(svm.tune, newdata = Dataset2)

它们的关键是确保您在此集合中拥有所有相同的预测变量,具有相同的列名(并且在我偏执的世界中以相同的顺序)。

D2.predictions 将包含您对未标记数据的预测 类。