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)
(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.
(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.
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
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'
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".
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).