Skip to contents

Use broom functions on fitted nested models.

tidy.nested_model_fit() summarises components of each model within a nested model fit, indicating which nested data frame each row corresponds to.

glance.nested_model_fit() summarises a nested model, returning a tibble::tibble() with 1 row.

glance_nested() summarises each model within a nested model fit, returning a tibble::tibble() with the same number of rows as the number of inner models.

Usage

# S3 method for nested_model_fit
tidy(x, ...)

# S3 method for nested_model_fit
glance(x, ...)

glance_nested(x, ...)

Arguments

x

A nested_model_fit object produced by fit.nested_model().

...

Additional arguments passed into their respective functions. (e.g. for tidy.nested_model_fit(), parsnip::tidy.model_fit()).

Value

A tibble::tibble(). With glance.nested_model_fit(), the tibble will have 1 row.

Details

generics::glance() states that glance() methods should always return 1 row outputs for non-empty inputs. The 'nestedmodels' package is no exception: glance() methods will combine rows to produce a result with a single row. Specifically:

  • If a column contains 1 unique value, that value is used.

  • If a column is numeric, the mean will be calculated.

  • Otherwise, the results will be combined into a list.

Examples


library(dplyr)
library(parsnip)
library(broom)

data <- filter(example_nested_data, id %in% 1:5)

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

fit <- fit(
  model, z ~ x + y + a + b,
  group_by(data, id)
)

tidy(fit)
#> # A tibble: 25 × 6
#>       id term         estimate std.error statistic   p.value
#>    <int> <chr>           <dbl>     <dbl>     <dbl>     <dbl>
#>  1     1 (Intercept)  42.0       14.1        2.99  0.00453  
#>  2     1 x            -0.127      0.181     -0.704 0.485    
#>  3     1 y             0.105      0.0435     2.41  0.0200   
#>  4     1 a             0.0627     0.0920     0.681 0.499    
#>  5     1 b            -0.172      0.108     -1.59  0.118    
#>  6     2 (Intercept) -96.7       28.0       -3.45  0.00122  
#>  7     2 x             0.748      0.173      4.32  0.0000837
#>  8     2 y            -0.00751    0.0405    -0.186 0.854    
#>  9     2 a            -0.0695     0.0840    -0.827 0.412    
#> 10     2 b             0.0699     0.0849     0.824 0.414    
#> # ℹ 15 more rows
glance(fit)
#> # A tibble: 1 × 12
#>   r.squared adj.r.squared sigma statistic p.value    df logLik   AIC   BIC
#>       <dbl>         <dbl> <dbl>     <dbl>   <dbl> <dbl>  <dbl> <dbl> <dbl>
#> 1     0.184         0.111  14.4      2.76   0.168     4  -199.  410.  422.
#> # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
glance_nested(fit)
#> # A tibble: 5 × 13
#>      id r.squared adj.r.squared sigma statistic p.value    df logLik   AIC   BIC
#>   <int>     <dbl>         <dbl> <dbl>     <dbl>   <dbl> <dbl>  <dbl> <dbl> <dbl>
#> 1     1    0.161        0.0861  18.0       2.15 8.95e-2     4  -213.  438.  449.
#> 2     2    0.359        0.302   15.3       6.30 4.12e-4     4  -205.  421.  433.
#> 3     3    0.218        0.149   12.9       3.14 2.31e-2     4  -196.  404.  416.
#> 4     4    0.0867       0.00554  7.25      1.07 3.83e-1     4  -167.  347.  358.
#> 5     5    0.0934       0.0128  18.5       1.16 3.42e-1     4  -214.  440.  452.
#> # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>