nutterb / pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
180 stars 18 forks source link

Include variable labels and levels in table summaries #18

Closed nutterb closed 8 years ago

nutterb commented 9 years ago

@matthieugomez, @simonthelwall I've been working on some resolutions to the broom issue https://github.com/dgrtwo/broom/issues/16.

To that effect, I have a small suite of functions in a gist (https://gist.github.com/nutterb/ef5815cb535877b68667) that if you copy into your workspace, you can run the following code:

mtcars2 <- mtcars
Hmisc::label(mtcars2$mpg) <- "Gas Mileage"
Hmisc::label(mtcars2$qsec) <- "Quarter Mile Time"
Hmisc::label(mtcars2$am) <- "Transmission"
Hmisc::label(mtcars2$wt) <- "Weight"
Hmisc::label(mtcars2$gear) <- "Gears"

fit <- lm(mpg ~ qsec + factor(am) * wt + factor(gear), data = mtcars2)

library(pixiedust)
tidy_levels_labels(fit) %>%
  dust() %>%
  sprinkle(cols = 2:5, round = 1)
            term estimate std.error statistic p.value    term_plain             label level level_detail
1    (Intercept)      9.8         7       1.4     0.2          <NA>              <NA>  <NA>         <NA>
2           qsec        1       0.3       2.9       0          qsec Quarter Mile Time                   
3    factor(am)1     14.1       3.5         4       0    factor(am)        factor(am)     1      1 vs. 0
4             wt     -2.8       0.7        -4       0            wt            Weight                   
5  factor(gear)4        1       1.4       0.7     0.5  factor(gear)      factor(gear)     4      4 vs. 3
6  factor(gear)5      1.5       1.8       0.8     0.4  factor(gear)      factor(gear)     5      5 vs. 3
7 factor(am)1:wt     -4.5       1.3      -3.5       0 factor(am):wt factor(am):Weight    1:     1 vs. 0:

If you don't mind, I'd like your feedback on how well this satisfies your desire to separate of factor levels and handle the interactions.

The known limitations:

  1. This only works on models that have a model.frame method. I'm not sure I want to go chasing all of this information for models that don't have a convenient way to get the data out of the model.
  2. Clearly, the interaction between factor and numeric needs some work in the representation. I just haven't worked on it yet because I want to make sure I'm on the right path first.
  3. When a function is passed to the formula (ie, factor(am) or wt^2, I'm not going to be able to recover the variable label. Is that something you could live with? It would mean either making sure you calculate all of your transformations before passing them to the model, or using the replace sprinkle in the post-processing with pixiedust.

Any thoughts?

nutterb commented 8 years ago

Well, it's implemented and will appear in 0.4.0