WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

R · Advanced · question 55 of 100

What is the role of cross-validation in machine learning and how do you implement it in R?

📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

Cross-validation is an essential technique used in machine learning to evaluate the performance of a model on unseen data. The main idea behind cross-validation is to split the available data into two sets: a training set and a validation set. The model is trained on the training set and evaluated on the validation set, which serves as a proxy for new, unseen data.

There are several types of cross-validation techniques available, but the most common ones are k-fold cross-validation and leave-one-out cross-validation.

K-fold cross-validation involves splitting the data into k equally-sized subsets or "folds". The model is trained on k-1 of these folds and evaluated on the remaining fold. This process is repeated k times, with each fold serving as the validation set exactly once. The final performance metric is the average of the performance metrics obtained on each of the k validation sets.

Here is an example of how to perform k-fold cross-validation using the caret package in R:

    library(caret)
    data(iris)
    
    # define the control object for k-fold cross-validation
    ctrl <- trainControl(method = "cv", number = 5)
    
    # train the model using k-fold cross-validation
    model <- train(Species ~ ., data = iris, method = "rf", trControl = ctrl)
    
    # view the results
    print(model)

In this example, we are using the random forest algorithm (specified by the "rf" method) to predict the species of iris flowers based on their sepal length, sepal width, petal length, and petal width. We are using a 5-fold cross-validation strategy (specified by the number = 5 argument), and the resulting model is stored in the model object.

Leave-one-out cross-validation (LOOCV) is a special case of k-fold cross-validation where k is equal to the number of observations in the dataset. In LOOCV, the model is trained on all observations except one, and the performance is evaluated on the remaining observation. This process is repeated for each observation in the dataset, and the final performance metric is the average of the performance metrics obtained on each iteration.

Here is an example of how to perform LOOCV using the caret package in R:

    library(caret)
    data(iris)
    
    # define the control object for LOOCV
    ctrl <- trainControl(method = "LOOCV")
    
    # train the model using LOOCV
    model <- train(Species ~ ., data = iris, method = "rf", trControl = ctrl)
    
    # view the results
    print(model)

In this example, we are using the random forest algorithm (specified by the "rf" method) to predict the species of iris flowers based on their sepal length, sepal width, petal length, and petal width. We are using LOOCV (specified by the method = "LOOCV" argument), and the resulting model is stored in the model object.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic R interview — then scores it.
📞 Practice R — free 15 min
📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

All 100 R questions · All topics