broom

repository·main·Indexed 23 days ago

https://github.com/tidymodels/broom

A part of the tidymodels ecosystem that summarizes model information into tidy tibbles. It provides a consistent interface to extract model components via tidy(), model-wide statistics via glance(), and observation-level predictions or residuals via augment() across various modeling packages.

Tokens
1.3K
Snippets
4
Records
5
Agent score
31%

What's inside broom

  1. How broom works: tidy(), glance(), and augment()

    main

    broom summarizes model information into tidy tibble()s using three primary verbs:

    1. tidy(): Summarizes information about model components (e.g., regression coefficients). Each row represents an important component.
    2. glance(): Reports information about the entire model (e.g., goodness-of-fit measures). It returns a single-row tibble.
    3. augment(): Adds information about observations (e.g., fitted values, residuals) to the original dataset. New columns are prefixed with . to avoid overwriting existing data.
  2. Install broom

    main

    You can install broom from CRAN, as part of the tidymodels modeling set, or install the development version from GitHub using pak.

    To install the full tidymodels suite (recommended):

    install.packages("tidymodels")

    To install only broom:

    install.packages("broom")

    To install the development version from GitHub:

    install.packages("pak")
    pak::pak("tidymodels/broom")
    install.packages("tidymodels")
  3. Use glance() to report model fitness

    main

    Use glance() to return a tibble with exactly one row containing goodness-of-fit measures and related statistics. This is useful for checking model misspecification and comparing multiple models.

    library(broom)
    
    fit <- lm(Volume ~ Girth + Height, trees)
    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.948         0.944  3.88      255. 1.07e-18     2  -84.5  177.  183.
    #> # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
  4. Use tidy() to summarize model components

    main

    Use tidy() to produce a tibble() where each row contains information about an important component of the model, such as regression coefficients. This is useful for inspecting models or creating custom visualizations.

    library(broom)
    
    fit <- lm(Volume ~ Girth + Height, trees)
    tidy(fit)
    #> # A tibble: 3 × 5
    #>   term        estimate std.error statistic  p.value
    #>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
    #> 1 (Intercept)  -58.0       8.64      -6.71 2.75e- 7
    #> 2 Girth          4.71      0.264     17.8  8.22e-17
    #> 3 Height         0.339     0.130      2.61 1.45e- 2
  5. Use augment() to add model observations to a dataset

    main

    Use augment() to add columns to a dataset containing information such as fitted values, residuals, or cluster assignments. All added columns are prefixed with . to prevent overwriting existing columns.

    library(broom)
    
    fit <- lm(Volume ~ Girth + Height, trees)
    augment(fit, data = trees)
    #> # A tibble: 31 × 9
    #>    Girth Height Volume .fitted .resid   .hat .sigma   .cooksd .std.resid
    #>    <dbl>  <dbl>   <dbl>   <dbl>  <dbl>  <dbl>    <dbl>     <dbl>      <dbl>
    #>  1   8.3     70   10.3    4.84  5.46  0.116    3.79 0.0978        1.50  
    #>  2   8.6     65   10.3    4.55  5.75  0.147    3.77 0.148         1.60  
    #>  3   8.8     63   10.2    4.82  5.38  0.177    3.78 0.167         1.53  
    #>  4  10.5     72   16.4   15.9   0.526 0.0592   3.95 0.000409      0.140 
    #>  5  10.7     81   18.8   19.9  -1.07  0.121    3.95 0.00394      -0.294 
    #>  6  10.8     83   19.7   21.0  -1.32  0.156    3.94 0.00840      -0.370 
    #>  7  11       66   15.6   16.2  -0.593 0.115    3.95 0.00114      -0.162 
    #>  8  11       75   18.2   19.2  -1.05  0.0515   3.95 0.00138      -0.277 
    #>  9  11.1     80   22.6   21.4   1.19  0.0920   3.95 0.0348       0.321 
    #> 10  11.2     75   19.9   20.2  -0.288 0.0480   3.95 0.0000968    -0.0759
    #> # ℹ 21 more rows