Combine texreg, knitr, booktabs & dcolumn - r

I'm trying to compile a LaTeX report using RStudio and knitr. I'm having a hard time getting the packages booktabs and dcolumn to work with my texreg-generated table.
As an example, I am trying to recreate Table 2 in this example:.
My attempt as a .Rnw -file is below:
\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
<<setup, include=FALSE >>=
library(texreg)
#
\begin{document}
<<analysis, include=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)
m1 <- lm(weight ~ group)
m2 <- lm(weight ~ group - 1) # omitting intercept
#
<<table, results='asis'>>=
texreg(m2)
#
\end{document}
However, the generated LaTex table (below) includes neither the booktabs horizontal lines nor the dcolumn alignment. How to incorporate them? Many thanks for help!
\begin{table}
\begin{center}
\begin{tabular}{l c }
\hline
& Model 1 \\
\hline
groupCtl & $5.03^{***}$ \\
& $(0.22)$ \\
groupTrt & $4.66^{***}$ \\
& $(0.22)$ \\
\hline
R$^2$ & 0.98 \\
Adj. R$^2$ & 0.98 \\
Num. obs. & 20 \\
\hline
\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

Just to clarify: the crucial part in Robert's solution is the argument use.packages=FALSE not the seperation of code into two chunks.
The reason is the following: The way you call texreg() now makes it include the following in the tex output it produces:
\usepackage{booktabs}
\usepackage{dcolumn}
Saving the output in an object and then using cat() does not solve this.
You cannot use \usepackage()' outside the preamble. Knitr still compiles a PDF but apparently this use of\usepackage{}' in the document body screws up the use of booktabs and dcolumn even if you've loaded them in the preamble.
Add the argument use.packages=FALSE in texreg() - if set to FALSE, the use package statements are omitted from the output. Write the use package statements into the preamble of your document yourself and you'll have the regression table with booktabs and aligned numbers.

Try this:
\begin{document}
<<analysis, include=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)
m1 <- lm(weight ~ group)
m2 <- lm(weight ~ group - 1) # omitting intercept
table = texreg(m2,booktabs = TRUE,dcolumn = TRUE,use.packages=FALSE)
table2=texreg(list(m1,m2),booktabs = TRUE,dcolumn = TRUE,use.packages=FALSE)
#
<<table, results='asis',echo=FALSE>>=
cat(table)
cat(table2)
#
\end{document}

Related

Why is LaTeX failing to compile in RMarkdown

