# Random Forest
install.packages(c("randomForest","caret"))
# load the package
library(randomForest)
library(caret)
# load data
data(iris)

# Training and validation data
# define an 70%/30% train/test split of the dataset
split=0.70
trainIndex <- createDataPartition(iris$Species, p=split, list=FALSE)
data_train <- iris[ trainIndex,]
data_test  <- iris[-trainIndex,]

# fit model package random forest
fit <- randomForest(Species~., data=data_train,ntree=1000)
# summarize the fit
print(fit)
# make predictions
predictions <- predict(fit, data_test[,1:4])
# summarize accuracy
table(predictions, data_test$Species)


## fit model using caret and training control

# define training control
train_control <- trainControl(method="cv", number=10)

# define an 70%/30% train/test split of the dataset
split=0.70
trainIndex <- createDataPartition(iris$Species, p=split, list=FALSE)
data_train <- iris[ trainIndex,]
data_test  <- iris[-trainIndex,]

mod <- train(Species~., data = data_train, method="rf", trControl=train_control)

# make predictions
predictions <- predict(mod, data_test[,1:4]) 

# summarize results
confusionMatrix(predictions, data_test$Species)
