Handling Latex backslashes in xtable - r

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.

Related

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.

Combine texreg, knitr, booktabs & dcolumn

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}

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

Use xtable to produce a Latex table with significance stars (***)

I am currently using xtable to generate Latex tables from R. It works fine, but in one of the tables I have significance stars to some of the numbers. Something like this dataframe X:
1 2 3 4 5 Test1 Test2 Test3
a "1.34" "0.43" "-0.26" "0.13" "0.05" "3.35^{.}" "343^{***}" "3244^{***}"
b "2.02" "2.17" "-3.19" "4.43" "1.43" "390.1^{***}" "31.23^{***}" "24^{***}"
c "23.07" "32.1" "24.3" "3.89" "0.4" "429.38^{***}" "17.04^{***}" "2424^{***}"
d "21.48" "14.45" "14.19" "22.04" "0.15" "385.17^{***}" "2424^{***}" "2424^{***}"
I am using '^' before the stars because in Latex significance stars look better in that format. The other option would be:
a "1.34" "0.43" "-0.26" "0.13" "0.05" "3.35." "343***" "3244***"
b "2.02" "2.17" "-3.19" "4.43" "1.43" "390.1***" "31.23^***" "24***"
# etc.
If I use xtable via:
print(xtable(X, label="X"),
size="normalsize",
include.rownames=FALSE,
include.colnames=TRUE,
caption.placement="top",
hline.after=NULL
)
I get an output like the following:
\begin{table}[ht]
\centering
{\normalsize
\begin{tabular}{llllllll}
1 & 2 & 3 & 4 & 5 & Test1 & Test2 & Test3 \\
242 & 123 & -42.3 & 0.43 & 34 & 3.35\verb|^|\{.\} # Hhere is the problem: \verb
& 242.58\verb|^|\{***\} & 0.06\verb|^|\{***\} \\ # etc. etc.
\end{tabular}
}
\end{table}
The problem here is the \verb which was added. If xtable didn't add it, the table would be fine for me. So my question is: Is there a way around that? I just want significane stars which are in the format:
^{***}
in the Latex table, but produced in R already, so I can quickly produce new tables in the right format.
Right now I am using the following function to create the stars, then I use 'paste' in a different function (not shown) to add them to the tests in the respective cases:
symnum(s[[p]], corr = FALSE, cutpoints = c(0, .001,.01,.05, .1, 1),
symbols = c("^{***}","^{**}","^{*}","^{.}"," "))
But maybe there is a better solution. Let me know.
Try setting sanitize.text.function = function(x) x to turn off the sanitizing of non-numeric values.
However, I would also recommend not using stars at all.

Adding custom row(s) to an LaTeX-outputted R table of regression results using memisc, xtable etc.

It's common practice for tables of regression outcomes in academic papers to have a row(s) that describe some feature of the estimated model. For example, you might have a row name:
"Model included individual fixed effects" and then each associated cell will have a Yes/No as appropriate.
My question is whether it is possible in any of the many tools for making LaTeX tables with R (c.f., Tools for making latex tables in R) to pass the table-generating functions such a row To make this more concrete, I'm imagining having a parameter like:
model.info.row <- list(name = "Fixed effects", values = c("Y", "N", "Y"))
I've read through the memisc mtable and toLaTeX documentation and did not see anything that seems capable of this---not sure about other packages / approaches, but this seems like such a common use case that I suspect there is some way of doing this.
You might try to add that new line(s) directly to the table you want to pass to e.g. xtable. Really lame example:
Let us have some model:
m <- lm(mtcars$hp ~ mtcars$wt)
Grab the table which is returned in xtable and other helpers:
df <- as.data.frame(summary(m)$coefficient)
Add a new line with some values:
df[3, ] <- c(sample(c('foo', 'bar'), 4, replace = TRUE))
Update the rowname of your custom line:
rownames(df)[3] <- 'FOOBAR'
Check out results:
> df
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.82092177119464 32.3246158121787 -0.0563323561763288 0.95545056134944
mtcars$wt 46.1600502824445 9.62530003926982 4.79569988406785 4.14582744107531e-05
FOOBAR bar foo bar bar
Or just call xtable:
> xtable(df)
% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Tue Jun 12 01:39:46 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rllll}
\hline
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & -1.82092177119464 & 32.3246158121787 & -0.0563323561763288 & 0.95545056134944 \\
mtcars\$wt & 46.1600502824445 & 9.62530003926982 & 4.79569988406785 & 4.14582744107531e-05 \\
FOOBAR & bar & foo & bar & bar \\
\hline
\end{tabular}
\end{center}
\end{table}
I ended up writing some hacky R code (note that it only works on a system w/ sed, wc and awk available) that's more flexible and works well the memisc 'mtable' function, which is my preferred way of generating LaTeX tables. Basically you write your table to a text file, then call this function with (1) the line number in the file where you want to make an insertion (2) the line you want to insert and (3) the name of the file with you want to make the insertions into (note that this function will overwrite your existing file). The code is:
insert.note <-function(linenumber, line, file){
num.lines <- as.numeric(system(paste("wc", file, "| awk '{print $1}'"), intern=TRUE))
tmp <- tempfile()
system(paste("head -n ", linenumber, file, "> ", tmp))
sink(tmp, append=TRUE)
cat(line)
sink()
system(paste("tail -n", num.lines - linenumber, file, ">>", tmp))
system(paste("mv", tmp, file))
}
As a helper function, this code creates a valid line of LaTeX using mtable's double & column spacing:
create.note <- function(l, include.row.end = TRUE){
n <- length(l)
s <- ""
i <- 1
for(note in l){
if(i < n){
cap <- "&&"
} else {
if(include.row.end){
cap <- "\\\\ \n "
} else {
cap <- " \n"
}
}
s <- paste(s, note, cap)
i <- i + 1
}
s
}
The include.row.end parameter is in case you want to pass it something like "\midrule" and don't want to get an extra line.

Resources