2

I've used R to run some logit regressions - testing the characteristics of investment firms and whether or not any predict sustainable behaviours.

In my paper I've copied acorss the output from R, however, I've had feedback saying I should try to format the output tables in a more classic academic way.

Does anyone have advice on how best to do this, or know of any tutorials which help with this kind of thing?

Many thanks, Rory

R Wd
  • 59
  • 5

4 Answers4

3

The modelsummary package for R supports hundreds of model types out of the box. It allows you to customize tables extensively and easily, and can save to multiple formats including Word, HTML, LaTeX/PDF. (Disclaimer: I am the package maintainer.)

The simplest way to summarize a logit model is this:

library(modelsummary)

mod <- glm(vs ~ hp, data = mtcars, family = binomial)

modelsummary(mod)

enter image description here

You can also display models side by side by saving them in a list:

models <- list(
    glm(vs ~ hp, data = mtcars, family = binomial),
    glm(vs ~ hp + mpg, data = mtcars, family = binomial))

modelsummary(models)

enter image description here

The package allows you to draw a vast array of different tables and plots such as these:

enter image description here

enter image description here

Vincent
  • 15,809
  • 7
  • 37
  • 39
2

You may use kable() function from kableExtra package. You can then copy and paste to use it. Remember to supply the outputin tidy form. It is done using the tidy() function from broom package.

model <- lm(mpg ~ disp + wt, data = mtcars)
kableExtra::kable(x = broom::tidy(model), format = "pipe")

You will get a table like this:

term estimate std.error statistic p.value
(Intercept) 34.9605540 2.1645395 16.151497 0.0000000
disp -0.0177247 0.0091904 -1.928609 0.0636198
wt -3.3508253 1.1641281 -2.878399 0.0074307

To know more about it run ?kableExtra::kable in console. There are more formatting options, e.g., latex, html, pipe (Pandoc's pipe tables), simple (Pandoc's simple tables), and rest.

You can learn more from these places:

  1. Pretty printing tables in markdown
  2. Stargazer documentation
Md Ahsanul Himel
  • 315
  • 1
  • 10
1

The best way to go is to prepare the report in RMarkdown and then use kableExtra and tidy to get the tables nicely formatted. For example:

library(kableExtra)
library(broom)
lm(Sepal.Length ~ Sepal.Width + Petal.Length, data=iris) |>
  tidy() |> 
  kable() |> 
  kable_classic()

Will create a table with regression results. You can play around by adding columns (as the result of tidy is a data frame you can e.g. calculate confidence intervals and add them) and also modify the formatting, see kableExtra manual (https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html), there's also a version for PDF documents obtained through LaTeX.

Claudio
  • 1,229
  • 6
  • 11
1

If you want a more scientific output that satisfies the requirements of many publishers, you want to look at the stargazer package: https://cran.r-project.org/web/packages/stargazer/stargazer.pdf

See also here for some examples, especially the HTML ones: http://www.princeton.edu/~otorres/NiceOutputR.pdf

deschen
  • 10,012
  • 3
  • 27
  • 50