插入包中的“随机森林”出错

时间:2015-03-09 12:41:20

标签: r random-forest r-caret

我在运行OS X 10.10.2(优胜美地)的机器上使用R-studio(版本0.98.994。)来应用Caret Package中的“随机森林”。这是我的代码:

library(caret)
data(iris)
inTrain <- createDataPartition(y=iris$Species, p=0.7, list=FALSE)
training <- iris[inTrain,]
testing <- iris[-inTrain,]

# Use o Random Forest do CARET
modFit <- train(Species ~ ., data=training, method="rf", prox=TRUE)
modFit

以下是错误:

Error in checkInstall(models$library) : 
Calls: <Anonymous> ... train.formula -> train -> train.default -> checkInstall

1 个答案:

答案 0 :(得分:2)

您错过了randomForest库。它是caret中建议的库之一,rf方法来自何处。安装后它应该像这样工作:

library(randomForest)
library(caret)

data(iris)
inTrain <- createDataPartition(y=iris$Species, p=0.7, list=FALSE)
training <- iris[inTrain,]
testing <- iris[-inTrain,]

# Use o Random Forest do CARET
modFit <- train(Species ~ ., data=training, method="rf", prox=TRUE)
modFit

输出:

> modFit
Random Forest 

105 samples
  4 predictor
  3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Bootstrapped (25 reps) 

Summary of sample sizes: 105, 105, 105, 105, 105, 105, ... 

Resampling results across tuning parameters:

  mtry  Accuracy  Kappa  Accuracy SD  Kappa SD
  2     0.949     0.923  0.0290       0.0436  
  3     0.953     0.929  0.0305       0.0460  
  4     0.948     0.921  0.0297       0.0447  

Accuracy was used to select the optimal model using  the largest value.
The final value used for the model was mtry = 3.