Closed dchiu911 closed 5 years ago
library(caret) #> Warning: package 'caret' was built under R version 3.5.2 #> Loading required package: lattice #> Loading required package: ggplot2 #> Warning: package 'ggplot2' was built under R version 3.5.2 library(randomForest) #> randomForest 4.6-14 #> Type rfNews() to see new features/changes/bug fixes. #> #> Attaching package: 'randomForest' #> The following object is masked from 'package:ggplot2': #> #> margin ntree <- 33 set.seed(321) trainData <- twoClassSim(5000, linearVars = 3, noiseVars = 9) testData <- twoClassSim(5000, linearVars = 3, noiseVars = 9) set.seed(432) mySeeds <- c(sapply(simplify = FALSE, 1:25, function(u) sample(10 ^ 4, 3)), 2019) cvCtrl <- trainControl(method = "repeatedcv", number = 5, repeats = 5, classProbs = TRUE, summaryFunction = twoClassSummary, seeds = mySeeds) fitRFcaret <- train(Class ~ ., data = trainData, trControl = cvCtrl, ntree = ntree, method = "rf", metric = "ROC") set.seed(tail(mySeeds, 1)[[1]]) fitRFmanual <- randomForest(Class ~ ., data = trainData, mtry = fitRFcaret$bestTune$mtry, ntree = ntree) all.equal(as.vector(predict(fitRFcaret, testData)), as.vector(predict(fitRFmanual, testData))) #> [1] TRUE
Created on 2019-05-29 by the reprex package (v0.3.0)
Created on 2019-05-29 by the reprex package (v0.3.0)