PLM object to LaTeX table - r

How can I make LaTeX tables with plm objects?
I have been using apsrtable to make LaTeX tables for output summaries for lm objects, but can't seem to find an easy way to do the same with plm. I am calculating panel corrected standard errors using plm and VcovBK(), but then have to go into latex and change the standard errors by hand.

As of version 2.0, the stargazer package supports PLM objects. It also makes some very nice tables, so it is worth a look.

A general approach which shouod work is to put your results in a data.frame and use xtable to create a LaTex table. This might take a bit more work than using premade templates, but it is very flexible.

Related

saving tables (e.g. latex) from Bayesian estimation functions in R

I would like to save the results of a Bayesian estimation function (e.g. brm) to file (e.g. in Latex format), for sharing results and preparing publications. It appears that existing packages (e.g. xtable, stargazer) are designed with non-Bayesian statistics in mind and cannot handle these model objects. Are there any existing packages or available code to handle this task (before I begin to recreate the wheel)? I have found tools for making tables from models estimated using JAGS/BUGS here but brm uses stan to estimate models.
If you call launch_shinystan on the object and go to the Estimate tab, there is a link to Generate LaTeX table that gives you a bunch of options to check on the left and it outputs the syntax on the right.
For posterity's sake, for basic tables one may also access specific parts of the model by for example:
summary(model)$fixed
where model is a stanfit object, and pass that to xtable or another function to output latex tables.

including a label for omitted coefficients in texreg

I want to replicate in texreg the functionality contained in stargazer via the arguments omit and omit.labels (see here). Unfortunately, I cannot use the stargazer package as it does not support the model I am using and is not extensible. Since texreg is easily extensible I can get it to work with my models. I can also easily omit some output from texreg with the omit.coef argument. What I can't seem to figure out is how to insert labels for the omitted coefficients. Does this exist in texreg? Does anyone have experience trying to write this functionality into an extract function? Alternatively, has anyone figured out how to extend stargazer to work with a custom model?
Context: I am writing a presentation in knitr and need to convert the output of some estimators into latex which will then get converted to pdf for my beamer presentation. The output has a bunch of covariates and thus is too long to display nicely in beamer. I want to truncate the output by omitting some covariates and inserting in their place a line indicating whether these covariates have been included in the model or not, e.g. collapse the variables "County Population", "County Income", etc. into a line that reads "County controls" and then have "Yes" or "No" to indicate whether these controls were included in the estimate or not. Ideally, someone could help me figure out a way to do this in texreg. If not, I would be open to other packages/approaches, e.g. xtable.
A possible option is the github version of huxtable. (I am the author.) This has a huxreg function which creates a table from a bunch of regressions, much like texreg: it'll work for anything that has a broom::tidy method defined for it. You can then edit the table much like a normal data frame, just rbind in the rows you want.
You'll need to install using devtools::install_github, if you want to try this route.

How to make publishable tables and plots using R? [duplicate]

There are a range of tools available for creating publication quality tables using R, Sweave, and LaTeX.
In particular, there are helper functions like latex in the Hmisc package, and xtable in the xtable package. I've also often written my own code so that I could have complete control over table formatting (e.g., see this example).
However, when preparing publication quality tables a range of issues often arise:
how and when to apply numeric formatting
how to precisely control alignment of columns and cells
how to precisely control cell borders
how to convert variable labels to variable names
and so on
Beyond the high level issues of specifying the desired table format, there are issues of implementation.
When should a helper function such as xtable be used?
Which helper function should be used in a given situation?
How can the default output of helper functions be customised to particular requirements?
Question
It seems to me that the above issues are deserving of a detailed textbook-style introduction.
Are there any online or offline resources that provide a detailed overview of how to produce publication quality tables using R, Sweave, and LaTeX, and that address the issues discussed above?
Just to tie this up with a nice little bow at the time of current writing, the best existant tutorials on publication-quality tables and usage scenarios appear to be an amalgamation of these documents:
A Sweave example (source)
The Joy of Sweave: A Beginner's Guide to Reproducible Research with Sweave (source)
Latex and R via Sweave: An example document how to use Sweave (source)
Sweave = R · LaTeX2 (source)
The xtable gallery (source)
The Sweave Homepage
LaTeX documentation
Going beyond the scope of what currently exists, you may want to ask the author of The Joy of Sweave for a document on publication-quality tables specifically. It seems like he's gone above and beyond this problem in his research. In addition to the questions you've raised, this space specifically could use a style guide that, flatly, does not currently exist.
And, as mentioned in the question errata, this is a perfect example of a question for https://tex.stackexchange.com/. I encourage you to continue to ask specific questions there when you run into any difficulties in your current projects.
The package stargazer can create publication-quality - incl. using templates designed to resemble existing academic journals - from commonly used R statistical functions and packages (lm, glm, plm, svyglm, survival, pscl, AER, and others). Also good for creating summary statistics tables, and can directly output data frame content as well.
There is a tabular function in the tables package which addresses formatting, alignment and label operations. The package has a vignette which is a good starting point.
xtable has worked fine for me so far.
In combination with siunitx, and when necessary, longtable, it can produce pretty effective tables, in my opinion. With packages like booktabs and caption, the aesthetics can be pleasing too.
I am not sure this level of detail was asked for by the OP, but for what it's worth, the basic implementation could be something along these lines: https://tex.stackexchange.com/questions/41067/caption-for-longtable-in-sweave/41183#41183 (my own answer to another question).
I highly recommend ConTeXt which makes use of the TABLE package. There is a Table overview in contextgarden and an exhaustive manual.

