Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regression tables in Markdown format (for flexible use in R Markdown v2)

The new version of R Markdown is based on pandoc, so you can easyly change the output format.

My Problem is to get markdown formated tables from e.g. regression models, because LATEX and HTML tables do not survive the pandoc conversion.

I know packages that generate LATEX/HTML output from a variety of models (stargazer, texreg, asprtable...) and I'm aware of functions/packages, that generate markdown tables from data frames and matrices but not from other objects.

Any suggestions?

like image 701
sammerk Avatar asked Jun 21 '14 13:06

sammerk


People also ask

Can you insert a table into R markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.

What is the difference between markdown and RMarkdown?

R Markdown (markup language)R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.

What is knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


2 Answers

My above comment in more details:

  1. Define a few models for reproducible example:

    lm0 <- lm(hp ~ wt, mtcars)
    lm1 <- lm(qsec ~ hp, mtcars)
    lm2 <- lm(qsec ~ wt, mtcars)
    
  2. Create a comparative table from those:

    require(memisc)
    mtable123 <- mtable('Model 1' = lm0,
                'Model 2' = lm1,
                'Model 3' = lm2,
                summary.stats = c('R-squared','F','p','N'))
    
  3. Render markdown table with a simple call to pander:

    pander(mtable123)
    
  4. Enjoy the result:

    --------------------------------------------------
         &nbsp;        Model 1    Model 2    Model 3  
    ----------------- ---------- ---------- ----------
     **(Intercept)**   -1.821\   20.556***\ 18.875***\
                       (32.325)   (0.542)    (1.103)  
    
         **wt**       46.160***\     \       -0.319\  
                       (9.625)               (0.328)  
    
         **hp**           \      -0.018***\     \     
                                  (0.003)             
    
      **R-squared**     0.434      0.502      0.031   
    
          **F**         22.999     30.190     0.945   
    
          **p**         0.000      0.000      0.339   
    
          **N**           32         32         32    
    --------------------------------------------------
    

Thanks for Roman Tsegelskyi for implementing this nice feature in GSoC 2014.

like image 183
daroczig Avatar answered Sep 19 '22 13:09

daroczig


Just generate the HTML or LATEX tables. All you have to do is to just add results='asis' to the code chunk. It will leave the output as it is.

For example this code using xtable works for me.

```{r,results='asis'}
x<-rnorm(100)
y<-rnorm(100)
lm <- lm(y~x)
library(xtable)
print(xtable(summary(lm)),type='html')
```
like image 32
Koundy Avatar answered Sep 22 '22 13:09

Koundy