Skip to contents

generics::fit_xy() method for nested models. This should not be called directly and instead should be called by workflows::fit.workflow().

Usage

# S3 method for nested_model
fit_xy(
  object,
  x,
  y,
  case_weights = NULL,
  control = parsnip::control_parsnip(),
  ...
)

Arguments

object

An nested_model object (see nested()).

x

A data frame of predictors.

y

A data frame of outcome data.

case_weights

An optional vector of case weights. Passed into parsnip::fit.model_spec().

control

A parsnip::control_parsnip() object. Passed into parsnip::fit.model_spec().

...

Passed into parsnip::fit.model_spec(). Currently unused.

Value

A nested_model_fit object with several elements:

  • spec: The model specification object (the inner model of the nested model object)

  • fit: A tibble containing the model fits and the nests that they correspond to.

  • inner_names: A character vector of names, used to help with nesting the data during predictions.

Examples


library(dplyr)
library(parsnip)
library(recipes)
#> 
#> Attaching package: ‘recipes’
#> The following object is masked from ‘package:Matrix’:
#> 
#>     update
#> The following object is masked from ‘package:stats’:
#> 
#>     step
library(workflows)

data <- filter(example_nested_data, id %in% 11:20)

model <- linear_reg() %>%
  set_engine("lm") %>%
  nested()

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

wf <- workflow() %>%
  add_recipe(recipe) %>%
  add_model(model)

fit(wf, data)
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════
#> Preprocessor: Recipe
#> Model: nested_model()
#> 
#> ── Preprocessor ────────────────────────────────────────────────────────────────
#> 1 Recipe Step
#> 
#> • step_nest()
#> 
#> ── Model ───────────────────────────────────────────────────────────────────────
#> # A tibble: 10 × 2
#>    .nest_id .model_fit
#>    <fct>    <list>    
#>  1 Nest 1   <fit[+]>  
#>  2 Nest 2   <fit[+]>  
#>  3 Nest 3   <fit[+]>  
#>  4 Nest 4   <fit[+]>  
#>  5 Nest 5   <fit[+]>  
#>  6 Nest 6   <fit[+]>  
#>  7 Nest 7   <fit[+]>  
#>  8 Nest 8   <fit[+]>  
#>  9 Nest 9   <fit[+]>  
#> 10 Nest 10  <fit[+]>