R统计表格输出为latex:stargazer及其它

标签:
rlatextable |
分类: 15R语言 |
这个名字很炫的包是用来生成统计模型摘要的latex代码的。这样把生成的代码放到latex里边编译,就可以生成出版级的效果(显然这针对的是R输出的苍白无力)。
library(stargazer)
prostate <- read.csv("E:/RB/prostate.csv")
# 生成prostate的摘要
stargazer(prostate)
## \begin{table}[!htbp] \centering
## \caption{}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}}lccccc}
## \\[-1.8ex]\hline
## \hline \\[-1.8ex]
## Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\
## \hline \\[-1.8ex]
## lcavol & 97 & 1.350 & 1.179 & $-$1.347 & 3.821 \\
## age & 97 & 63.870 & 7.445 & 41 & 79 \\
## lbph & 97 & 0.100 & 1.451 & $-$1.386 & 2.326 \\
## lcp & 97 & $-$0.179 & 1.398 & $-$1.386 & 2.904 \\
## gleason & 97 & 6.753 & 0.722 & 6 & 9 \\
## lpsa & 97 & 2.478 & 1.154 & $-$0.431 & 5.583 \\
## \hline \\[-1.8ex]
## \normalsize
## \end{tabular}
## \end{table}
line1 <- lm(lcavol ~ ., data = prostate)
# 生成回归的摘要
stargazer(line1)
(略)
latex编译的效果是这样的:
除了这个占星师的包,还有好几个类似的可以把R输出转换为latex代码的包,比如texreg。
细节请参考: [1]stargazer:beautiful LATEX and ASCII tables from R statistical outputhttp://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf
[2]texreg: Conversion of Statistical Model Output in R to LATEX and HTML Tableshttp://cran.r-project.org/web/packages/texreg/vignettes/jss1020.pdf
Creating reasonable layouted LaTeX tables from R output was easier then expected. I should have googled it long ago…
install.packages("xtable")
Lets say you created a tabular output in R called “tab1″, e.g by
doing:
data(CO2)
tab1 <- with(CO2, table(Treatment , Type))
tab1
your text output in R would look like
Type
Treatment Quebec Mississippi
nonchilled 21 21
chilled 21 21
Now you would like this or whatever table
or
library(xtable)
xtable(tab1)
and the output will be:
% latex table generated in R 2.9.0 by xtable 1.5-5 package
% Wed Jul 08 16:20:54 2009
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& Quebec & Mississippi \\
\hline
nonchilled & 21 & 21 \\
chilled & 21 & 21 \\
\hline
\end{tabular}
\end{center}
\end{table}
If you are
using
<< echo = FALSE , results = tex >>
library(xtable)
data(CO2)
with(CO2, xtable(table(Treatment , Type)))
@
and a the result of the R run is a LaTeX document. Another post
will give a hint how to paste R graphics into the same Sweave or
LaTeX document… later…
The full usage
of
xtable(x, caption=NULL, label=NULL, align=NULL, digits=NULL,
display=NULL)
.. add table captions
xtable(table , caption = "My table caption")
and labels
xtable(table , label = " MyLaTeXlable")
to the LaTeX tables.
>
>
[1,]
[2,]
[3,]
[4,]
[5,]
>
%
%
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
1
\end{tabular}
\end{center}
\end{table}
Guest post by Marek Hlavac
Since its
install.packages(“stargazer”) |
New Features: Text Output and Confidence Intervals
In this blog post, I would like to draw attention to two new
features of
-
stargazer
can now produce ASCII text output, in addition to LaTeX code. As a result, users can now create beautiful tables that can easily be inserted into Microsoft Word documents, published on websites, or sent via e-mail. Sharing your regression results has never been easier. Users can also use this feature to preview their LaTeX tables before they use the stargazer-generated code in their .tex documents. -
In addition to standard errors,
stargazer can now report confidence intervals at user-specified confidence levels (with a default of 95 percent). This possibility might be especially appealing to researchers in public health and biostatistics, as the reporting of confidence intervals is very common in these disciplines.
In the reproducible example presented below, I demonstrate these two new features in action.
Reproducible Example
I begin by creating model objects for two Ordinary Least Squares
(OLS) models (using the
## 2 OLS models |
I then use
stargazer(linear.1, linear.2, probit.model, title="Regression Results", align=TRUE, dep.var.labels=c("Overall Rating","High Rating"), covariate.labels=c("Handling of Complaints","No Special Privileges", "Opportunity to Learn","Performance-Based Raises","Too Critical","Advancement"), omit.stat=c("LL","ser","f"), no.space=TRUE) |
http://www.r-statistics.com/wp-content/uploads/2013/07/table_example_1.bmp
In the next table, I limit myself to the two linear models, and
report 90 percent confidence intervals (using
the
stargazer(linear.1, linear.2, title="Regression Results", dep.var.labels=c("Overall Rating","High Rating"), covariate.labels=c("Handling of Complaints","No Special Privileges", "Opportunity to Learn","Performance-Based Raises","Too Critical","Advancement"), omit.stat=c("LL","ser","f"), ci=TRUE, ci.level=0.90, single.row=TRUE) |
http://www.r-statistics.com/wp-content/uploads/2013/07/table_example_2.bmp
To produce ASCII text output, rather than LaTeX code, I can simply set the argument type to “text”:
stargazer(linear.1, linear.2, type="text", title="Regression Results", dep.var.labels=c("Overall Rating","High Rating"), covariate.labels=c("Handling of Complaints","No Special Privileges", "Opportunity to Learn","Performance-Based Raises","Too Critical","Advancement"), omit.stat=c("LL","ser","f"), ci=TRUE, ci.level=0.90, single.row=TRUE) |
http://www.r-statistics.com/wp-content/uploads/2013/07/table_example_3.bmp
What Else is New?
The two new features that I have focused on in this blog post, of
course, do not exhaust the range of innovations that the
newstargazer
Additionally, users have a greater scope for making changes to the
table’s formatting. A much-demanded addition to version 4.0
concerns column labels. Using
arguments
More advanced users can now choose whether the LaTeX table should
be enclosed within a floating environment
(arguments
Marek Hlavac is a doctoral student in the Political Economy and
Government program at Harvard Unviersity. If you have any
suggestions for future versions of
the
stargazer
Ease of use
stargazer
A quick reproducible example shows just how
easy
install.packages("stargazer") library(stargazer) |
To create a summary statistics table from
the
stargazer(attitude) |
http://i2.wp.com/www.r-statistics.com/wp-content/uploads/2013/01/stargazer_summ_stat.jpg?resize=474,233
To output the contents of the first four rows of same data frame,
specify the part of the data frame you would like to see, and set
thesummary
stargazer(attitude[1:4,], summary=FALSE) |
http://i0.wp.com/www.r-statistics.com/wp-content/uploads/2013/01/stargazer_data_frame.jpg?resize=300,78
Now, let us try to create a simple regression table with three
side-by-side models – two Ordinary Least Squares (OLS) and one
probit regression model – using the
## 2 OLS models linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude) linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude) |
http://i1.wp.com/www.r-statistics.com/wp-content/uploads/2013/01/stargazer_regression.jpg?resize=474,548
Many supported models
stargazer
Beautiful aesthetics
stargazer
http://i0.wp.com/www.r-statistics.com/wp-content/uploads/2013/01/stargazer_complex_table.jpg?resize=237,300
If you’d like to create tables that look like those from your
discipline’s leading journal,
stargazer, of course, is not
the only R package that creates LaTeX code from R statistical
output. Other packages with similar capabilities
include
Some extra Q&A with Marek Hlavac (the package author)
What was your motivation for starting the package? (self use, for students, for other people etc.)
As a doctoral student in Political Economy and Government at Harvard University, I saw an urgent need for an easy-to-use tool to create well-formatted stargazer tables. Although other packages were available for this task, none of them combined a large number of supported models, good aesthetics, and simplicity of use in a way that I thought would be ideal.
Why is the “summary” parameter turned on as default for data.frames?
In published papers in the social sciences, summary statistics table are found more commonly than direct print-outs of chunks of data sets. For this reason, I thought the stargazer package should default to the user’s most likely need.
This has been a guest post by Marek Hlavac, the author of
the{stargazer}
Finishing note: You (the readers) are invited to leave a comment to
the author, or suggest your own guest post here,
by