Skip to contents

fit.model_spec() takes a nested model specification and fits the inner model specification to each nested data frame in the given dataset.

Usage

# S3 method for nested_model
fit(
  object,
  formula,
  data,
  case_weights = NULL,
  control = parsnip::control_parsnip(),
  ...
)

Arguments

object

An nested_model object (see nested()).

formula

An object of class formula. Passed into parsnip::fit.model_spec(). This should not contain the variable to nest by.

data

A data frame. If used with a 'nested_model' object, the data frame must already be nested.

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(parsnip)
library(tidyr)

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

nested_data <- nest(example_nested_data, data = -id)

fit(model, z ~ x + y + a + b, nested_data)
#> Nested model fit, with 20 inner models
#> # A tibble: 20 × 2
#>       id .model_fit
#>    <int> <list>    
#>  1     1 <fit[+]>  
#>  2     2 <fit[+]>  
#>  3     3 <fit[+]>  
#>  4     4 <fit[+]>  
#>  5     5 <fit[+]>  
#>  6     6 <fit[+]>  
#>  7     7 <fit[+]>  
#>  8     8 <fit[+]>  
#>  9     9 <fit[+]>  
#> 10    10 <fit[+]>  
#> 11    11 <fit[+]>  
#> 12    12 <fit[+]>  
#> 13    13 <fit[+]>  
#> 14    14 <fit[+]>  
#> 15    15 <fit[+]>  
#> 16    16 <fit[+]>  
#> 17    17 <fit[+]>  
#> 18    18 <fit[+]>  
#> 19    19 <fit[+]>  
#> 20    20 <fit[+]>