Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include apsrtable (or stargazer) output in an Rmd file

I tried to include the summary of an lm object in an Rmd file, using code like the following but it didn't work. Could you help me do that?

```{r summary_lm, results='asis', echo=FALSE, comment=NA}

library(apsrtable)
my_model <- lm(y ~ x, data = data.frame(y = rnorm(10), x = 1:10))
res <- apsrtable(my_model) # my_model is a linear regression model (lm)

cat("$$latex \n",res,"\n$$ \n")

```
like image 863
George Dontas Avatar asked Feb 10 '13 11:02

George Dontas


2 Answers

The $$ syntax only applies to math expressions, and you were trying to put a table in it, which will not work. The apsrtable, as far as I understand, is for LaTeX only, but LaTeX and Markdown are very different -- there is little hope you can redo LaTeX entirely with Markdown. I think people invented the $$ syntax for Markdown due to the fact that it is well supported by MathJax, and also note there are many variants/flavors based on the original Markdown.

At the moment you may consider:

  • use the xtable or ascii or R2HTML package to generate HTML tables
  • request the package author of apsrtable to support HTML tables
like image 173
Yihui Xie Avatar answered Oct 11 '22 22:10

Yihui Xie


What about including my_model in Markdown format with `pander˙:

> library(pander)
> pander(my_model)

--------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
      **x**         0.1174      0.1573     0.7465     0.4767  

 **(Intercept)**   -0.2889      0.9759     -0.296     0.7748  
--------------------------------------------------------------

Table: Fitting linear model: y ~ x

Or in PHP MarkdownExtra/rmarkdown format:

> panderOptions('table.style', 'rmarkdown')
> pander(my_model)


|      &nbsp;       |  Estimate  |  Std. Error  |  t value  |  Pr(>|t|)  |
|:-----------------:|:----------:|:------------:|:---------:|:----------:|
|       **x**       |   0.1174   |    0.1573    |  0.7465   |   0.4767   |
|  **(Intercept)**  |  -0.2889   |    0.9759    |  -0.296   |   0.7748   |

Table: Fitting linear model: y ~ x
like image 20
daroczig Avatar answered Oct 11 '22 21:10

daroczig