Rmarkdown/knitr 中的 texreg

texreg in Rmarkdown/knitr

简单的问题:我可以使用 knitr 在 Rmarkdown 的降价(不是 LaTex)文档中使用 texreg 吗?

我以为我可以,但我好像错了?我可以在 Rmarkdown 中包含方程式的 LaTex 代码,但使用 texreg 的 tables 没有成功(至少当剩余文档是用 markdown 编写时。

我收到这个错误:

pandoc document conversion failed with error 43

无论我使用 texreg()(对于 LatTex)、htmlreg() 还是 screenreg(),我都没有在编织的 PDF 文档中得到像样的 table .

---
title: "Title"
fontsize: 12pt
output:
   pdf_document:
   latex_engine: xelatex
---

然后尝试开发一个table:

```{r Table_with_texreg, results="asis"}
library(texreg)
modellist = list(model1, model2, model3, model4, model5)

texreg(modellist, type = 'un', 
summaries = c('ChiSqM_Value', 'ChiSqM_DF', 'ChiSqM_DF', 'ChiSqM_PValue', 'CFI',
'RMSEA_Estimate'), single.row=TRUE)

texreg() 之后的一些参数是 MplusAutomation 包的命令,指定要使用哪些参数(非标准化)和哪个模型拟合指数。对question/post.)

这是编织时生成的LaTex代码,导致转换错误43:

\begin{tabular}{l c c c c c }
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 & Model 5 \
\hline
 DISCRIM$<$-AGEA           & $-0.00 \; (0.00)^{***}$ & $-0.01 \; (0.00)^{***}$ & $-0.01 \; (0.00)^{***}$ & $-0.00 \; (0.00)^{***}$ & $-0.01 \; (0.00)^{***}$ \
 DISCRIM$<$-GNDR           & [=14=].05 \; (0.02)^{**}$   & [=14=].07 \; (0.02)^{***}$  & [=14=].09 \; (0.02)^{***}$  & [=14=].03 \; (0.02)^{*}$    & [=14=].07 \; (0.02)^{***}$  \
 DISCRIM$<$-MINORITY       & [=14=].14 \; (0.04)^{***}$  & [=14=].17 \; (0.04)^{***}$  & [=14=].13 \; (0.04)^{***}$  & [=14=].19 \; (0.04)^{***}$  & [=14=].19 \; (0.04)^{***}$  \
 PDJSEX\_R$<$-AGEA         & $-0.01 \; (0.00)^{***}$ &                         &                         & $-0.01 \; (0.00)^{***}$ &                         \
 PDJSEX\_R$<$-GNDR         & [=14=].38 \; (0.03)^{***}$  &                         &                         & [=14=].38 \; (0.03)^{***}$  &                         \
 PDJSEX\_R$<$-MINORITY     & [=14=].11 \; (0.04)^{**}$   &                         &                         & [=14=].11 \; (0.04)^{**}$   &                         \
 PDJETN\_R$<$-AGEA         & $-0.00 \; (0.00)^{***}$ &                         & $-0.00 \; (0.00)^{***}$ &                         &                         \
 PDJETN\_R$<$-GNDR         & $-0.02 \; (0.02)$       &                         & $-0.02 \; (0.02)$       &                         &                         \
 PDJETN\_R$<$-MINORITY     & [=14=].94 \; (0.08)^{***}$  &                         & [=14=].93 \; (0.08)^{***}$  &                         &                         \
 PREDJ\_R$<$-DISCRIM       & .00 \; (0.00)$        & .00 \; (0.00)$        & .00 \; (0.00)$        & .00 \; (0.00)$        & .00 \; (0.00)$        \
 LKRSP\_R$<$-DISCRIM       & .05 \; (0.01)^{***}$  & .08 \; (0.01)^{***}$  & .08 \; (0.01)^{***}$  & .08 \; (0.01)^{***}$  &                         \
 TRTBD\_R$<$-DISCRIM       & .02 \; (0.01)^{***}$  & .05 \; (0.01)^{***}$  & .05 \; (0.01)^{***}$  & .05 \; (0.01)^{***}$  &                         \
 PDJSEX\_R$<$-$>$DISCRIM   & [=14=].74 \; (0.02)^{***}$  &                         &                         & [=14=].76 \; (0.02)^{***}$  &                         \
 PDJETN\_R$<$-$>$DISCRIM   & [=14=].62 \; (0.02)^{***}$  &                         & [=14=].66 \; (0.02)^{***}$  &                         &                         \
 PDJETN\_R$<$-$>$PDJSEX\_R & [=14=].78 \; (0.02)^{***}$  &                         &                         &                         &                         \
 PREDJ_R<-Thresholds     & [=14=].19 \; (0.10)^{*}$    & [=14=].19 \; (0.10)^{*}$    & [=14=].18 \; (0.10)$        & [=14=].18 \; (0.10)$        & [=14=].18 \; (0.10)$        \
 PREDJ_R<-Thresholds     & [=14=].71 \; (0.09)^{***}$  & [=14=].71 \; (0.09)^{***}$  & [=14=].70 \; (0.09)^{***}$  & [=14=].70 \; (0.09)^{***}$  & [=14=].70 \; (0.09)^{***}$  \
 LKRSP_R<-Thresholds     & [=14=].14 \; (0.07)$        & [=14=].13 \; (0.07)$        & [=14=].15 \; (0.07)^{*}$    & [=14=].14 \; (0.07)$        & [=14=].14 \; (0.07)$        \
 LKRSP_R<-Thresholds     & [=14=].72 \; (0.07)^{***}$  & [=14=].72 \; (0.07)^{***}$  & [=14=].73 \; (0.07)^{***}$  & [=14=].72 \; (0.07)^{***}$  & [=14=].72 \; (0.07)^{***}$  \
 TRTBD_R<-Thresholds     & [=14=].51 \; (0.06)^{***}$  & [=14=].51 \; (0.06)^{***}$  & [=14=].50 \; (0.06)^{***}$  & [=14=].51 \; (0.06)^{***}$  & [=14=].50 \; (0.06)^{***}$  \
 TRTBD_R<-Thresholds     & .11 \; (0.07)^{***}$  & .11 \; (0.07)^{***}$  & .12 \; (0.07)^{***}$  & .11 \; (0.07)^{***}$  & .11 \; (0.07)^{***}$  \
 PDJSEX_R<-Thresholds    & [=14=].86 \; (0.06)^{***}$  & [=14=].86 \; (0.06)^{***}$  & [=14=].87 \; (0.06)^{***}$  & [=14=].86 \; (0.06)^{***}$  & [=14=].86 \; (0.06)^{***}$  \
 PDJSEX_R<-Thresholds    & .44 \; (0.06)^{***}$  & .44 \; (0.06)^{***}$  & .44 \; (0.06)^{***}$  & .44 \; (0.06)^{***}$  & .44 \; (0.06)^{***}$  \
 PDJETN_R<-Thresholds    & [=14=].80 \; (0.07)^{***}$  & [=14=].79 \; (0.07)^{***}$  & [=14=].80 \; (0.07)^{***}$  & [=14=].79 \; (0.07)^{***}$  & [=14=].80 \; (0.07)^{***}$  \
 PDJETN_R<-Thresholds    & .29 \; (0.09)^{***}$  & .29 \; (0.09)^{***}$  & .29 \; (0.09)^{***}$  & .29 \; (0.09)^{***}$  & .29 \; (0.09)^{***}$  \
 DISCRIM$<$-$>$DISCRIM     & [=14=].79 \; (0.02)^{***}$  & [=14=].74 \; (0.02)^{***}$  & [=14=].74 \; (0.02)^{***}$  & [=14=].75 \; (0.02)^{***}$  & [=14=].84 \; (0.01)^{***}$  \
 PDJSEX\_R$<$-DISCRIM      &                         & .03 \; (0.01)^{***}$  & .03 \; (0.01)^{***}$  &                         & [=14=].98 \; (0.01)^{***}$  \
 PDJETN\_R$<$-DISCRIM      &                         & [=14=].90 \; (0.01)^{***}$  &                         & [=14=].90 \; (0.01)^{***}$  & [=14=].87 \; (0.01)^{***}$  \
 LKRSP\_R$<$-AGEA          &                         &                         &                         &                         & $-0.01 \; (0.00)^{***}$ \
 LKRSP\_R$<$-GNDR          &                         &                         &                         &                         & [=14=].06 \; (0.02)^{***}$  \
 LKRSP\_R$<$-MINORITY      &                         &                         &                         &                         & [=14=].14 \; (0.04)^{***}$  \
 TRTBD\_R$<$-AGEA          &                         &                         &                         &                         & $-0.00 \; (0.00)^{**}$  \
 TRTBD\_R$<$-GNDR          &                         &                         &                         &                         & [=14=].06 \; (0.02)^{**}$   \
 TRTBD\_R$<$-MINORITY      &                         &                         &                         &                         & [=14=].20 \; (0.05)^{***}$  \
 LKRSP\_R$<$-$>$DISCRIM    &                         &                         &                         &                         & [=14=].74 \; (0.02)^{***}$  \
 TRTBD\_R$<$-$>$DISCRIM    &                         &                         &                         &                         & [=14=].72 \; (0.02)^{***}$  \
 TRTBD\_R$<$-$>$LKRSP\_R   &                         &                         &                         &                         & [=14=].88 \; (0.01)^{***}$  \
\hline
ChiSqM_Value               & 331.41                  & 808.73                  & 776.77                  & 568.88                  & 648.83                  \
ChiSqM_DF                  & 10                      & 17                      & 14                      & 14                      & 10                      \
ChiSqM_PValue              & 0.00                    & 0.00                    & 0.00                    & 0.00                    & 0.00                    \
CFI                        & 0.97                    & 0.93                    & 0.94                    & 0.95                    & 0.95                    \
RMSEA_Estimate             & 0.02                    & 0.03                    & 0.03                    & 0.03                    & 0.03                    \
\hline
\multicolumn{6}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}

P.S。编织到 HTML(不是 PDF)并使用 htmlreg(不是 texreg)似乎工作正常。不过,我更愿意使用 PDF。

您写的是要生成 PDF,但不使用 LaTeX。你确定吗?我不知道该怎么做,但这里有两种解决方案,用于生成 a) HTML 输出和 b) 使用 LaTeX 的 PDF,均使用 texreg:

解决方案 1(对于 HTML 输出):

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo = FALSE, message = FALSE}
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)

library("texreg")
```

Some equation:
$$a + b = 2$$

Table follows:
```{r, results = 'asis', echo = FALSE}
htmlreg(lm.D9, star.symbol = "\*", doctype = FALSE, center = FALSE, caption = "")
```

这是输出:

方案二(PDF输出):

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo = FALSE, message = FALSE}
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)

library("texreg")
```

Some equation:
$$a + b = 2$$

Table follows:
```{r, results = 'asis', echo = FALSE}
texreg(lm.D9, table = FALSE, use.packages = FALSE)
```

这是输出: