How to decimal-align regression coefficients in Latex table output in rmarkdown document

后端 未结 2 1702
-上瘾入骨i
-上瘾入骨i 2021-01-06 18:41

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\

2条回答
  •  没有蜡笔的小新
    2021-01-06 19:02

    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")
    

提交回复
热议问题