2

Simple question: can I use texreg in a markdown (not LaTex) document in Rmarkdown using knitr?

I thought I could, but it seems I'm wrong? I can include LaTex code within Rmarkdown for equations but have no success for tables with texreg (at least when the remaining document is written in markdown.

I get this error:

pandoc document conversion failed with error 43

Irrespectively of whether I use texreg() (for LatTex), htmlreg(), or screenreg(), I don't get a decent table in the knitted PDF document.

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

Then trying to develop a 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)

(Some of the arguments after texreg() are commands for the MplusAutomation package, specifying which parameters (unstandardized) and which model fit indices to use. Shouldn't be of any significance for the question/post.)

This is the LaTex code generated when knitting, resulting in coversion error 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           & $0.05 \; (0.02)^{**}$   & $0.07 \; (0.02)^{***}$  & $0.09 \; (0.02)^{***}$  & $0.03 \; (0.02)^{*}$    & $0.07 \; (0.02)^{***}$  \\
 DISCRIM$<$-MINORITY       & $0.14 \; (0.04)^{***}$  & $0.17 \; (0.04)^{***}$  & $0.13 \; (0.04)^{***}$  & $0.19 \; (0.04)^{***}$  & $0.19 \; (0.04)^{***}$  \\
 PDJSEX\_R$<$-AGEA         & $-0.01 \; (0.00)^{***}$ &                         &                         & $-0.01 \; (0.00)^{***}$ &                         \\
 PDJSEX\_R$<$-GNDR         & $0.38 \; (0.03)^{***}$  &                         &                         & $0.38 \; (0.03)^{***}$  &                         \\
 PDJSEX\_R$<$-MINORITY     & $0.11 \; (0.04)^{**}$   &                         &                         & $0.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     & $0.94 \; (0.08)^{***}$  &                         & $0.93 \; (0.08)^{***}$  &                         &                         \\
 PREDJ\_R$<$-DISCRIM       & $1.00 \; (0.00)$        & $1.00 \; (0.00)$        & $1.00 \; (0.00)$        & $1.00 \; (0.00)$        & $1.00 \; (0.00)$        \\
 LKRSP\_R$<$-DISCRIM       & $1.05 \; (0.01)^{***}$  & $1.08 \; (0.01)^{***}$  & $1.08 \; (0.01)^{***}$  & $1.08 \; (0.01)^{***}$  &                         \\
 TRTBD\_R$<$-DISCRIM       & $1.02 \; (0.01)^{***}$  & $1.05 \; (0.01)^{***}$  & $1.05 \; (0.01)^{***}$  & $1.05 \; (0.01)^{***}$  &                         \\
 PDJSEX\_R$<$-$>$DISCRIM   & $0.74 \; (0.02)^{***}$  &                         &                         & $0.76 \; (0.02)^{***}$  &                         \\
 PDJETN\_R$<$-$>$DISCRIM   & $0.62 \; (0.02)^{***}$  &                         & $0.66 \; (0.02)^{***}$  &                         &                         \\
 PDJETN\_R$<$-$>$PDJSEX\_R & $0.78 \; (0.02)^{***}$  &                         &                         &                         &                         \\
 PREDJ_R$1<-Thresholds     & $0.19 \; (0.10)^{*}$    & $0.19 \; (0.10)^{*}$    & $0.18 \; (0.10)$        & $0.18 \; (0.10)$        & $0.18 \; (0.10)$        \\
 PREDJ_R$2<-Thresholds     & $0.71 \; (0.09)^{***}$  & $0.71 \; (0.09)^{***}$  & $0.70 \; (0.09)^{***}$  & $0.70 \; (0.09)^{***}$  & $0.70 \; (0.09)^{***}$  \\
 LKRSP_R$1<-Thresholds     & $0.14 \; (0.07)$        & $0.13 \; (0.07)$        & $0.15 \; (0.07)^{*}$    & $0.14 \; (0.07)$        & $0.14 \; (0.07)$        \\
 LKRSP_R$2<-Thresholds     & $0.72 \; (0.07)^{***}$  & $0.72 \; (0.07)^{***}$  & $0.73 \; (0.07)^{***}$  & $0.72 \; (0.07)^{***}$  & $0.72 \; (0.07)^{***}$  \\
 TRTBD_R$1<-Thresholds     & $0.51 \; (0.06)^{***}$  & $0.51 \; (0.06)^{***}$  & $0.50 \; (0.06)^{***}$  & $0.51 \; (0.06)^{***}$  & $0.50 \; (0.06)^{***}$  \\
 TRTBD_R$2<-Thresholds     & $1.11 \; (0.07)^{***}$  & $1.11 \; (0.07)^{***}$  & $1.12 \; (0.07)^{***}$  & $1.11 \; (0.07)^{***}$  & $1.11 \; (0.07)^{***}$  \\
 PDJSEX_R$1<-Thresholds    & $0.86 \; (0.06)^{***}$  & $0.86 \; (0.06)^{***}$  & $0.87 \; (0.06)^{***}$  & $0.86 \; (0.06)^{***}$  & $0.86 \; (0.06)^{***}$  \\
 PDJSEX_R$2<-Thresholds    & $1.44 \; (0.06)^{***}$  & $1.44 \; (0.06)^{***}$  & $1.44 \; (0.06)^{***}$  & $1.44 \; (0.06)^{***}$  & $1.44 \; (0.06)^{***}$  \\
 PDJETN_R$1<-Thresholds    & $0.80 \; (0.07)^{***}$  & $0.79 \; (0.07)^{***}$  & $0.80 \; (0.07)^{***}$  & $0.79 \; (0.07)^{***}$  & $0.80 \; (0.07)^{***}$  \\
 PDJETN_R$2<-Thresholds    & $1.29 \; (0.09)^{***}$  & $1.29 \; (0.09)^{***}$  & $1.29 \; (0.09)^{***}$  & $1.29 \; (0.09)^{***}$  & $1.29 \; (0.09)^{***}$  \\
 DISCRIM$<$-$>$DISCRIM     & $0.79 \; (0.02)^{***}$  & $0.74 \; (0.02)^{***}$  & $0.74 \; (0.02)^{***}$  & $0.75 \; (0.02)^{***}$  & $0.84 \; (0.01)^{***}$  \\
 PDJSEX\_R$<$-DISCRIM      &                         & $1.03 \; (0.01)^{***}$  & $1.03 \; (0.01)^{***}$  &                         & $0.98 \; (0.01)^{***}$  \\
 PDJETN\_R$<$-DISCRIM      &                         & $0.90 \; (0.01)^{***}$  &                         & $0.90 \; (0.01)^{***}$  & $0.87 \; (0.01)^{***}$  \\
 LKRSP\_R$<$-AGEA          &                         &                         &                         &                         & $-0.01 \; (0.00)^{***}$ \\
 LKRSP\_R$<$-GNDR          &                         &                         &                         &                         & $0.06 \; (0.02)^{***}$  \\
 LKRSP\_R$<$-MINORITY      &                         &                         &                         &                         & $0.14 \; (0.04)^{***}$  \\
 TRTBD\_R$<$-AGEA          &                         &                         &                         &                         & $-0.00 \; (0.00)^{**}$  \\
 TRTBD\_R$<$-GNDR          &                         &                         &                         &                         & $0.06 \; (0.02)^{**}$   \\
 TRTBD\_R$<$-MINORITY      &                         &                         &                         &                         & $0.20 \; (0.05)^{***}$  \\
 LKRSP\_R$<$-$>$DISCRIM    &                         &                         &                         &                         & $0.74 \; (0.02)^{***}$  \\
 TRTBD\_R$<$-$>$DISCRIM    &                         &                         &                         &                         & $0.72 \; (0.02)^{***}$  \\
 TRTBD\_R$<$-$>$LKRSP\_R   &                         &                         &                         &                         & $0.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. Knitting to HTML (not PDF) and using htmlreg (not texreg) seems to work fine. I would have preferred to use PDF, though.

cibr
  • 453
  • 5
  • 16

1 Answers1

3

You are writing that you want to generate a PDF, but not use LaTeX. Are you sure about this? I don't know how to do that, but here are two solutions for generating a) HTML output and b) PDFs with LaTeX, both using texreg:

Solution 1 (for HTML output):

---
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 = "")
```

Here is the output:

HTML output

Solution 2 (for PDF output):

---
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)
```

