In an rmarkdown
document, I\'m creating a Latex table of regression coefficients with standard errors to compare several regression models in a single table. I\
Here is an attempt using broom
. You'll still need to clean up the labels though.
library(broom)
library(dplyr)
library(pander)
library(tidyr)
m1 = glm(mpg ~ wt + factor(cyl), data=mtcars)
m2 = glm(mpg ~ wt + factor(cyl) + hp + factor(am), data=mtcars)
base <- tidy(m1) %>% select(term, estimate) %>% mutate(type = "base_model")
with_am_hp <- tidy(m2) %>% select(term, estimate) %>% mutate(type = "Add_Horsepower_and_AM")
models <- bind_rows(base, with_am_hp)
formatted_models <- models %>% spread(type, estimate)
m1_glance <- glance(m1) %>% mutate(type = "base_model")
m2_glance <- glance(m2) %>% mutate(type = "Add_Horsepower_and_AM")
glance_table <- data.frame("Add_Horsepower_and_AM" = unlist(glance(m2)), "base_model" = unlist(glance(m1))) %>% mutate(term = row.names(.))
full_results <- bind_rows(formatted_models, glance_table)
pandoc.table(full_results, justify = "left")