Skip to contents

step_nest() creates a specification of a recipe step that will convert specified data into a single model term, specifying the 'nest' that each row of the dataset corresponds to.

Usage

step_nest(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  names = NULL,
  lookup_table = NULL,
  skip = FALSE,
  id = recipes::rand_id("nest")
)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

One or more selector functions to choose variables. For step_nest, this indicates the variables which will not be nested. See recipes::selections() for more details.

role

For model terms created by this step, what analysis role should they be assigned? By default, the new columns created by this step from the original variables will be used as predictors in a model.

trained

A logical to indicate if the quantities for preprocessing have been estimated.

names

The names of the variables selected by ... are stored here once this preprocessing step has been trained by recipes::prep().

lookup_table

The table describing which values of your selected columns correspond to which unique nest id are stored here once this preprocessing step has been trained by recipes::prep().

skip

A logical. Should the step be skipped when the recipe is baked by recipes::bake()? While all operations are baked when recipes::prep() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations.

id

A character string that is unique to this step to identify it.

Value

An updated version of recipe with the new step added to the sequence of any existing operations.

Details

step_nest() will create a single nominal variable (named '.nest_id') from a set of variables (of any type). Every unique combination of the specified columns will receive a single nest id.

This recipe step is designed for use with nested models, since a model will be fitted on the data corresponding to each nest id. Using a recipe is often easier and more reliable than nesting the data manually.

The nest id corresponding to each unique combination of column values is decided when the recipe is prepped (if this recipe is contained in a workflow, this happens when the workflow is fitted). This means that when using a prepped recipe on new data (using recipes::prep() or workflows::predict.workflow()), all unique combinations of nesting columns must also exist in the training data. You will be warned if this is not the case. If you are using the 'rsample' package to create splits and this presents an issue, you may want to consider using nested_resamples().

step_nest() is designed so that nesting the transformed data by its '.nest_id' column is equivalent to the following action on the non-transformed data:

data %>%
  dplyr::group_by(...) %>% # '...' represents your specified terms
  tidyr::nest()

Tidying

When you tidy() this step, a tibble is returned showing how each unique value of the terms you have specified correspond to each nest id.

Case weights

The underlying operation does not allow for case weights.

Examples


library(recipes)

recipe <- recipe(example_nested_data, z ~ x + id) %>%
  step_nest(id)

recipe %>%
  prep() %>%
  bake(NULL)
#> # A tibble: 1,000 × 3
#>        x     z .nest_id
#>    <int> <dbl> <fct>   
#>  1    49  29.1 Nest 1  
#>  2    50  29.7 Nest 1  
#>  3    51  26.6 Nest 1  
#>  4    52  28.8 Nest 1  
#>  5    53  23.9 Nest 1  
#>  6    54  30.0 Nest 1  
#>  7    55  24.0 Nest 1  
#>  8    56  25.5 Nest 1  
#>  9    57  30.6 Nest 1  
#> 10    58  28.6 Nest 1  
#> # ℹ 990 more rows

recipe2 <- recipe(example_nested_data, z ~ x + id) %>%
  step_nest(-c(x, z))

recipe2 %>%
  prep() %>%
  bake(NULL)
#> # A tibble: 1,000 × 3
#>        x     z .nest_id
#>    <int> <dbl> <fct>   
#>  1    49  29.1 Nest 1  
#>  2    50  29.7 Nest 1  
#>  3    51  26.6 Nest 1  
#>  4    52  28.8 Nest 1  
#>  5    53  23.9 Nest 1  
#>  6    54  30.0 Nest 1  
#>  7    55  24.0 Nest 1  
#>  8    56  25.5 Nest 1  
#>  9    57  30.6 Nest 1  
#> 10    58  28.6 Nest 1  
#> # ℹ 990 more rows