Press "Enter" to skip to content

Expand broom::tidy() output for categorical parameter estimates

[This article was first published on r-bloggers on Guy Abel, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

Introduction

The tidycat package includes the tidy_categorical() function to expand broom::tidy() outputs for categorical parameter estimates.

Hello World

The tidy() function in the broom package takes the messy output of built-in functions in R, such as lm(), and turns them into tidy data frames.

library(dplyr)
library(broom) m1 % mutate(transmission = recode_factor(am, `0` = "automatic", `1` = "manual")) %>% lm(mpg ~ as.factor(cyl) + transmission + wt * as.factor(cyl), data = .) tidy(m1)
## # A tibble: 7 x 5
## term estimate std.error statistic p.value
## 
## 1 (Intercept) 41.5 4.54 9.14 0.00000000190
## 2 as.factor(cyl)6 -8.66 10.4 -0.836 0.411 ## 3 as.factor(cyl)8 -16.9 5.27 -3.20 0.00374 ## 4 transmissionmanual -0.902 1.51 -0.595 0.557 ## 5 wt -6.19 1.65 -3.75 0.000937 ## 6 as.factor(cyl)6:wt 2.12 3.40 0.625 0.538 ## 7 as.factor(cyl)8:wt 3.84 1.77 2.17 0.0399

The tidy_categorical() function adds

  • further columns (variable, level and effect) to the broom::tidy() output to help manage categorical variables
  • further rows for reference category terms and a column to indicate their location (reference) when setting include_reference = TRUE (default)

It requires two inputs

  • a data frame d of parameter estimates from a model from broom::tidy()
  • the corresponding model object m passed to broom::tidy()

For example:

library(tidycat)
d1 % tidy(conf.int = TRUE) %>% tidy_categorical(m = m1)
d1 %>% select(-(3:5))
## # A tibble: 10 x 8
## term estimate conf.low conf.high variable level effect reference ##  ## 1 (Interce~ 41.5 32.1 50.8 (Intercept) (Inte~ main Non-Baselin~
## 2  0 0 0 as.factor(~ 4 main Baseline Ca~
## 3 as.facto~ -8.66 -30.0 12.7 as.factor(~ 6 main Non-Baselin~
## 4 as.facto~ -16.9 -27.7 -6.00 as.factor(~ 8 main Non-Baselin~
## 5  0 0 0 transmissi~ autom~ main Baseline Ca~
## 6 transmis~ -0.902 -4.02 2.22 transmissi~ manual main Non-Baselin~
## 7 wt -6.19 -9.59 -2.79 wt wt main Non-Baselin~
## 8  0 0 0 as.factor(~ 4 intera~ Baseline Ca~
## 9 as.facto~ 2.12 -4.87 9.12 as.factor(~ 6 intera~ Non-Baselin~
## 10 as.facto~ 3.84 0.192 7.50 as.factor(~ 8 intera~ Non-Baselin~

The expanded data frame from tidy_categorical() of parameter estimates can be particularly useful for creating coefficient plots, allowing:

  • grouping terms from the same categorical variable from the additional columns.
  • inclusion of reference categories in a coefficient plot from the additional rows, allowing the reader to better grasp the meaning of the parameter estimates in each categorical variable.

For example:

library(forcats)
library(ggplot2)
library(ggforce) d1 %>% slice(-1) %>% mutate(variable = fct_inorder(variable)) %>% ggplot(mapping = aes(x = level, y = estimate, colour = reference, ymin = conf.low, ymax = conf.high)) + facet_row(facets = "variable", scales = "free_x", space = "free") + geom_hline(yintercept = 0, linetype = "dashed") + geom_pointrange()

Installation

You can install the released version of tidycat from CRAN with:

install.packages("tidycat")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("guyabel/tidycat")

14 Comments

  1. Like!! Really appreciate you sharing this blog post.Really thank you! Keep writing.

  2. SMS July 8, 2020

    Your site is very helpful. Many thanks for sharing!

  3. Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.

  4. I like this website very much, Its a very nice office to read and incur information.

  5. SMS July 8, 2020

    Your site is very helpful. Many thanks for sharing!

  6. jordan shoes July 14, 2020

    I have to express my appreciation to the writer just for bailing me out of such a condition. After surfing around throughout the world-wide-web and getting techniques which were not productive, I figured my entire life was well over. Being alive minus the strategies to the issues you have resolved through your main write-up is a critical case, as well as ones which may have badly damaged my entire career if I hadn’t come across your web page. Your personal training and kindness in handling every aspect was crucial. I’m not sure what I would’ve done if I hadn’t come across such a stuff like this. I’m able to now look forward to my future. Thanks a lot very much for the impressive and results-oriented help. I will not hesitate to endorse your site to anybody who needs to have direction on this problem.

  7. supreme clothing July 17, 2020

    I simply wanted to thank you so much yet again. I do not know the things I could possibly have worked on in the absence of those tactics shared by you relating to such a question. Certainly was the terrifying dilemma for me, but considering the professional mode you solved the issue made me to cry over delight. Extremely thankful for this service and as well , hope that you are aware of an amazing job that you’re putting in teaching people today through the use of your web site. I’m certain you haven’t got to know any of us.

  8. goyard July 21, 2020

    I in addition to my buddies appeared to be reviewing the great hints found on your web site and then suddenly I had a terrible suspicion I had not expressed respect to the site owner for those secrets. These guys came as a result very interested to learn them and have seriously been taking advantage of them. I appreciate you for indeed being quite accommodating and for selecting certain beneficial subjects millions of individuals are really desperate to learn about. Our own honest regret for not expressing appreciation to sooner.

  9. air max 2018 July 30, 2020

    I am just commenting to let you know of the fine experience my cousin’s princess undergone checking your web page. She came to understand lots of issues, which include how it is like to have an ideal coaching mood to let many more with no trouble understand various tortuous issues. You undoubtedly surpassed visitors’ expectations. I appreciate you for providing these good, trustworthy, informative as well as unique thoughts on your topic to Mary.

  10. goyard August 2, 2020

    Needed to draft you the very little observation in order to give many thanks as before with your marvelous strategies you have featured at this time. This is really remarkably generous of people like you to make unhampered precisely what a lot of folks would have advertised as an ebook in making some dough on their own, even more so now that you might well have done it if you desired. Those thoughts additionally worked to be a good way to be aware that some people have similar interest much like my own to see significantly more when it comes to this condition. I’m certain there are numerous more pleasant opportunities ahead for individuals who read carefully your website.

  11. supreme clothing August 5, 2020

    Thank you so much for giving everyone an exceptionally marvellous possiblity to discover important secrets from this site. It is usually very ideal and also packed with amusement for me and my office fellow workers to search your blog at least three times in 7 days to read the new guidance you will have. Not to mention, I’m so actually impressed with all the effective things you give. Certain two facts in this article are clearly the very best I’ve had.

Leave a Reply

Your email address will not be published.