

Let’s learn how to add resampling to our model by adding the parameter trControl (train control) to our train() function. The function train() has other optional parameters. That’s how you can use the function train() to create different basic models. , data = mtcars, method = "ridge") # Try using "lasso" , data = mtcars, method = "lm") # Ridge regression model model <- train(mpg ~. library(caret) # Simple linear regression model (lm means linear model) model <- train(mpg ~ wt, data = mtcars, method = "lm") # Multiple linear regression model model <- train(mpg ~. If you want to pass all attributes you can write it as “ x ~. The formula can be written as “ x ~ y, z, w” where x is the dependent variable, mpg, in our case, and y, z and w are independent variables. Now, let’s create regression models to predict how many miles per gallon (mpg) a car model can reach based on the other attributes. ?mtcars # Get more information about this dataset data(mtcars) # Load the dataset head(mtcars) am: Transmission (0 = automatic, 1 = manual).The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).įormat A data frame with 32 observations on 11 variables. Below, there is an explanation about this dataset: Motor Trend Car Road Tests (mtcars) Description In this tutorial, I’m using the mtcars dataset. The method parameter is a string specifying which classification or regression model to use. I’ll explain more about how to write your formula below. The formula parameter is where you specify what is your dependent variable (what you want to predict) and independent variables (features). The train() function has three basic parameters: As its name suggests, it is used to train a model, that is, to apply an algorithm to a set of data and create a model which represents that dataset.

The function train() is a core function of caret. We’re gonna do that by using the train() function.

install.packages("caret") Creating a simple model
#Caret logistic regression install
If you’re using RStudio (which is recommended), you can also install it by clicking on “tools” > “Install Packages…” in the toolbar. Installing caret is just as simple as installing any other package in R.
