library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
library(kernlab)
## 
## Attaching package: 'kernlab'
## The following object is masked from 'package:ggplot2':
## 
##     alpha
data(swiss)

inTrain <- createDataPartition(y = swiss$Fertility, p = 0.80, list = FALSE)
trainingData <- swiss[inTrain,]
testingData <- swiss[-inTrain,]

dim(trainingData)
## [1] 39  6
dim(testingData)
## [1] 8 6
set.seed(13)
model <- train(Fertility ~., data = trainingData, method = "rf")

model
## Random Forest 
## 
## 39 samples
##  5 predictor
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 39, 39, 39, 39, 39, 39, ... 
## Resampling results across tuning parameters:
## 
##   mtry  RMSE      Rsquared   MAE     
##   2     8.582087  0.6197510  7.067163
##   3     8.679048  0.5984455  7.171887
##   5     8.930488  0.5612741  7.343815
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was mtry = 2.
prediction <- predict(model, newdata = testingData)

confusionMatrix(
  factor(prediction, levels = 1:10),
  factor(testingData$Fertility, levels = 1:10)
)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction 1 2 3 4 5 6 7 8 9 10
##         1  0 0 0 0 0 0 0 0 0  0
##         2  0 0 0 0 0 0 0 0 0  0
##         3  0 0 0 0 0 0 0 0 0  0
##         4  0 0 0 0 0 0 0 0 0  0
##         5  0 0 0 0 0 0 0 0 0  0
##         6  0 0 0 0 0 0 0 0 0  0
##         7  0 0 0 0 0 0 0 0 0  0
##         8  0 0 0 0 0 0 0 0 0  0
##         9  0 0 0 0 0 0 0 0 0  0
##         10 0 0 0 0 0 0 0 0 0  0
## 
## Overall Statistics
##                                   
##                Accuracy : NaN     
##                  95% CI : (NA, NA)
##     No Information Rate : NA      
##     P-Value [Acc > NIR] : NA      
##                                   
##                   Kappa : NaN     
##                                   
##  Mcnemar's Test P-Value : NA      
## 
## Statistics by Class:
## 
##                      Class: 1 Class: 2 Class: 3 Class: 4 Class: 5 Class: 6
## Sensitivity                NA       NA       NA       NA       NA       NA
## Specificity                NA       NA       NA       NA       NA       NA
## Pos Pred Value             NA       NA       NA       NA       NA       NA
## Neg Pred Value             NA       NA       NA       NA       NA       NA
## Prevalence                NaN      NaN      NaN      NaN      NaN      NaN
## Detection Rate            NaN      NaN      NaN      NaN      NaN      NaN
## Detection Prevalence      NaN      NaN      NaN      NaN      NaN      NaN
## Balanced Accuracy          NA       NA       NA       NA       NA       NA
##                      Class: 7 Class: 8 Class: 9 Class: 10
## Sensitivity                NA       NA       NA        NA
## Specificity                NA       NA       NA        NA
## Pos Pred Value             NA       NA       NA        NA
## Neg Pred Value             NA       NA       NA        NA
## Prevalence                NaN      NaN      NaN       NaN
## Detection Rate            NaN      NaN      NaN       NaN
## Detection Prevalence      NaN      NaN      NaN       NaN
## Balanced Accuracy          NA       NA       NA        NA
#confusionMatrix(prediction,testingData$Fertility)

plot(model$finalModel)

plot(varImp(model), top = 10)