没有您的数据或可重复的示例,很难真正改进您的模型。我可以向您建议一些程序和软件包,它们可以在此类任务中为您提供很多帮助。
看看caret 包,它专为模型调整而设计。这个包真的很好documented 有很多有用的例子。在这里,我可以展示使用 caret 的一般工作流程:
#load library and the data for this example
library(caret)
#this is a caret built-in dataset
data(GermanCredit)
df <- GermanCredit[,1:10]
str(GermanCredit)
#caret offers useful function for data splitting. Here we split the data according to
#the class column (our outcome to be predict), in 80% training and 20% testing data
ind <- createDataPartition(df$Class,p=0.8,list = F)
training <- df[ind,]
test <- df[-ind,]
#here we set the resampling method for hyperparameters tuning
#in this case we choose 10-fold cross validation
cn <- trainControl(method = "cv",number = 10)
#the grid of hyperparameters with which to tune the model
grid <- expand.grid(mtry=2:(ncol(training)-1))
#here is the proper model fitting. We fit a random forests model (method="rf") using
#Class as outcome and all other variables as predictors, using the selected resampling
#method and tuning grid
fit <-
train(
Class ~ .,
data = training,
method = "rf",
trControl = cn,
tuneGrid = grid
)
模型的输出如下所示:
Random Forest
800 samples
9 predictor
2 classes: 'Bad', 'Good'
No pre-processing
Resampling: Cross-Validated (10 fold)
Summary of sample sizes: 720, 720, 720, 720, 720, 720, ...
Resampling results across tuning parameters:
mtry Accuracy Kappa
2 0.71125 0.1511164
3 0.70875 0.1937589
4 0.70000 0.1790469
5 0.70000 0.1819945
6 0.70375 0.1942889
7 0.70250 0.1955456
8 0.70625 0.2025015
9 0.69750 0.1887295
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mtry = 2.
如您所见,函数train 为每个调整参数值(在本例中仅mtry)构建了一个randomForests 模型,并根据模型选择最佳参数设置以最大精度。最终的参数设置用于构建最终模型,所有数据都提供给train(在这种情况下,所有观察到的training data.frame)。输出给出了重采样性能,这通常是乐观的。要针对test 集测试模型的准确性,我们可以这样做:
#predict the output on the test set.
p <- predict(fit,test[,-10])
#this function built a confusion matrix and calculate a lot of accuracies statistics
confusionMatrix(p,test$Class)
您可以使用train 的... 参数将所选模型的特定参数(在本例中为randomForest)添加到函数中。像这样:
fit <-
train(
Class ~ .,
data = training,
method = "rf",
trControl = cn,
tuneGrid = grid,
ntrees=200# grown 200 trees
)
要找到最好的变量集(也称为变量选择或特征选择)caret 有很多有用的功能。包中有variable selection 的整个小插图部分,包括简单的过滤器、反向选择、递归特征消除、遗传算法、模拟退火,显然还有许多模型的内置特征选择方法(如变量重要性对于randomForest)。然而,特征选择是一个很大的话题,我建议你从caret包中的方法开始,如果你没有找到你要找的东西,请深入挖掘。