Search code examples
rstargazer

Visualizing multiple summaries with stargazer


I am currently working on a project where I am comparing multiple linear regressions. Stargazer does an amazing job with exporting summaries for the relevant model but I have been wondering if it was possible to implement a function in the stargazer command to simultaneously export several summaries and graphically order them. My current approach looks like this:

First, I export my relevant summaries to a html format using:

stargazer(development_gr_US_pre_BvG, type = "html", out = "whatever")

stargazer(development_gr_US_post_BvG, type = "html", out = "whatever")

The corresponding output is:

<table style="text-align:center"><tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td><em>Dependent variable:</em></td></tr>
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>n</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">earliest_filing_year</td><td>1.510<sup>*</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.760)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>-3,007.075<sup>*</sup></td></tr>
<tr><td style="text-align:left"></td><td>(1,523.425)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>14</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>0.248</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>0.185</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>11.463 (df = 12)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>3.947<sup>*</sup> (df = 1; 12)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>

<table style="text-align:center"><tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td><em>Dependent variable:</em></td></tr>
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>n</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">earliest_filing_year</td><td>-4.400<sup>**</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.648)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>8,872.900<sup>**</sup></td></tr>
<tr><td style="text-align:left"></td><td>(1,304.897)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>4</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>0.958</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>0.938</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>1.449 (df = 2)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>46.095<sup>**</sup> (df = 1; 2)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>

I then transfer the output to Notepad++ and format it in the following way:

<table style="float: left;">
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>n</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">earliest_filing_year</td><td>1.510<sup>*</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.760)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>-3,007.075<sup>*</sup></td></tr>
<tr><td style="text-align:left"></td><td>(1,523.425)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>14</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>0.248</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>0.185</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>11.463 (df = 12)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>3.947<sup>*</sup> (df = 1; 12)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>

<table style="float: left;">
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>n</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">earliest_filing_year</td><td>-4.400<sup>**</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.648)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>8,872.900<sup>**</sup></td></tr>
<tr><td style="text-align:left"></td><td>(1,304.897)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>4</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>0.958</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>0.938</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>1.449 (df = 2)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>46.095<sup>**</sup> (df = 1; 2)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>

When I then open the document in my browser it looks like this

But that is just my bumpy workaround solution. I had been wondering if there was a nice way to tell R to immediately store multiple summaries that way, or - even better - give them to me as a PNG in that format immediately. Thank you for your guidance!


Solution

  • stargazer can easily take more models and display side-by-side. As "text" in the console, or export as "tex" or "html" files. So you only see the covariate labels once, same is true for the model statistics.

    library("plm")
    library("stargazer")
    
    data("Produc", package = "plm")
    
    # Regression    
    model1 <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
                 data = Produc, 
                 index = c("state","year"),
                 method="pooling")
    
    model2 <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp),
                 data = Produc, 
                 index = c("state","year"),
                 method="pooling")
    
    stargazer(model1, model2, type = "html", out="models.htm")
    

    Output looks like this:

    enter image description here