A table from a lm() output

Hi,

I'm trying to find an easy and quick way how to create a table with lm() statistics. I thought an as_flextable() fuction would a nice solution but I cannot find a way how to (de) select some columns (e.g. deselct 't value' column). Or change the color for a rows which are statistically significant.

I would aprreciate both (a) solutions using flextable package and (b) tips for other workflows.

library(flextable)

lm <- lm(mpg ~ cyl, data = mtcars)

as_flextable(lm)

Many thanks,

Jakub

(a) One way to use the flextable package to create a table with lm() statistics while deselecting certain columns or changing the color of specific rows is to use the flextable() function to create the table and then use the various formatting functions available in the package to customize the table.

For example, to remove a column, you can use the remove() function and specify the column name. To change the color of a specific row, you can use the bg() function and specify the row index and the color.

library(flextable)
lm_ft <- flextable(lm)
lm_ft <- remove(lm_ft, j = c("t value"))
lm_ft <- bg(lm_ft, i = c(1), j = 1:ncol(lm_ft), bg = "blue")
lm_ft

(b) Another way to create a table with lm() statistics is to use the stargazer package. It allows you to easily create LaTeX, HTML, and ASCII tables from various model objects, including lm(). The package also provides options for selecting or deselecting columns, controlling the appearance of the table and more.

library(stargazer)

stargazer(lm,type = "text", 
          omit.stat = c("t","F"),
          title = "Linear Regression Results",
          dep.var.caption = "Dependent variable",
          covariate.labels = c("Cyl"))

The above are great suggestions; a further one might be
lm is turned into a flextable as youve seen via the as_flextable.lm method (i.e. a function that is called when as.flextable() is requested and the input to it is an lm.
You can directly view the code with the command :

getAnywhere(as_flextable.lm)

you can make your own function out of the bones of this.
The basic idea is that it gets the likely relevant info out of the lm via calls to broom functions

 data_t <- broom::tidy(x)
    data_g <- broom::glance(x)

and then it creates a flextable out of an arrangement of those facts. you could alter any part of this.

1 Like

Hi, thanks you very much for you answer.

But I encountered a few problems.

First, after using flextable() instead of as_flextable I get the error message 'Error in flextable(lm) : is.data.frame(data) is not TRUE'

Second, after using lm_ft <- remove(lm_ft, j = c("t value")), I get the error message 'Error in remove(lm_ft, j = c("t value")) :
... must contain names or character strings'

Once again, many thanks!

If I use a stargazer(), what is the workflow after suggested command to get, say png table, instertable into a Word document?

Jakub

You can use the select() function from the dplyr package to select the columns you want and then pass the resulting data frame to the flextable() function.

library(broom)
library(dplyr)

lm <- lm(mpg ~ cyl, data = mtcars)
coef_table <- as.data.frame(summary(lm)$coefficients)
coef_table <- select(coef_table, Estimate, `Std. Error`, `Pr(>|t|)`)
ft <- flextable(coef_table)
ft

You can also change the color of rows in the flextable by using the bg() function to apply a background color to specific cells or rows, based on a condition. For example, to change the background color of rows where the Pr(>|t|) value is less than 0.05.

ft <- ft %>% bg(i = coef_table$`Pr(>|t|)` < 0.05, j = "Pr(>|t|)", bg = "lightgreen")

This will change the background color of the "Pr(>|t|)" column in the rows where the Pr(>|t|) value is less than 0.05 to "lightgreen".

Many thanks, I'm trying now to add number of observations, R^2 and adjusted R^2. Any tips?

Or what workflow would you recomment with stargazer()?

Many thanks,

Jakub

Here is the complete code:

lm <- lm(mpg ~ cyl, data = mtcars)
glance_results <- glance(lm)
coef_table <- as.data.frame(coef(summary(lm)))
coef_table <- select(coef_table, Estimate, `Std. Error`,`Pr(>|t|)`)
coef_table <- cbind(coef_table, R2 = glance_results$r.squared, adj_R2 = glance_results$adj.r.squared)

ft <- flextable(coef_table)
ft

You can use the stargazer() function from the stargazer package to create a table of your linear model statistics.

library(stargazer)

lm <- lm(mpg ~ cyl, data = mtcars)

stargazer(lm, type = "text", 
          title = "Linear Model Statistics", 
          dep.var.labels.include = FALSE,
          keep.stat = c("rsq", "adj.rsq"))

I would highly recommend the gtsummary package as an alternative: https://www.danieldsjoberg.com/gtsummary/

This is the basic code:

library(gtsummary)
library(tidyverse)
library(flextable)

lm <- lm(mpg ~ cyl, data = mtcars)

lm %>% 
  tbl_regression()

Adding in some of the other things you were looking for (bold for P<0.05, model fit stats and a flextable object):

lm %>% 
  tbl_regression(intercept = TRUE) %>% 
  add_glance_table(include = c(r.squared, adj.r.squared)) %>% 
  bold_p(t = 0.05) %>% 
  as_flex_table()

This link has further details for exporting tables from gtsummary as pngs https://stackoverflow.com/questions/60751172/how-to-output-gtsummary-tables-as-images, but this may be easier to achieve using RMarkdown or Quarto docs.

As an aside, while it's widely done, bold or stars for 'significant' p-values is discouraged in many areas due to an over reliance on arbitrary thresholds for significance (e.g. p<0.05).

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.