Get startedGet started for free

5-fold cross-validation

In this course, you will use a wide variety of datasets to explore the full flexibility of the caret package. Here, you will use the famous Boston housing dataset, where the goal is to predict median home values in various Boston suburbs.

You can use exactly the same code as in the previous exercise, but change the dataset used by the model:

model <- train(
  medv ~ ., 
  Boston, # <- new!
  method = "lm",
  trControl = trainControl(
    method = "cv", 
    number = 10,
    verboseIter = TRUE
  )
)

Next, you can reduce the number of cross-validation folds from 10 to 5 using the number argument to the trainControl() argument:

trControl = trainControl(
  method = "cv", 
  number = 5,
  verboseIter = TRUE
)

This exercise is part of the course

Machine Learning with caret in R

View Course

Exercise instructions

  • Fit an lm() model to the Boston housing dataset, such that medv is the response variable and all other variables are explanatory variables.
  • Use 5-fold cross-validation rather than 10-fold cross-validation.
  • Print the model to the console and inspect the results.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Fit lm model using 5-fold CV: model
model <- train(
  ___, 
  ___,
  method = "lm",
  trControl = trainControl(
    method = "cv", 
    number = ___,
    verboseIter = TRUE
  )
)

# Print model to console
Edit and Run Code