Code Monkey home page Code Monkey logo

Comments (3)

larmarange avatar larmarange commented on July 19, 2024

The purpose of tidy_add_variable_labels() is and only is to add the appropriate variable labels.
If it was not called before in the pipeline, tidy_add_variable_labels() calls tidy_identify_variables() whose purpose is to identify the corresponding variable of each term.

Regarding extracting the factor level, it is the purpose of tidy_add_term_labels().

You also have tidy_add_reference_rows() to add a row with the reference level.

In broom.helpers, each single operation is done by a different function, and all these functions can be combined in a pipeline.

Most users do not need these individual functions and would rather use tidy_plus_plus() which combines all features of broom.helpers in one function (see documentation for the full list of arguments).

See examples below

I'm closing the issue as the feature you refer to is already implemented. But feel free to reopen if you identify something missing.

library(broom.helpers)
mtcars$f_cyl <- factor(mtcars$cyl)
m <- lm(mpg ~ f_cyl, mtcars)

tidy_and_attach(m) |> 
  tidy_add_variable_labels() 
#> # A tibble: 3 × 12
#>   term      variable var_label var_class var_type var_nlevels estimate std.error
#>   <chr>     <chr>    <chr>     <chr>     <chr>          <int>    <dbl>     <dbl>
#> 1 (Interce… (Interc… (Interce… <NA>      interce…          NA    26.7      0.972
#> 2 f_cyl6    f_cyl    f_cyl     factor    categor…           3    -6.92     1.56 
#> 3 f_cyl8    f_cyl    f_cyl     factor    categor…           3   -11.6      1.30 
#> # ℹ 4 more variables: statistic <dbl>, p.value <dbl>, conf.low <dbl>,
#> #   conf.high <dbl>

tidy_and_attach(m) |> 
  tidy_add_term_labels()
#> # A tibble: 3 × 15
#>   term        variable    var_label   var_class var_type   var_nlevels contrasts
#>   <chr>       <chr>       <chr>       <chr>     <chr>            <int> <chr>    
#> 1 (Intercept) (Intercept) (Intercept) <NA>      intercept           NA <NA>     
#> 2 f_cyl6      f_cyl       f_cyl       factor    categoric…           3 contr.tr…
#> 3 f_cyl8      f_cyl       f_cyl       factor    categoric…           3 contr.tr…
#> # ℹ 8 more variables: contrasts_type <chr>, label <chr>, estimate <dbl>,
#> #   std.error <dbl>, statistic <dbl>, p.value <dbl>, conf.low <dbl>,
#> #   conf.high <dbl>

tidy_and_attach(m) |> 
  tidy_add_reference_rows() |> 
  tidy_add_term_labels()
#> # A tibble: 4 × 16
#>   term        variable    var_label   var_class var_type   var_nlevels contrasts
#>   <chr>       <chr>       <chr>       <chr>     <chr>            <int> <chr>    
#> 1 (Intercept) (Intercept) (Intercept) <NA>      intercept           NA <NA>     
#> 2 f_cyl4      f_cyl       f_cyl       factor    categoric…           3 contr.tr…
#> 3 f_cyl6      f_cyl       f_cyl       factor    categoric…           3 contr.tr…
#> 4 f_cyl8      f_cyl       f_cyl       factor    categoric…           3 contr.tr…
#> # ℹ 9 more variables: contrasts_type <chr>, reference_row <lgl>, label <chr>,
#> #   estimate <dbl>, std.error <dbl>, statistic <dbl>, p.value <dbl>,
#> #   conf.low <dbl>, conf.high <dbl>

tidy_plus_plus(m)
#> # A tibble: 3 × 17
#>   term   variable var_label var_class var_type    var_nlevels contrasts      
#>   <chr>  <chr>    <chr>     <chr>     <chr>             <int> <chr>          
#> 1 f_cyl4 f_cyl    f_cyl     factor    categorical           3 contr.treatment
#> 2 f_cyl6 f_cyl    f_cyl     factor    categorical           3 contr.treatment
#> 3 f_cyl8 f_cyl    f_cyl     factor    categorical           3 contr.treatment
#> # ℹ 10 more variables: contrasts_type <chr>, reference_row <lgl>, label <chr>,
#> #   n_obs <dbl>, estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> #   p.value <dbl>, conf.low <dbl>, conf.high <dbl>

