rtidymodels

Why Is My Tidymodels Recipe Not Working When I Use Interaction Effects?


I'm working through trying to understand tidymodels and am running into an issue where my recipe won't allow interaction effects and returns an error. The code I use is this:

library(tidyverse)
library(ISLR)
data("Auto")
library(tidymodels)

auto_data <- initial_split(Auto, strata = origin)

auto_train <- training(auto_data)

auto_test <- testing(auto_data)

recipe <- recipe(mpg ~ horsepower * origin, data = auto_train)

And the error I get is: Error in inline_check():

! No in-line functions should be used here; use steps to define baking actions. Run rlang::last_trace() to see where the error occurred.

Why won't the recipe let me use interaction effects? I get no such error when I do:

recipe <- recipe(mpg ~ horsepower + origin, data = auto_train)

Solution

  • From the documentation on ?recipe for the formula argument:

    A model formula. No in-line functions should be used here (e.g. log(x), x:y, etc.) and minus signs are not allowed. These types of transformations should be enacted using step functions in this package. Dots are allowed as are simple multivariate outcome terms (i.e. no need for cbind; see Examples). A model formula may not be the best choice for high-dimensional data with many columns, because of problems with memory.

    Note that it explicitly says not to specify interactions (x:y) in the formula.

    In other words, the intent is for you to specify interactions explicitly with a designated step_interaction rather than in the formula.