Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created November 4, 2014 15:04
Show Gist options
  • Save andrewheiss/7942480391f67809bc57 to your computer and use it in GitHub Desktop.
Save andrewheiss/7942480391f67809bc57 to your computer and use it in GitHub Desktop.
mtable and stargazer
library(memisc)
library(stargazer)
library(pander)
lm0 <- lm(sr ~ pop15 + pop75, data = LifeCycleSavings)
lm1 <- lm(sr ~ dpi + ddpi, data = LifeCycleSavings)
lm2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
mtable123 <- mtable("Model 1"=lm0,"Model 2"=lm1,"Model 3"=lm2, summary.stats=c("sigma","R-squared","F","p","N"))
pander(mtable123)
# -----------------------------------------------------
# &nbsp; Model 1 Model 2 Model 3
# ----------------- ----------- ----------- -----------
# **(Intercept)** 30.628***\ 6.360***\ 28.566***\
# (7.409) (1.252) (7.355)
# **pop15** -0.471**\ \ -0.461**\
# (0.147) (0.145)
# **pop75** -1.934\ \ -1.691\
# (1.041) (1.084)
# **dpi** \ 0.001\ -0.000\
# (0.001) (0.001)
# **ddpi** \ 0.529*\ 0.410*\
# (0.210) (0.196)
# **sigma** 3.931 4.189 3.803
# **R-squared** 0.262 0.162 0.338
# **F** 8.332 4.528 5.756
# **p** 0.001 0.016 0.001
# **N** 50 50 50
# -----------------------------------------------------
stargazer(lm0, lm1, lm2, type="text",
star.cutoffs=c(0.05, 0.01, 0.001), model.names=FALSE,
dep.var.labels.include=FALSE, dep.var.caption="",
notes=c("Here are some useful notes about the results",
"Like stuff about robust standard errors and whatnot"))
# ===================================================================================
# (1) (2) (3)
# -----------------------------------------------------------------------------------
# pop15 -0.471** -0.461**
# (0.147) (0.145)
# pop75 -1.934 -1.691
# (1.041) (1.084)
# dpi 0.001 -0.0003
# (0.001) (0.001)
# ddpi 0.529* 0.410*
# (0.210) (0.196)
# Constant 30.628*** 6.360*** 28.566***
# (7.409) (1.252) (7.355)
# -----------------------------------------------------------------------------------
# Observations 50 50 50
# R2 0.262 0.162 0.338
# Adjusted R2 0.230 0.126 0.280
# Residual Std. Error 3.931 (df = 47) 4.189 (df = 47) 3.803 (df = 45)
# F Statistic 8.332*** (df = 2; 47) 4.528* (df = 2; 47) 5.756*** (df = 4; 45)
# ===================================================================================
# Note: *p<0.05; **p<0.01; ***p<0.001
# Here are some useful notes about the results
# Like stuff about robust standard errors and whatnot
@daroczig
Copy link

daroczig commented Nov 4, 2014

The problem is that Pandoc's markdown has a limited support for tables -- so that it can be truly compatible with most document formats. E.g. it does not support col/row spanning and borders, that's why Roman and I came up with the workaround of putting two values in a cell with the help of so called "hard breaks" inside of cells. Notes below tables are not supported similarly, only captions can be added.

If we want the Observations/R2 etc to be separated from the other model parameters, it could be returned in a separate table, like it's done for a simple linear model:

> pander(summary(lm0))

--------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
    **pop15**      -0.4708      0.1468     -3.207    0.002414 

    **pop75**       -1.934      1.041      -1.858    0.06943  

 **(Intercept)**    30.63       7.409       4.134   0.0001457 
--------------------------------------------------------------


-------------------------------------------------------------
 Observations   Residual Std. Error   $R^2$   Adjusted $R^2$ 
-------------- --------------------- ------- ----------------
      50               3.931         0.2617       0.2303     
-------------------------------------------------------------

Table: Fitting linear model: sr ~ pop15 + pop75

And it comes with some minor functions as well:

> pander(summary(lm0), covariate.labels = c('foo', 'bar'), omit = '(Intercept)', summary = FALSE, caption = 'foobar!', add.significance.stars = TRUE)

----------------------------------------------------------
 &nbsp;    Estimate   Std. Error   t value     Pr(>|t|)   
--------- ---------- ------------ --------- --------------
 **foo**   -0.4708      0.1468     -3.207   _0.002414_ * *

 **bar**    -1.934      1.041      -1.858     _0.06943_   
----------------------------------------------------------

Table: foobar!

It seems to me that if you would like to get stargazer-like markdown output, then it requires same trade-off (as seen above), and it has to be implemented separately from stargazer as it returns ASCII that cannot be (easily) post-processed. But please let me know what features you miss, and I will be happy to try to resolve those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment