0

This post is a continuation of this one here. I tried to create the regression output table using model summary as suggested. But I run into some trouble.

# Code from the original question
library(mfx)
library(dplyr)

year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
female <- sample(c(0,1), replace=TRUE, size=10000)
smoker <- sample(c(0,1), replace=TRUE, size=10000)
dta <- data.frame(year = year, group = group, value = value, female=female, smoker = smoker)
table_list <- dta %>%
  group_by(year, group) %>%
  group_split()

model_list <- lapply(table_list, function(x) probitmfx(smoker ~ female, data = x))

# New code
library(modelsummary)
tab <- modelsummary(model_list, output = 'flextable',
             stars = TRUE, 
             title = "Table x.", gof_omit = 'IC|Log|Adj')
tab

Question 1. This gives me a very strange looking table like this: enter image description here

Question 2. Also, I would like to add a header using add_header, like you could with kable. How would I do that in modelsummary?

tab %>% 
  add_header(c(" " = 2, "Donations" = 4, "Crimes (person)" = 4, "Crimes (property)" = 4))%>%
  autofit()

Edit: Version R

This is the version platform   
x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes 

Edit1:

I get the same result independent of whether I set kable or flextable. I've updated the package and restarted the computer.

Stata_user
  • 562
  • 3
  • 14

1 Answers1

1

What is important to understand about modelsummary is that it only prepares the regression table "structure", and that it delegates the "drawing" of the table to external packages: kableExtra, gt, flextable, or huxtable (more packages to come).

This is great, because the supported table-making packages are super powerful, and they allow nearly unlimited customization. But of course, the downside is that you need to learn a bit about those packages if you want to customize. The packages in question are really well document, so please check out their websites:

Your problems seem to be:

  1. I cannot reproduce the table you pasted on my computer in a fresh R session by just copy pasting your code. Please update your packages, restart your R session entirely, and try again. (What version of R are you on?)
  2. The width of your flextable is too small, so flextable inserts line breaks. Fix by using the width command from the flextable package.
  3. You are trying to add a row of labels using the kableExtra syntax (which is the default output format for modelsummary), but you manually decided to create a flextable output. Similar functionality is available from the add_header_row from the flextable package.

I'll use the magrittr pipe for readability:

library(magrittr)

kableExtra solution (the default in modelsummary):

library(kableExtra)

modelsummary(model_list) %>%
  add_header_above(c(" " = 1, "first" = 4, "second" = 10))

flextable solution (your prefered output format):

library(flextable)

modelsummary(model_list, output = "flextable") %>%
  add_header_row(values = c("", "first", "second"),
                 colwidths = c(1, 4, 10)) %>%
  width(width = 10)
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • Thanks Vincent. I wish I could make it work. This seems to be the only package that allows me to present mfx results. – Stata_user Feb 19 '21 at 16:37
  • The coefficient values seem to be correct. It's just that they are repeated many many times. – Stata_user Feb 19 '21 at 16:42
  • The output of `sessionInfo()` would be more useful to diagnose the problem. Make sure the `modelsummary`, `flextable`, and `kableExtra` packages are loaded. – Vincent Feb 19 '21 at 16:51