Here is the output:

PDF output

Philip Leifeld
  • 2,253
  • 15
  • 24
  • Thanks for the comment, and thanks for developing a great package! Maybe I have to use LaTex. After just having learned the basics of R, I find learning LaTex as well is a pretty steep learning curve. (Generating PDF is no problem in Rmarkdown unless I add the table.) Thanks for the code! – cibr Feb 12 '17 at 20:17
  • Of course! Glad you find it useful! To be honest, though, I don't see why you would think that the second solution has a steeper learning curve. You still write Markdown code, it's just converted to LaTeX internally because you want to generate a PDF. As you can see, the code for both solutions is very similar. – Philip Leifeld Feb 12 '17 at 23:23
  • Thanks, your code works fine when knitting to PDF, but not mine... I added the LaTex code generated by texreg, this code results in a conversion error. – cibr Feb 13 '17 at 08:00
  • Thanks for providing a more detailed example in your updated question. You need to double-escaped underscores and the like. E.g., instead of `\_`, you should write `\\_` in the coefficient names. You can use the `custom.coef.names` argument to do that if you don't want to change the coefficient names inside your model. I don't know if that's the only problem, but that's definitely a show-stopper. If you need more help, you should provide a minimal self-contained example that reproduces your table from scratch. – Philip Leifeld Feb 14 '17 at 01:59
  • I think there are more problems in the coefficient names. For example, in `PREDJ_R$1<-Thresholds`, there is both an unescaped underscore and a single `$`, which usually tells LaTeX that it should expect some math to follow -- but you don't close the inline math block. – Philip Leifeld Feb 14 '17 at 02:04
  • Thanks for your help and your patience! The code in question is generated automatically by the MplusAutomation package, I have informed the developers of your comment. – cibr Feb 14 '17 at 09:07