我正在尝试使用caret构建一个使用PCA作为预处理的预测模型。预处理步骤如下:
preProc <- preProcess(IL_train[,-1], method="pca", thresh = 0.8)
是否可以将thresh
参数直接传递给caret的train()
函数?我尝试了以下方法,但不起作用:
modelFit_pp <- train(IL_train$diagnosis ~ . , preProcess="pca", thresh= 0.8, method="glm", data=IL_train)
如果不行,我该如何将单独的preProc
结果传递给train()
函数?
回答:
根据文档说明,您可以通过trainControl
来指定额外的预处理参数
?trainControl...preProcOptions A list of options to pass to preProcess. The type of pre-processing (e.g. center, scaling etc) is passed in via the preProc option in train....
由于您的数据集无法重现,我们来看一个例子。我将使用mlbench
中的Sonar
数据集,并为了有趣而使用pls
算法。
library(caret)library(mlbench)data(Sonar)ctrl <- trainControl(preProcOptions = list(thresh = 0.95))mod <- train(Class ~ ., data = Sonar, method = "pls", trControl = ctrl)
虽然文档不是最激动人心的阅读内容,但一定要尝试浏览一下。包作者们努力创建文档,其中有很多值得探索的地方。