parsnip set_engine() in R: Choose the Model Engine
The parsnip set_engine() function in R declares which package or system actually fits a model. You pass a model specification and an engine name, and parsnip routes the fit to that backend, such as lm, glmnet, or ranger.
linear_reg() |> set_engine("lm") # base R backend
linear_reg() |> set_engine("glmnet") # penalized regression
rand_forest() |> set_engine("ranger") # fast random forest
rand_forest() |> set_engine("randomForest") # classic implementation
boost_tree() |> set_engine("xgboost") # gradient boosting
set_engine(spec, "ranger", importance = "impurity") # engine-specific arg
spec |> set_engine("lm") |> translate() # inspect the engine callNeed explanation? Read on for examples and pitfalls.
What set_engine() does
set_engine() picks the software that powers a parsnip model. A model specification such as rand_forest() describes the kind of model you want. It says nothing about how that model is computed. set_engine() fills that gap by naming the engine, the concrete implementation that does the math.
The same model type often has several engines. A random forest can run on ranger, randomForest, spark, or h2o. Each is a different package with its own speed, defaults, and extra options. set_engine() lets you switch between them without rewriting the rest of your code.
The function returns an updated specification, so it fits naturally into a pipe. Nothing is computed yet. set_engine() only records your choice, and the engine package is loaded later when you call fit().
set_mode() says regression or classification, and set_engine() says which package computes it. Keeping them apart is what lets tidymodels swap one engine for another with a single line.set_engine() syntax and arguments
set_engine() takes a spec, an engine name, and optional engine arguments. The signature is short, but the ... slot is where engine-specific tuning happens.
The object argument is the spec you built with a model function like linear_reg() or boost_tree(). The engine argument is a single character string, such as "lm" or "xgboost".
The ... argument passes options straight to the engine. Anything you place there is handed to the underlying fitting function unchanged. For example, set_engine("ranger", importance = "impurity") forwards importance to ranger::ranger(). These names come from the engine package, not from parsnip.
Choose an engine: four examples
Every example below builds a spec and inspects it. Building a spec needs only parsnip, so the code runs anywhere even before an engine package is installed.
Example 1: Set the lm engine and fit
The lm engine needs no extra package, so it is the simplest case. Build the linear regression spec, set the engine, then fit on mtcars.
The printed spec confirms the engine before any data is touched. After fit(), the coefficients come straight from stats::lm().
Example 2: Switch engines without changing the model
The same model type accepts a different engine in one line. Here linear_reg() moves from lm to glmnet for penalized regression.
Only the engine string changed. The model type, the formula, and the rest of your pipeline stay identical, which makes engine comparison cheap.
Example 3: Pass engine-specific arguments
Use ... to forward options that only one engine understands. A ranger random forest can compute variable importance, so pass importance through set_engine().
The print output separates main arguments from engine-specific ones. trees is a parsnip argument shared by every random forest engine, while importance belongs to ranger alone.
Example 4: Discover engines and inspect the call
show_engines() lists every engine a model type supports. Pair it with translate() to see the exact backend call your spec will generate.
translate() reveals that the lm engine maps to stats::lm(). Use it whenever you want to confirm which function will run before you commit to a fit.
show_engines("rand_forest") returns the valid options, which saves you from typos like "RandomForest" that parsnip will reject.set_engine() vs set_mode() vs set_args()
set_engine() is one of three verbs that finish a model specification. Each adjusts a different part of the spec, and they all return an updated spec for the pipe.
| Function | Sets | Example |
|---|---|---|
set_engine() |
The computational backend | set_engine("ranger") |
set_mode() |
Regression or classification | set_mode("classification") |
set_args() |
Main model arguments | set_args(trees = 1000) |
The decision rule is direct. Use set_engine() to choose or change the package that fits the model. Use set_mode() when a dual-mode model like decision_tree() needs to know whether to predict a class or a number. Use set_args() to update tuning parameters after the spec already exists.
method = "ranger". parsnip splits that into a model function plus set_engine(), so one model type can reach many engines.Common pitfalls
Two mistakes account for most set_engine() trouble. Both are easy to avoid once you know the pattern.
The first is relying on the default engine. If you skip set_engine(), parsnip quietly picks a default, which may not be the package you expected. Being explicit removes the surprise.
The second pitfall is a missing engine package. set_engine("ranger") succeeds even when ranger is not installed, because the spec is only a description. The error appears later at fit() time, asking you to install the package.
xgboost or glmnet records your choice, but the package must be installed before fit() runs. A missing package fails at fit time, not when you call set_engine().Try it yourself
Try it: Build a rand_forest() spec with 300 trees, set the mode to regression, and set the engine to ranger with importance = "permutation". Save the spec to ex_spec.
Click to reveal solution
Explanation: set_engine() names the ranger backend and forwards importance through ... as an engine-specific argument. The spec records everything, and no package loads until fit() runs.
Related parsnip functions
set_engine() works with the rest of the parsnip specification verbs. These functions cover the neighboring steps of building and fitting a model.
set_mode()declares regression or classification for dual-mode models.set_args()updates main model arguments on an existing spec.show_engines()lists every engine available for a model type.translate()shows the exact engine call a spec will generate.fit()trains the spec once the engine and mode are set.
FAQ
What does set_engine() do in R?
set_engine() tells a parsnip model specification which package or system will fit the model. The same model type, such as rand_forest(), can run on several engines like ranger or randomForest. set_engine() records your choice and returns an updated spec. No computation happens until you call fit(), at which point parsnip loads the engine package and routes the fitting work to it.
What is the difference between set_engine() and set_mode()?
They configure different parts of a spec. set_engine() chooses the computational backend, the package that does the math. set_mode() chooses the prediction type, either regression or classification. A model like decision_tree() supports both modes, so it needs set_mode() before fitting. Single-purpose models such as linear_reg() set the mode automatically, but every model still needs an engine.
Do I need set_engine() if there is a default engine?
You can skip it, since parsnip fills in a default engine. Relying on the default is risky, though, because the default may change between package versions or differ from what you assume. Calling set_engine() explicitly documents the backend in your code and protects against silent surprises. It costs one line and makes the model specification self-explanatory for anyone reading it later.
How do I see which engines a model supports?
Call show_engines() with the model type as a string, for example show_engines("rand_forest"). It returns a tibble of every registered engine and the modes each one supports. This is the reliable way to find valid engine names, since the strings are exact and case-sensitive. Checking first avoids typos that set_engine() would otherwise reject when you try to fit.
Can I pass arguments to the engine through set_engine()?
Yes. Any named argument in the ... slot of set_engine() is forwarded straight to the underlying fitting function. For example, set_engine("ranger", importance = "impurity") passes importance to ranger::ranger(). These argument names come from the engine package itself, not from parsnip, so check the engine documentation for the options it accepts.
For the full argument reference, see the parsnip set_engine() documentation.