在Rmarkdown中使用apsrtable的多个lm()模型的表 [英] Table of multiple lm() models using apsrtable in Rmarkdown

查看:186
本文介绍了在Rmarkdown中使用apsrtable的多个lm()模型的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一个使用lm()函数创建的多个模型的结果一起呈现在格式良好的表中.该表将在.Rmd文件中生成,并输出到PDF文档.

Present the results of multiple models, created using the lm() function, together in a nicely-formatted table. This table will be generated in a .Rmd file and output to a PDF document.

在使用R和RStudio的可重复性研究中,有一个使用apsrtable()函数并排显示多个模型的示例.本书提供了以下代码(p.173-174):

In Reproducible Research with R and RStudio, there is an example using the apsrtable() function to display multiple models side-by-side. This book provides the following code (p. 173-174):

代码

\begin{table}
    \caption{Example Nested Estimates Table with \emph{aprstable}}
    \label{BasicApsrTableExample}
        \begin{center}
<<results= asis , echo=FALSE>>=
# Load apsrtable package
library(apsrtable)
# Create nested regression model table
apsrtable(M1, M2, M3, M4, M5, Sweave = TRUE,
      stars = "default")
@
       \end{center}
\end{table}

其中,使用M2 <- lm(Examination ~ Education + Agriculture, data = swiss)批量创建模型M1 ... M5.

where the models M1 ... M5 are created in chunks using M2 <- lm(Examination ~ Education + Agriculture, data = swiss).

输出

以下是书中所报告的结果的屏幕抓​​图.这正是我要在.Rmd文件中创建并输出到PDF文档的表.

Below is a screen grab of the results, as reported in the book. This is exactly the table I want to create in my .Rmd file and output to a PDF document.

尝试1 当我尝试在代码内 中使用此代码时(如下所示),并将其输出为PDF,我收到一条错误消息:Error: $ operator is invalid for atomic vectors

Attempt 1 When I try to use this code inside a code chunk—as shown below—and the output to a PDF, I get a an error message: Error: $ operator is invalid for atomic vectors

```{r}
t.model2 = xtable(model2,label = NULL)
t.model3 = xtable(model3,label = NULL)

library(apsrtable)

apsrtable(t.model2, t.model3, Sweave = TRUE, stars = "default")
```

尝试2 当我在代码块的 outside 中使用上述代码时,.Rmd文件输出为PDF,但显示以下内容:

Attempt 2 When I use the above code outside a code chunk, the .Rmd file outputs to PDF, but displays the following:

我的问题

  • 为什么这些尝试失败了?
  • 正确的使用方法是 .cmd中的apsrtable函数?
  • 该方法是否可以将该.Rmd文件输出为PDF?
  • Why are these attempts failing?
  • What is the correct way to use the apsrtable function inside a .Rmd?
  • Will this method work to ouput this .Rmd file to PDF?

相关的堆栈溢出问题

在以下位置包含apsrtable(或stargazer)输出一个Rmd文件

推荐答案

您需要注意以下两点:

  • 块选项results='asis'
  • \usepackage{dcolumn}必须位于帮助文件中所述的序言中.
  • Chunk option results='asis'
  • \usepackage{dcolumn} must be in the preamble as stated in the help file.

另一个选择是 stargazer 软件包,该软件包不仅可以编织PDF,但也可以转换为HTML(请参见屏幕截图).

Another option would be the stargazer package, which allows to knit not only to PDF but to HTML as well (see screenshot).

---
title: "stargazer"
author: "hplieninger"
date: "3 August 2018"
output: pdf_document
header-includes:
    - \usepackage{dcolumn}
---

```{r}
m1 <- lm(Fertility ~ Education , data = swiss)
m2 <- lm(Fertility ~ Education + Agriculture, data = swiss)
m3 <- lm(Fertility ~ . , data = swiss)
```

```{r, results='asis'}
apsrtable::apsrtable(m1, m2, m3, Sweave = TRUE)
```

```{r, results='asis'}
# If output: pdf_document
stargazer::stargazer(m1, m2, m3)
# If output: html_document
# stargazer::stargazer(m1, m2, m3, type = "html")
```

这篇关于在Rmarkdown中使用apsrtable的多个lm()模型的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