Table 多个 lm() 模型在 Rmarkdown 中使用 apsrtable
Table of multiple lm() models using apsrtable in Rmarkdown
目标
将使用 lm()
函数创建的多个模型的结果一起呈现在格式良好的 table 中。此 table 将在 .Rmd 文件中生成并输出到 PDF 文档。
建议的解决方案
在使用 R 和 RStudio 的可重复研究 中,有一个使用 apsrtable()
函数并排显示多个模型的示例。本书提供如下代码(第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}
其中模型 M1 ... M5
是使用 M2 <- lm(Examination ~ Education + Agriculture, data = swiss)
.
分块创建的
输出
下面是书中报告的结果的屏幕截图。这正是我想在我的 .Rmd 文件中创建并输出到 PDF 文档的 table。
问题
尝试 1
当我尝试在 in 代码块中使用此代码时(如下所示)以及 PDF 的输出,我收到一条错误消息: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
当我在 外部 代码块中使用上述代码时,.Rmd 文件输出为 PDF,但显示以下内容:
问题
我的问题
- 为什么这些尝试都失败了?
- 正确的使用方法是什么
apsrtable
.Rmd 中的函数?
- 此方法能否将此 .Rmd 文件输出为 PDF?
相关堆栈溢出问题
Tools for making latex tables in R
Include apsrtable (or stargazer) output in an Rmd file
Combining several regression tables into one for use in xtable with Sweave in R
您需要注意以下两件事:
- 块选项
results='asis'
\usepackage{dcolumn}
必须如帮助文件中所述在序言中。
另一个选项是 stargazer 包,它不仅可以编织成 PDF,还可以编织成 HTML(见屏幕截图)。
---
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")
```
目标
将使用 lm()
函数创建的多个模型的结果一起呈现在格式良好的 table 中。此 table 将在 .Rmd 文件中生成并输出到 PDF 文档。
建议的解决方案
在使用 R 和 RStudio 的可重复研究 中,有一个使用 apsrtable()
函数并排显示多个模型的示例。本书提供如下代码(第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}
其中模型 M1 ... M5
是使用 M2 <- lm(Examination ~ Education + Agriculture, data = swiss)
.
输出
下面是书中报告的结果的屏幕截图。这正是我想在我的 .Rmd 文件中创建并输出到 PDF 文档的 table。
问题
尝试 1
当我尝试在 in 代码块中使用此代码时(如下所示)以及 PDF 的输出,我收到一条错误消息: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 当我在 外部 代码块中使用上述代码时,.Rmd 文件输出为 PDF,但显示以下内容:
问题
我的问题
- 为什么这些尝试都失败了?
- 正确的使用方法是什么
apsrtable
.Rmd 中的函数? - 此方法能否将此 .Rmd 文件输出为 PDF?
相关堆栈溢出问题
Tools for making latex tables in R
Include apsrtable (or stargazer) output in an Rmd file
Combining several regression tables into one for use in xtable with Sweave in R
您需要注意以下两件事:
- 块选项
results='asis'
\usepackage{dcolumn}
必须如帮助文件中所述在序言中。
另一个选项是 stargazer 包,它不仅可以编织成 PDF,还可以编织成 HTML(见屏幕截图)。
---
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")
```