2

I am trying to export the results of multiple regressions in a single table. Ideally, it should be formatted similar to stargazer() output. The problem is that I have not found reliably working R functions for the kind of regressions I need (Fama-MacBeth regressions), so I use my custom regression functions, which produce all necessary output (estimates of coefficients, standard errors, t-stat, R^2).

Does stargazer() or other similar function have the parameters, which allow me to export results of multiple regressions to Latex in a nice form when output of my regression is just a dataframe?

EDIT: I was just wondering whether it is possible to create publication-style tables, looking like this: enter image description here

Moysey Abramowitz
  • 352
  • 1
  • 7
  • 19
  • So what's not working for you? In `stargazer::stargazer(...)` function **`...`** arguments takes model objects and/or data frames, vector, etc. If you can format output of your functions to get data frames or objects of `lm` class `stargazer` should be able to handle that. – Konrad Oct 04 '18 at 16:03
  • When I use stargazer(output1, output2), I get latex output in the form of two separate tables. I want to get 1 table, in which each column corresponds to different regression specification. Output1 and output2 are two dataframes, which contain the output of two different regression models. – Moysey Abramowitz Oct 04 '18 at 16:10
  • You can create a stargazer output of any dataframe with `summary=FALSE` switch. If you have two different regression outputs with output1 and output2, you can merge(output1, output2, all.x=T, all.y=T) the two outputs into a single dataframe, and stargaze that. Here is one example solving a similar problem: https://stackoverflow.com/questions/49004490/include-vif-information-in-stargazer-output/49014115#49014115 – Otto Kässi Oct 04 '18 at 16:13
  • 1
    Stargazer is a bit limited in what it can do beyond standard regression outputs. Nonetheless you can overwrite the default standard errors and coefficients by providing your own standard errors and coefficients as `se` and `coef` parameters into stargazer. You can also insert your own lines into the output as follows: `add.lines = list(c('My custom R2', 0.001, 0.002, 0.003))`. Here's you can find more discussion on this: https://stackoverflow.com/questions/21338567/get-coefficients-estimated-by-maximum-likelihood-into-a-stargazer-table – Otto Kässi Oct 05 '18 at 09:39

1 Answers1

0

Here's a simple example that might help you forward (example is too long for a comment, so making this an answer):

library(stargazer)
library(broom)

## generate dummy data
set.seed(123)
x <- runif(1000)
z <- x^0.5
y <-  x + z + rnorm(1000, sd=.05)
model1 <- lm(y ~ x)
model2 <- lm(y ~ z)

## transform model summaries into dataframes
tidy(model1) -> model1_tidy
tidy(model2) -> model2_tidy

merge(model1_tidy, model2_tidy, by='term', all.x=T, all.y=T)  -> output

stargazer(output, type='latex', summary=FALSE)

You will need to figure out the column headers by yourself but I believe you get the idea.

Otto Kässi
  • 2,943
  • 1
  • 10
  • 27
  • 1
    You can actually just run `broom::tidy(model)`. No need for `summary` call. – Konrad Oct 04 '18 at 16:29
  • But this only merge the two outputs. Don't creates a stylized table. – Ariel Apr 03 '20 at 20:07
  • @Ariel SC, late reply but for future reference, you can use stargazer or other tools to create a nice table from the `tidy()` output. – Otto Kässi Jun 23 '21 at 09:38
  • @OttoKässi Your code doesn't merge correctly. It duplicates the data (**std.error.x**, **std.error.y**, etc.). You may want to have just one variable called **std.error**. Also your code does not provide the outcome that Moysey Abramowitz is looking for. Please revise it! – vog Oct 17 '21 at 20:55