加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2014-12-30 01:49:07)
标签:

r

latex

table

分类: 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 data.frame as a nice LaTeX-table, the only thing to do is:

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 Sweave the usage becomes
<< 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() is
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.




> library('xtable')
> m=matrix(rnorm(25,5,1),5,5)
> m
         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 5.223797 4.921448 4.775009 5.253216 5.002215
[2,] 5.111304 6.761457 5.561525 5.693226 3.857417
[3,] 3.868195 3.759403 5.971332 4.240052 4.328775
[4,] 5.009473 4.624340 7.367284 3.844524 4.888032
[5,] 4.923996 5.239990 5.336282 5.264121 3.130824
> xtable(m)
% latex table generated in R 2.11.1 by xtable 1.5-6 package
% Tue Oct 12 11:35:50 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
  \hline
 & 1 & 2 & 3 & 4 & 5 \\ 
  \hline
1 & 5.22 & 4.92 & 4.78 & 5.25 & 5.00 \\ 
  2 & 5.11 & 6.76 & 5.56 & 5.69 & 3.86 \\ 
  3 & 3.87 & 3.76 & 5.97 & 4.24 & 4.33 \\ 
  4 & 5.01 & 4.62 & 7.37 & 3.84 & 4.89 \\ 
  5 & 4.92 & 5.24 & 5.34 & 5.26 & 3.13 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}



Guest post by Marek Hlavac

Since its first introduction on this blog, stargazer, a package for turning R statistical output into beautiful LaTeX and ASCII text tables, has made a great deal of progress. Compared to available alternatives (such as apsrtable or texreg), the latest version (4.0) of stargazer supports the broadest range of model objects. In particular, it can create side-by-side regression tables from statistical model objects created by packages AERbetaregdynlmehaergmgee,gmmlme4MASSmgcvnlmennetordinalplmpsclquantreg,releventrmsrobustbasespdepstatssurveysurvival and Zelig.  You can install stargazer from CRAN in the usual way:


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 that make the package even more useful:

  • 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 lm() command) and a probit model (usingglm() ). Note that I use data from attitude, one of the standard data frames that should be provided with your installation of R.


## 2 OLS models
 
linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
 
## create an indicator dependent variable, and run a probit model
 
attitude$high.rating <- (attitude$rating > 70)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit"))

I then use stargazer to create a ‘traditional’ LaTeX table with standard errors. With the sole exception of the argument no.space – which I use to save space by removing all empty lines in the table – both the command call and the resulting table should look familiar from earlier versions of the package:


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 ci and ci.level arguments). In addition, I use the argument single.row to report the coefficients and confidence intervals on the same row.


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 brings. The package can now output beautiful LaTeX and ASCII text tables directly into .tex and.txt files, respectively, using theout argument.

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 column.labels andcolumn.separate, users can now add a label to each of the columns in their regression table. Such labels can be used to indicate, among other things, the sub-sample or research hypothesis that a particular column refers to. In addition, users can also change the caption above the names of the dependent variables (argument dep.var.caption), as well as tinker with the font size in the resulting table (argumentfont.size).

More advanced users can now choose whether the LaTeX table should be enclosed within a floating environment (arguments floatand float.env), and where the resulting table should be placed within the LaTeX document (argument table.placement). In this way, they might, for example, create a LaTeX table  that is rotated by 90 degrees (when float.env = “sidewaystable”).

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 package, please contact him athlavac@fas.harvard.edu .


stargazer is a new R package that creates LaTeX code for well-formatted regression tables, with multiple models side-by-side, as well as for summary statistics tables. It can also output the content of data frames directly into LaTeX. Compared to available alternatives,stargazer excels in three regards:  its ease of use, the large number of models it supports, and its beautiful aesthetics.

Ease of use

stargazer was designed with the user’s comfort in mind. The learning curve is very mild and all arguments are very intuitive, so that even a beginning user of R or LaTeX can quickly become familiar with the package’s many capabilities. The package is intelligent, and tries to minimize the amount of effort the user has to put into adjusting argument values. If stargazer is given a set of regression model objects, for instance, the package will create a side-by-side regression table. By contrast, if the user feeds it a data frame, stargazer will know that the user is most likely looking for a summary statistics table or – if the summary argument is set to false – wants to output the content of the data frame.

A quick reproducible example shows just how easy stargazer is to use. You can install stargazer from CRAN in the usual way:


install.packages("stargazer")
library(stargazer)

To create a summary statistics table from the ‘attitude’ data frame (which should be available with your default installation of R), simply run the following:


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 option to FALSE:


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 lm() and glm() functions. We can set thealign argument to TRUE, so that coefficients in each column are aligned along the decimal point:


## 2 OLS models
linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
 
## create an indicator dependent variable, and run a probit model
 
attitude$high.rating <- (attitude$rating > 70)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit"))
 
stargazer(linear.1, linear.2, probit.model, title="Regression Results", align=TRUE)

http://i1.wp.com/www.r-statistics.com/wp-content/uploads/2013/01/stargazer_regression.jpg?resize=474,548

Many supported models

stargazer supports objects from the most widely used statistical functions and packages. These include objects from betareg (betareg), coxph (survival), clm (ordinal), clogit (survival), ergm (ergm),gam (mgcv), gee (gee), glm (stats), glmer (lme4), gls (nlme), hurdle (pscl), ivreg (AER), lm (stats), lmer (lme4), lmrob (robustbase), multinom (nnet), nlmer (lme4), plm (plm), pmg (plm), polr (MASS), rlm (MASS), svyglm (survey), survreg (survival), tobit (AER), zeroinfl (pscl), as well as from the implementation of these in Zelig. In addition, stargazeralso supports several Zelig models for social network analysis:cloglog.netgamma.netprobit.net, and logit.net. The number of models and objects can stargazer can accommodate puts it ahead of most of the alternative R-to-LaTeX options. As the development of the package continues, this list will continue expanding to include linear mixed effects models, matching models, as well as new, user-made, or customized statistical models.

Beautiful aesthetics

stargazer is very pleasing to the eye, and allows the user to customize the formatting of the resulting table, including all variable labels. Below is an example of a good-looking, complex regression table created by 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 can help you with that as well. You can use the style argument to choose a template of your choice. Economics and management scholars can thus create tables that resemble those published in the American Economic Review, in the Quarterly Journal of Economics, or in Administrative Science Quarterly. Political scientists can avail themselves of templates based on the American Political Science Review, the American Journal of Political Science, and onInternational Organization. For sociologists and demographers, theAmerican Sociological Review, the American Sociological Reviews andDemography are available.

stargazer, of course, is not the only R package that creates LaTeX code from R statistical output. Other packages with similar capabilities include apsrtablextablememisctexreg and outreg. Each of these has its own strengths and weaknesses, and users should explore all of them to find the best fit for their needs.

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} R package for beautiful LaTeX tables from R’s statistical models’ outputs.

Finishing note: You (the readers) are invited to leave a comment to the author, or suggest your own guest post here, by contacting me.


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有