My code should be a pretty easy knit to a pdf, but it will not compile and I'm getting this message in R Markdown:
! LaTeX Error: Unicode character ₁ (U+2081)
not set up for use with LaTeX.
Error: LaTeX failed to compile L-work-5.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See L- work-5.log for more info.
Execution halted
here is the code:
---
title: "work 5"
author: "PLars"
date: "4/2/2022"
output: pdf_document
fonttheme: professionalfonts
fontsize: 12pt
editor_options:
markdown:
wrap: 72
---
```{r, echo = FALSE, results = "hide", message = FALSE, purl = FALSE}
library(knitr)
opts_chunk$set(tidy = FALSE,
fig.align = "left",
background = '#a6a6a6',
fig.width = 10,
fig.height = 10,
out.width ="\\linewidth",
out.height = "\\linewidth",
message = FALSE,
warning = FALSE,
fig.align = "left"
)
options(width = 55, digits = 3)
library(scales)
percent <- function(x, digits = 2, format = "f", ...) {
paste0(formatC(100 * x, format = format, digits = digits, ...), "%")
}
library(haven)
library(tinytex)
library(stargazer)
library(tidyverse)
library(texreg)
library(dplyr)
library(texreg)
library(AER)
library(tidyverse)
```
**Part I - Categorical Models (5 points)**
Say that you estimate an ordered logit model with a three category
dependent variable and two independent variables, X~₁i~ and X~₂i~, and
obtain the following results:
```{=tex}
\begin{center}
\begin{tabular}{c|rc}
\hline \hline
& $\hat{\beta}$ & SE \\
\hline
$X_{1}$ & $-0.68$ & $(0.23)$ \\
$X_{2}$ & $-0.47$ & $(0.13)$ \\
\hline
$\tau_1$ & $-1.02$ & $(0.46)$ \\
$\tau_2$ & $.85$ & $(0.21)$ \\
\hline
\end{tabular}
\end{center}
```
```{=tex}
\begin{enumerate}
\item Calculate $\Pr(Y_i=1 | X_{1i}=1, X_{2i}=0)$.
\item Calculate $\Pr(Y_i=2 | X_{1i}=1, X_{2i}=0)$.
\item Calculate $\Pr(Y_i=3 | X_{1i}=1, X_{2i}=0)$.
\item Calculate the first difference (difference in probability in category) that result from changing X_{2i} from -2 to 2, holding X_{1i} fixed at 0. Do calculations for each possible value of Y_i.
\item Explain how we might assess whether the parallel regression assumption holds for this model? If it does not, what alternative might you pursue if this were your model?
\end{enumerate}
```
#
First, we calculate $X_i \beta$
```{r}
(xiB <- (-.068*1) + (-0.47*0))
```
Then, plug into the following equations:
```{r}
(prob1 <- 1/(1 + exp(-(-1.02-xiB))))
(prob2 <- 1/(1 + exp(-(.85-xiB))) - prob1)
(prob3 <- 1 - (1/(1 + exp(-(.85-xiB)))))
prob1 + prob2 + prob3
```
For a start, try deleting the special characters ₁ and ₂ in the line
two independent variables, X~₁i~ and X~₂i~
This will let you compile.
You might be able to get this to work by including something like
\newunicodechar{₁}{\ensuremath{{}_1}}
and similarly for the subscript-2 character, at the top of your file (from this TeX Stack Exchange question), but I haven't tested it and don't want to go down that rabbit hole right now ...
Or just change the relevant text to
two independent variables, $X_{1i}$ and $X_{2i}$
which will probably typeset it as originally intended!

R: math notation in data.frame

