Train Function In R: Fit Predict Model

train function in r

Today, we will look at how to use the train function in R language. The train function in R uses linear regression training. So, this article will demonstrate how to use the train function in R from the mlbench and caret library.

The syntax of the train function

train(x, y, ...)

Parameters:

  • x: A data frame, matrix, …
  • y: A vector or number

The example

If you want to use the train function in R, we must first install the caret library and other essential packages before we can utilize the train function. If you don’t know how to install the package, you can click this link here. Or you can run the code below:

if(!require('caret')) {
    install.packages('caret')
    library('caret')
}

if(!require('mlbench')) {
    install.packages('mlbench')
    library('mlbench')
}

We can use these packages when they have been installed. Here, we will use dataset iris to the train function:

# Import packages
library(mlbench)
library(caret)

# Load iris dataset
data <- data(iris)

# View the iris dataset (first 6 lines)
cat('iris is:\n')
head(iris)

The next step is a generation of the object by the dataset iris in R.

# Create Binary Object
iris$binary  <- ifelse(iris$Species != "setosa", 0, 1)
iris$Species <- NULL

Finally, we may adjust the data model using the train function. It will accept the binary object that we produced before.

# The train function
irisTrain <- train(binary ~.,
                 data=iris,
                 method = "knn",
                 family="binomial",
                 trControl = ctrl)
irisTrain

Output

+ Fold1.Rep1: k=5 
- Fold1.Rep1: k=5 
+ Fold1.Rep1: k=7 
- Fold1.Rep1: k=7 
+ Fold1.Rep1: k=9 
- Fold1.Rep1: k=9 
... 
+ Fold4.Rep1: k=9 
- Fold4.Rep1: k=9 
Aggregating results
Selecting tuning parameters
Fitting k = 9 on full training set

The model is now being trained and displayed as follows:

# View first 6 lines
head(irisTrain$resample)

You can see full code below:

# Install packages
if(!require('caret')) {
  install.packages('caret')
  library('caret')
}

if(!require('mlbench')) {
  install.packages('mlbench')
  library('mlbench')
}

# Import packages
library(caret)
library(mlbench)

# Load iris dataset
data <- data(iris)

# View the iris dataset (first 10 lines)
cat('iris is: \n')
head(iris, n =10)

# Create Binary Object
iris$binary  <- ifelse(iris$Species != "setosa", 0, 1)
iris$Species <- NULL

# The train function
irisTrain <- train(data=iris, binary ~.,
                   family="binomial",
                   method = "knn",
                   trControl = ctrl)

# View first 6 lines
head(irisTrain$resample)

Output

  RMSE Rsquared MAE k   Resample
1    0        1   0 5 Fold1.Rep1
2    0        1   0 7 Fold1.Rep1
3    0        1   0 9 Fold1.Rep1
4    0        1   0 5 Fold2.Rep1
5    0        1   0 7 Fold2.Rep1
6    0        1   0 9 Fold2.Rep1

Another example you can refer to better understand this function:

data(iris)
# Import library
library(caret)
library(mlbench)

# Get data
trainData <- iris[,1:3]
trainClasses <- iris[,5]

# Train data
trainFit1 <- train(trainData, trainClasses,
                   tuneLength = 10,
                   method = "knn",
                   trControl = trainControl(method = "cv"),
                   preProcess = c("center", "scale"))

# View train
cat('View train 1:\n')
head(trainFit1$resample)

# Train data
trainFit2 <- train(trainData, trainClasses,
                    preProcess = c("center", "scale"),
                    method = "knn",
                    trControl = trainControl(method = "boot"),
                    tuneLength = 10)

# View train
cat('View train 2:\n')
head(trainFit2$resample)

Output

View train 1:
   Accuracy Kappa Resample
1 0.8666667   0.8   Fold02
2 0.9333333   0.9   Fold01
3 0.9333333   0.9   Fold05
4 1.0000000   1.0   Fold06
5 0.8666667   0.8   Fold10
6 0.8000000   0.7   Fold04
View train 2:
        RMSE  Rsquared         MAE   Resample
1 0.03030148 0.9962124 0.003978780 Resample01
2 0.05042729 0.9887703 0.006993007 Resample05
3 0.01178511 0.9993971 0.001666667 Resample09
4 0.00000000 1.0000000 0.000000000 Resample04
5 0.01183536 0.9994025 0.001540832 Resample08
6 0.00000000 1.0000000 0.000000000 Resample12

Summary

Above is what I can share with you about the train function in R. If you have any questions, please leave a comment below. I will answer as possible. Thank you for reading!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *