Search code examples
rregressionknitrkable

Extract regression summary() output in word document in a nicely formated resut


Is there a way to extract the summary of a regression model in a nice latex format like the correlation table I attach below? The table should be exported using knitr in word document or at least saved in word document.

enter image description here

# install.packages("dplyr")
# install.packages("kableExtra")
library(dplyr)
library(kableExtra)

mlm1 <- lm(mpg ~  . , data = mtcars)
summary(mlm1)


summary(mlm1) %>%
  kbl(caption="Table 1: Summary Statistics of Financial Well-Being  
               Score by Gender and Education",
       format= "html",
                  align="r") %>%
   kable_classic(full_width = F, html_font = "helvetica")

Solution

  • you can use the broom package:

    library(dplyr)
    library(kableExtra)
    library(broom)
    
    mlm1 <- lm(mpg ~  . , data = mtcars)
    summary(mlm1)
    
    tidy(mlm1) %>%  kbl(caption="Table 1: Summary Statistics of Financial Well-Being  
                   Score by Gender and Education",
                        format= "html",
                        align="r") %>%
      kable_classic(full_width = F, html_font = "helvetica")
    

    enter image description here

    Also, you can reports information about the entire model (i.e. R-squared, AIC, ...) via broom::glance(mlm1).