简体   繁体   English

Markdown格式的回归表(在R Markdown v2中灵活使用)

[英]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. R Markdown的新版本基于pandoc,因此您可以轻松更改输出格式。

My Problem is to get markdown formated tables from eg regression models, because LATEX and HTML tables do not survive the pandoc conversion. 我的问题是从回归模型中获取markdown格式表,因为LATEX和HTML表不能在pandoc转换中存活。

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. 我知道从各种模型(stargazer,texreg,asprtable ......)生成LATEX / HTML输出的包,我知道函数/包,它们从数据框和矩阵生成降价表,但不从其他对象生成。

Any suggestions? 有什么建议?

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调用渲染降价表:

     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. 感谢Roman Tsegelskyi在GSoC 2014中实现了这个不错的功能。

Just generate the HTML or LATEX tables. 只需生成HTML或LATEX表。 All you have to do is to just add results='asis' to the code chunk. 您所要做的就是将结果='asis'添加到代码块中。 It will leave the output as it is. 它将保持输出不变。

For example this code using xtable works for me. 例如,使用xtable的代码对我有用。

```{r,results='asis'}
x<-rnorm(100)
y<-rnorm(100)
lm <- lm(y~x)
library(xtable)
print(xtable(summary(lm)),type='html')
```

Here what I did some hours ago: 这是我几个小时前做的:

  1. Some data: 一些数据:

     ```{r} lm1 <- lm(qsec ~ hp, mtcars) lm2 <- lm(qsec ~ wt, mtcars) ``` 
  2. We use the package sjPlot 我们使用包sjPlot

     ```{r} library(sjPlot) tab_model(lm1,lm2, file="output.html")# You have to save the table in html format. ``` 

    The next part needs to be outside the chunk in markdown: 下一部分需要在降价区域之外:

htmltools::includeHTML("output.html") htmltools :: includeHTML( “output.html”)

在此输入图像描述

Another way to use sjPlot (great library for easy presentation of regression output) is to use the no.output feature: 另一种使用sjPlot(用于简单呈现回归输出的库)的方法是使用no.output功能:

     library(sjmisc)
     library(sjPlot)
     library(magrittr)

     lm(qsec ~ wt, mtcars) %>% 
       sjt.lm(no.output = TRUE, show.se = TRUE) %>% 
       return() %>% .[["knitr"]] %>% asis_output

The huxtable package can now print nicely formatted regression table. huxtable包现在可以打印格式良好的回归表。 See the documentaton https://hughjonesd.github.io/huxtable/huxreg.html 请参阅文档https://hughjonesd.github.io/huxtable/huxreg.html

I'm updating this with an example that uses the popular (and newer) knitr and kableExtra packages. 我正在使用一个使用流行(和更新)knitr和kableExtra包的示例来更新它。

library(knitr)
library(xtable)

lm(hp ~ wt, mtcars) %>%
summary() %>%
xtable() %>%
kable()

Now you can access all the cool table-formatting features available in Hao Zhu's kableExtra package. 现在,您可以访问Hao Zhu的kableExtra包中提供的所有酷表格格式功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM