R: math notation in data.frame - r

> 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.

Related

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

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

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.

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