> df = data.frame(Parameters = c(expression(beta[1])))
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""expression"" to a data.frame
I'm trying to write math notation in a data.frame, but it seems that the two are not compatible. Is there a way around this?
I have also tried
> data.frame(Parameters = paste(expression(beta[1])))
Parameters
1 beta[1]
How can I get to show up?
If you want to store the latex code for those symbols inside a dataframe then be able to generate correct latex code from xtable, you will need to override the sanitize function in print.xtable by feeding in a dummy function that returns the input exactly (See this question: Using xtable with R and Latex, math mode in column names?):
df = data.frame(Parameter = c("$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
Estimate = beta, row.names = 1)
print(xtable(t(df)), sanitize.text.function = function(x){x})
Latex Table:
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
\hline
& $\beta_{0}$ & $\beta_{1}$ & $\beta_{2}$ \\
\hline
Estimate & 0.05 & 0.10 & 0.15 \\
\hline
\end{tabular}
\end{table}
Similar to xtable, stargazer has some cool options to generate nice looking tables in latex. One thing you can do is to change the variable names to math notation using the covariate.labels argument in stargazer:
library(stargazer)
beta = 1:3*0.05
df = data.frame(Parameter = c("beta0", "beta1", "beta2"),
Estimate = beta, row.names = 1)
stargazer(t(df), covariate.labels = c(NA, "$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
header = FALSE, summary = FALSE)
This outputs a latex table code:
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}} cccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& $\beta_{0}$ & $\beta_{1}$ & $\beta_{2}$ \\
\hline \\[-1.8ex]
Estimate & $0.050$ & $0.100$ & $0.150$ \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
You can copy and paste the code here to render the latex table.
Also note that the default for type= in stargazer is "latex", which generates latex code, but you can also specify type="text" to generate a table in your console. This option, however, does not allow you to render the math symbols.
stargazer(t(df), covariate.labels = c(NA, "$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
header = FALSE, summary = FALSE, type = "text")
# ==========================
# 0 1 2
# --------------------------
# Estimate 0.050 0.100 0.150
# --------------------------
Another option using my package:
library(huxtable)
dfr = data.frame(Parameter = c("$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
Estimate = 'beta')
ht <- as_hux(dfr)
escape_contents(ht) <- FALSE
ht # will print as TeX within a markdown pdf_document
I am not very sure what you are trying to do here. If you are trying to create a dataframe df with a column named "Parameter" with values taken from a vector within a list beta, then the below code will do the job.
df = data.frame(Parameters = beta[[1]])
# Assuming that the first object in beta is a vector that you want to set as "Paramters" column.
Please provide more information as to what these objects are if this is not what you were looking for.

Right-align LaTeX longtables through R's print.xtable

Say I have the following program in R to generate a LaTeX longtable:
library(xtable)
tabela <- xtabs(Temp ~ Month, airquality)
xtabela <- xtable(tabela)
print.xtable(xtabela, tabular.environment = 'longtable', floating = FALSE)
Which yields
\begin{longtable}{rr}
\hline
& Month \\
\hline
5 & 2032.00 \\
6 & 2373.00 \\
7 & 2601.00 \\
8 & 2603.00 \\
9 & 2307.00 \\
\hline
\hline
\end{longtable}
However, I want this table to be completely aligned to the right. In LaTeX, I just need to use \begin{longtable}[r]{rr} in order to accomplish this, but how do I pass this [r] argument through R's print.xtable? Alternatively, how do I achieve the same result through other methods (I've tried \raggedleft, but it only works with regular tabular objects)?
As a very rough method, you could do:
cat(paste(c("\\begin{longtable}[r]{", align(xt), "}\n"), collapse=""))
print(xtabella, only.contents=T)
cat("\\end{longtable}\n")

How to remove "Standard Error" column from xtable() output of an lm on R/RSweave/LaTeX

I'm currently doing some data analysis on population data, so reporting the standard errors in the tables of parameter coefficients just doesn't really make statistical sense. I've done a fair bit of searching and can't find any way to customize the xtable output to remove it. Can anyone point me in the right direction?
Thanks a lot, I didn't post this lightly; if it's something obvious, I apologize for having wasted time!
so after my (other) whole long-winded answer... this works too:
xtable(summary(model1)$coefficients[,c(1,3,4)])
Or more generically:
sm <- summary(SomeModel)
SE.indx <- which(colnames(sm$coefficients) == "Std. Error") # find which column is Std. Error (usually 2nd)
sm$coefficients <- sm$coefficients[, -SE.indx] # Remove it
xtable(sm$coefficients) # call xtable on just the coefficients table
Results:
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sun Dec 9 00:01:46 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
\hline
& Estimate & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & 29.80 & 30.70 & 0.00 \\
crim & -0.31 & -6.91 & 0.00 \\
age & -0.09 & -6.50 & 0.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
Using the first example in help(lm):
xtable(as.matrix(coef(lm.D9)))
% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Sat Dec 8 19:53:09 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rr}
\hline
& x \\
\hline
(Intercept) & 5.03 \\
groupTrt & -0.37 \\
\hline
\end{tabular}
\end{center}
\end{table}
I agreed with not using std erros if this were descriptions of a population and not just a sample. By that reasoning, however, you would not want to leave in p-values or t-statistics. That was the reason I only included the coefficients. To remove the standard error column only from the summary coefficient matrix:
xtable( coef(summary(lm.D9))[,-2] )
% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Sat Dec 8 21:02:17 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
\hline
& Estimate & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & 5.03 & 22.85 & 0.00 \\
groupTrt & -0.37 & -1.19 & 0.25 \\
\hline
\end{tabular}
\end{center}
\end{table}
Looking at str(summary(Model1)) we see that $coefficients has the Std. Error value we want to remove.
lesserSummary <- function(x) {
## returns same as summary(x), but with "Std. Error" remove from coefficients.
## and class of object is "modifiedSummary"
# grab the summary
sm <- summary(x)
# find which column is std error
SE.indx <- which(colnames(sm$coefficients) == "Std. Error")
# remove it
sm$coefficients <- sm$coefficients[, -SE.indx]
# give it some class
class(sm) <- "modifiedSummary"
# return it
sm
}
xtable.modifiedSummary <-
function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, ...) {
# x is a modifiedSummary object
# This function is a modification of xtable:::xtable.summary.lm
# Key Difference is simply the number of columns that x$coef is expected to have
# (Here 3. Originally 4)
x <- data.frame(x$coef, check.names = FALSE)
class(x) <- c("xtable", "data.frame")
caption(x) <- caption
label(x) <- label
align(x) <- switch(1 + is.null(align), align, c("r", "r", "r", "r"))
digits(x) <- switch(1 + is.null(digits), digits, c(0, 4, 2, 4))
display(x) <- switch(1 + is.null(display), display, c("s", "f", "f", "f"))
return(x)
}
xtable_mod <- function(x) {
# Wrapper function to xtable.modified summary, calling first lesserSummary on x
xtable(lesserSummary(x))
}
EXAMPLE:
xtable_mod(model1)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sat Dec 8 23:44:54 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
\hline
& Estimate & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & 29.8007 & 30.70 & 0.0000 \\
crim & -0.3118 & -6.91 & 0.0000 \\
age & -0.0896 & -6.50 & 0.0000 \\
\hline
\end{tabular}
\end{center}
\end{table}
Below are the steps taken to arrive at the above conclusion.
You can likely modify the call to xtable, but you first need to follow it down a bit:
start by looking at the source for xtable:
xtable
# function (x, caption = NULL, label = NULL, align = NULL, digits = NULL,
# display = NULL, ...)
# {
# UseMethod("xtable")
# }
# <environment: namespace:xtable>
We see that it simply has a call to UseMethod(). So lets see which methods are available:
methods(xtable)
# [1] xtable.anova* xtable.aov* xtable.aovlist*
# [4] xtable.coxph* xtable.data.frame* xtable.glm*
# [7] xtable.lm* xtable.matrix* xtable.prcomp*
# [10] xtable.summary.aov* xtable.summary.aovlist* xtable.summary.glm*
# [13] xtable.summary.lm* xtable.summary.prcomp* xtable.table*
# [16] xtable.ts* xtable.zoo*
There are several. Note that the ones with an asterisk * are non-visible.
The method called is determined by the class of the object we are calling xtable on.
Let's say our output is Model1 We take a look at its class: '
class(Model1)
# [1] "lm"
So the source we want to look at is xtable.lm.
xtable.lm
# Error: object 'xtable.lm' not found
Error? That's right, it is non-visible. So we use the package name with triple-colons. Note: please be sure to read the notice in the help file ?":::"
xtable:::xtable.lm
# function (x, caption = NULL, label = NULL, align = NULL, digits = NULL,
# display = NULL, ...)
# {
# return(xtable.summary.lm(summary(x), caption = caption, label = label,
# align = align, digits = digits, display = display))
# }
# <environment: namespace:xtable>
We notice that xtable.lm calls xtable.summary.lm and passes as its first argument a summary(x), where x is our model.
So that leads us to two place to investigate: summary and xtable.summary.lm