Created on 2023-03-31 with reprex v2.0.2

from broom.helpers.

larmarange avatar larmarange commented on July 19, 2024

Same examples but with full tables visible. The levels are visible in the label column.

library(broom.helpers)
mtcars$f_cyl <- factor(mtcars$cyl)
m <- lm(mpg ~ f_cyl, mtcars)

tidy_and_attach(m) |> 
  tidy_add_variable_labels() |> 
  knitr::kable()
term variable var_label var_class var_type var_nlevels estimate std.error statistic p.value conf.low conf.high
(Intercept) (Intercept) (Intercept) NA intercept NA 26.663636 0.9718008 27.437347 0.0000000 24.67608 28.651192
f_cyl6 f_cyl f_cyl factor categorical 3 -6.920779 1.5583482 -4.441099 0.0001195 -10.10796 -3.733599
f_cyl8 f_cyl f_cyl factor categorical 3 -11.563636 1.2986235 -8.904533 0.0000000 -14.21962 -8.907653
tidy_and_attach(m) |> 
  tidy_add_term_labels() |> 
  knitr::kable()
term variable var_label var_class var_type var_nlevels contrasts contrasts_type label estimate std.error statistic p.value conf.low conf.high
(Intercept) (Intercept) (Intercept) NA intercept NA NA NA (Intercept) 26.663636 0.9718008 27.437347 0.0000000 24.67608 28.651192
f_cyl6 f_cyl f_cyl factor categorical 3 contr.treatment treatment 6 -6.920779 1.5583482 -4.441099 0.0001195 -10.10796 -3.733599
f_cyl8 f_cyl f_cyl factor categorical 3 contr.treatment treatment 8 -11.563636 1.2986235 -8.904533 0.0000000 -14.21962 -8.907653
tidy_and_attach(m) |> 
  tidy_add_reference_rows() |> 
  tidy_add_term_labels() |> 
  knitr::kable()
term variable var_label var_class var_type var_nlevels contrasts contrasts_type reference_row label estimate std.error statistic p.value conf.low conf.high
(Intercept) (Intercept) (Intercept) NA intercept NA NA NA NA (Intercept) 26.663636 0.9718008 27.437347 0.0000000 24.67608 28.651192
f_cyl4 f_cyl f_cyl factor categorical 3 contr.treatment treatment TRUE 4 NA NA NA NA NA NA
f_cyl6 f_cyl f_cyl factor categorical 3 contr.treatment treatment FALSE 6 -6.920779 1.5583482 -4.441099 0.0001195 -10.10796 -3.733599
f_cyl8 f_cyl f_cyl factor categorical 3 contr.treatment treatment FALSE 8 -11.563636 1.2986235 -8.904533 0.0000000 -14.21962 -8.907653
tidy_plus_plus(m) |> 
  knitr::kable()
term variable var_label var_class var_type var_nlevels contrasts contrasts_type reference_row label n_obs estimate std.error statistic p.value conf.low conf.high
f_cyl4 f_cyl f_cyl factor categorical 3 contr.treatment treatment TRUE 4 11 0.000000 NA NA NA NA NA
f_cyl6 f_cyl f_cyl factor categorical 3 contr.treatment treatment FALSE 6 7 -6.920779 1.558348 -4.441099 0.0001195 -10.10796 -3.733599
f_cyl8 f_cyl f_cyl factor categorical 3 contr.treatment treatment FALSE 8 14 -11.563636 1.298623 -8.904533 0.0000000 -14.21962 -8.907653

Created on 2023-03-31 with reprex v2.0.2

from broom.helpers.

aghaynes avatar aghaynes commented on July 19, 2024

perfect, thanks!

from broom.helpers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.