General guide for creating publication quality tables using R, Sweave, and LaTeX

There are a range of tools available for creating publication quality tables using R, Sweave, and LaTeX.
In particular, there are helper functions like latex in the Hmisc package, and xtable in the xtable package. I've also often written my own code so that I could have complete control over table formatting (e.g., see this example).
However, when preparing publication quality tables a range of issues often arise:
how and when to apply numeric formatting
how to precisely control alignment of columns and cells
how to precisely control cell borders
how to convert variable labels to variable names
and so on
Beyond the high level issues of specifying the desired table format, there are issues of implementation.
When should a helper function such as xtable be used?
Which helper function should be used in a given situation?
How can the default output of helper functions be customised to particular requirements?
Question
It seems to me that the above issues are deserving of a detailed textbook-style introduction.
Are there any online or offline resources that provide a detailed overview of how to produce publication quality tables using R, Sweave, and LaTeX, and that address the issues discussed above?
Just to tie this up with a nice little bow at the time of current writing, the best existant tutorials on publication-quality tables and usage scenarios appear to be an amalgamation of these documents:
A Sweave example (source)
The Joy of Sweave: A Beginner's Guide to Reproducible Research with Sweave (source)
Latex and R via Sweave: An example document how to use Sweave (source)
Sweave = R · LaTeX2 (source)
The xtable gallery (source)
The Sweave Homepage
LaTeX documentation
Going beyond the scope of what currently exists, you may want to ask the author of The Joy of Sweave for a document on publication-quality tables specifically. It seems like he's gone above and beyond this problem in his research. In addition to the questions you've raised, this space specifically could use a style guide that, flatly, does not currently exist.
And, as mentioned in the question errata, this is a perfect example of a question for https://tex.stackexchange.com/. I encourage you to continue to ask specific questions there when you run into any difficulties in your current projects.
The package stargazer can create publication-quality - incl. using templates designed to resemble existing academic journals - from commonly used R statistical functions and packages (lm, glm, plm, svyglm, survival, pscl, AER, and others). Also good for creating summary statistics tables, and can directly output data frame content as well.
There is a tabular function in the tables package which addresses formatting, alignment and label operations. The package has a vignette which is a good starting point.
xtable has worked fine for me so far.
In combination with siunitx, and when necessary, longtable, it can produce pretty effective tables, in my opinion. With packages like booktabs and caption, the aesthetics can be pleasing too.
I am not sure this level of detail was asked for by the OP, but for what it's worth, the basic implementation could be something along these lines: https://tex.stackexchange.com/questions/41067/caption-for-longtable-in-sweave/41183#41183 (my own answer to another question).
I highly recommend ConTeXt which makes use of the TABLE package. There is a Table overview in contextgarden and an exhaustive manual.

Suggestion for R/LaTeX table creation package

I've been using xtable package for a long time, and looking forward to writting my first package in R... so I reckon that if I have some "cool" idea that's worth carying out, there's a great chance that somebody got there before me... =)
I'm interested in functions/packages specialized for LaTeX table creation (through R, of course). I bumped on quantreg package which has latex.table function. Any suggestion for similar function(s)/package(s)?
P.S.
I'm thinking about building a webapp in which users can define their own presets/templates of tables, choose style, statistics, etc. It's an early thought, though... =)
I sometimes divide the task of creating LaTeX tables into two parts:
I'll write the table environment, caption, and tabular environment commands directly in my LaTeX document.
I'll export just the body of the table from R using a custom function.
The R export part involves several steps:
Starting with a matrix of the whole table including any headings:
Add any LaTeX specific formatting to the table. E.g., enclose digits in dollar symbols to ensure that negative numbers display correctly.
Collapse rows into a single character value by replacing separate columns with the ampersand (&) and adding ends-of-row symbols "\\"
Add any horizontal lines to be displayed in the table. I use the booktabs LaTeX package.
Export the resulting character vector using the write function
The exported text file is then imported using the input command in LaTeX. I ensure that the file name corresponds to the table label.
I have used this approach in the context of writing journal articles.
In these cases, there are a lot of different types of tables (e.g., multi-page tables, landscape tables, tables requiring extended margins, tables requiring particular alignment, tables where I want to change the wording of the table title). In this setting, I've mostly found it easier to just export the data from R. In this way, the result is reproducible research, but it is easier to tweak aspects of table design in the LaTeX document. And in the context of journal articles, there are usually not too many tables and rather specific formatting requirements.
However, I imagine if I were producing large numbers of batch reports, I'd consider exporting more aspects directly from R.
Beyond xtable and Hmisc as listed by Rob, there are also at least
apsrtable which formats latex tables from one or more model objects
p2lh which exports R to LaTeX and HTML
RcmdrPlugin.Export which graphically exports output to LaTeX or HTML
reporttools which generates LaTeX tables of descriptive statistics
This was just based on a quick search. So there may be more for you to look at before you try to hook it into a webapp. Good luck.
In addition to the packages mentioned above, there is the stargazer package. It works well with objects from many commonly used functions and packages (lm, glm, svyglm, plm, survival, AER, pscl, and others), as well as with zelig objects.
Apart from xtable, there's the latex function in the Hmisc package.

Resources