Handling Latex backslashes in xtable

I have a table that includes the following column:
mytable <- data.frame(beta_0 = c(1,2,3)
What I want to do is output a table with a column header in latex markup, e.g. $\beta_0$
However, I can not seem to figure out how to output the "$\beta_0$" using print.xtable:
colnames(mytable) <- "$\beta_0$"
library(xtable)
print(xtable(mytable), include.rownames = F)
returns a column header of
\eta\_0\$
instead of
$\beta_0$
I presume that the answer is the "sanitize.colnames.function" argument to print.xtable, but it is not obvious to me how to use this, and ?print.xtable provides no examples.
Specifically, I would like to output a latex table like:
\begin{table}[ht]
\begin{center}
\begin{tabular}{r}
\hline
$\beta_0$ \\
\hline
1.00 \\
2.00 \\
3.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
Two issues here; first, you need a double backslash as otherwise it treats it as a control sequence. Second, by default, xtable sanitizes text so that it won't break LaTeX. Use one of the sanitize. parameters to control this; to do no sanitizing, pass it the identity function.
colnames(mytable) <- "$\\beta_0$"
print(xtable(mytable), include.rownames = F, sanitize.colnames.function = identity)
this is what did the trick for me:
mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
rownames(mat) <- "$y_{t-1}$"
colnames(mat) <- c("$R^2$", "$\\bar{x}$", "F-stat", "S.E.E", "DW")
mat <- xtable(mat)
print(mat, sanitize.text.function = function(x){x})
This way you avoid the backslash issue in the table text.

Resources