In conjunction with LaTeX, the output of the mtable() function of
the package can produce output as can be seen in the following example
(which is LaTeX converted into a png-file):

This LaTeX output was generated by the following code:

# First a couple of models are run:model1<-glm((vote3=="Truman")~occup4,data=vote.48,family="binomial")model2<-glm((vote3=="Truman")~total.income,data=vote.48,family="binomial")model3<-glm((vote3=="Truman")~occup4+total.income,data=vote.48,family="binomial")model4<-glm((vote3=="Truman")~relig3,data=vote.48,family="binomial")model5<-glm((vote3=="Truman")~occup4+relig3,data=vote.48,family="binomial")

The chief method to determine how coefficient estmates are shown and
what and how additional information is provided is by using the
coef.style= argument of mtable with which a pre-defined or
user-provided template is selected. To demonstrate this, we start with
the ‘standard’ way in which coefficient estimates are presented:

It is also possible to change the symbols for the significance levels:

# Why would one want to have letters instead? I have no idea, but# some Germen authors/editors seem to like it that way ...mtable("Model 1"=lm0,"Model 2"=lm1,"Model 3"=lm2,signif.symbols=c("<sup>a</sup>"=.05,"<sup>b</sup>"=.01,"<sup>c</sup>"=.001),summary.stats=c("R-squared","N"))

In general there is a certain variety of summary statistics available in
mtable(). What statistics these are depends on the statistical model
in question and the facilities provided by the corresponding
getSummary() method (see below). If mtable() is called without
the summary.stats= argument all available summary statistics are
shown:

mtable("Model 1"=lm0,"Model 2"=lm1,"Model 3"=lm2)

Model 1

Model 2

Model 3

(Intercept)

30

.

628***

6

.

360***

28

.

566***

(7

.

409)

(1

.

252)

(7

.

355)

pop15

−0

.

471**

−0

.

461**

(0

.

147)

(0

.

145)

pop75

−1

.

934

−1

.

691

(1

.

041)

(1

.

084)

dpi

0

.

001

−0

.

000

(0

.

001)

(0

.

001)

ddpi

0

.

529*

0

.

410*

(0

.

210)

(0

.

196)

R-squared

0

.

3

0

.

2

0

.

3

adj. R-squared

0

.

2

0

.

1

0

.

3

sigma

3

.

9

4

.

2

3

.

8

F

8

.

3

4

.

5

5

.

8

p

0

.

0

0

.

0

0

.

0

Log-likelihood

−137

.

8

−141

.

0

−135

.

1

Deviance

726

.

2

824

.

7

650

.

7

AIC

283

.

7

290

.

0

282

.

2

BIC

291

.

3

297

.

7

293

.

7

N

50

50

50

So if we prefer (or our reviewer, editor, supervisor or boss) we can show some unusual
statistics

Of course you would not only like to see your table of estimates on
screen but also to include it into a documement that reports your
findings. memisc() supports exporting tables of model estimates
(i.e. results of mtable()) into LaTeX documents and into formats
that can be read in by word-processing software: tab-delimited format
and HTML. There is no direct way to export model tables into a
word-processing format yet, mainly because there is no (simple) file
format standard (OpenDocument may be an emerging standard, but it is
not easy to create files in this format - at least not as easy as HTML).
These various options of exporting the results of mtable() are
discussed in the following. To this purpose, we return to the example
from above and ‘embellish’ a bit by changing the coefficient labels:

lm0<-lm(sr~pop15+pop75,data=LifeCycleSavings)lm1<-lm(sr~dpi+ddpi,data=LifeCycleSavings)lm2<-lm(sr~pop15+pop75+dpi+ddpi,data=LifeCycleSavings)mt123<-mtable("Model 1"=lm0,"Model 2"=lm1,"Model 3"=lm2,summary.stats=c("R-squared","N"))mt123<-relabel(mt123,"(Intercept)"="Constant",pop15="Percentage of population under 15",pop75="Percentage of population over 75",dpi="Real per-capita disposable income",ddpi="Growth rate of real per-capita disp. income")mt123

The first format to export mtable() results into is TeX/LaTeX
simply because this is the format in which the author of this package
usually writes his papers. This is achieved by, first, applying the
function toLatex() to the results of mtable(), which tranlates
them into a character string containing TeX/LaTeX code and, second,
using writeLines() to send this character string into a text file.
This was already shown at the beginning, but we take a closer look at it
here. Continuing the example immediately above, we call toLatex() to
see the TeX/LaTeX code:

It should be noted that toLatex() is a generic function and the
“memisc” package ‘only’ defines a method for “mtable” objects.
Alternatively one could use the call

format(mt123,target="LaTeX")

or even call the internal formatting function itself:

mtable_format_latex(m123)

By default, the TeX/LaTeX code created this way uses the macros
\toprule, \midrule, and \bottomrule provided by the LaTeX
package “booktabs”. If you do not like this package (why shouldn’t
anyone?) you can resolve this dependency by calling toLatex(),
format(), or mtable_format_latex() with the optional argument
useBooktabs=FALSE. Another default dependency is the LaTeX package
“dcolumn”, which is used to make sure that floating point numbers are
aligned on their decimal dots. This dependency can be resolved by the
optional argument useDcolumn=FALSE. For more aspects of LaTeX output
that can be customized see the help page ?mtable_format_latex.

Until version 0.97 of “memisc”, creating a text file with TeX/LaTeX code
had to be done in two steps, first creating a text string with the code
and then writing the text string into the file. Since version 0.98 both
steps can be done with a single function call, using write.mtable().
That is, to write the LaTeX formatted mtable() result in mt123
one can simple call:

Tab-delimited format is of course best-suited for exporting data frames
or matrices into files, but since the results of mtable() have a
tabular structure, they can also be exported into this format.
Tab-delimited format is quite simple and can be read by a wide variety
of software. However, this simplicity also means that only the cell
contents are exported while embellishments of the contents (e.g.
horizonal alignment of cells and cell borders) are not.

The following code exports the mtable() result named mt123 into
a text file in tab-delimited format:

write.mtable(mt123,file="mt123.txt")

After opening this file with LibreWriter, using its “covert text into
table” tool, and some manual tweaking the result looks like this:

(The original text file is available
here while the LibreOffice file is available
here.)
Obviously some further tweaking
(such as comma-oriented column tabulators) is needed to make this work
in a publication.

It is also posslibe export an mtable() result into “CSV” format and
import it into some spreadsheet software. This would be done so:

write.mtable(mt123,file="mt123.csv",colsep=",")

After opening this file with LibreCalc and some tweaking of the format,
the result looks like this:

Of course, having to tweak the format of mtable() results by hand is
frustrating, so in order to make easier to get well-formatted tables,
version 0.98 of “memisc” provides for exporting the results of
mtable() into HTML. HTML is the format of websites, but it can also
imported into contemporary word processing software with little loss in
formatting. Further, if one is using the “Rmarkdown” and “knitr”
packages to produced HTML-formatted reports, it is convenient to have
HTML versions of mtable() results.

To get a file in HTML format that contains the results of mtable()
one can again use the function write.mtable(), yet in this case with
the option format="HTML", or directly the function write_html():

write_html(mt123,file="mt123.html")

The file “mt123.html” generated that way can be included into your
favourite word-processing software, e.g. LibreOffice. This is how the
table would look like after including it into LibreOffece (and setting
the columns to “optimal width”):

and this
how it would look like after including into Word:

To view results of mtable() in HTML in interactive sessions with
RStudio, one can simply call show_html() as in

show_html(mt123)

Of course, this document is not an interactive sesssion but produced
using the “knitr” package. In this context, show_html() used inside
an R-chunk with chunk option results='asis' and the options setting
options(html_viewer="stdout"). With the following code one could
even make sure that all results of mtable() in a knitr document
are printed in HTML format:

knit_print.mtable<-function(x,...)knitr::asis_output(format_html(x))

This “trick” was used previously in this document where
the different display options of coefficient estmates were discussed.
Thus after this trick, in a knitr document we get

mt123

Model 1

Model 2

Model 3

Constant

30

.

628***

6

.

360***

28

.

566***

(7

.

409)

(1

.

252)

(7

.

355)

Percentage of population under 15

−0

.

471**

−0

.

461**

(0

.

147)

(0

.

145)

Percentage of population over 75

−1

.

934

−1

.

691

(1

.

041)

(1

.

084)

Real per-capita disposable income

0

.

001

−0

.

000

(0

.

001)

(0

.

001)

Growth rate of real per-capita disp. income

0

.

529*

0

.

410*

(0

.

210)

(0

.

196)

R-squared

0

.

3

0

.

2

0

.

3

N

50

50

50

Adapting mtable to new model classes and other tricks - the API of mtable¶

mtable() is designed to be easily adapted to all kind of model
classes: If there is a model class like, say, “modcls” then all that is
needed to get mtable() to report estimates of instances of this
model class is to define a function getSummary.modcls(), i.e. a
method function of objects of class “modcls”. This function needs to
return a list with components

“coef”: A matrix or array with coefficients and additional
information. The rows should refer to coefficients, the columns
should contain the estimates, standard errors, p-values, lower and
upper confidence interval limits. The columns should be labelled
“est”, “se”, “stat”, “p”, “lwr”, and “upr”.

For single-equation models, this component should be a matrix. For
multiple-equation models, it should be a three-dimensional array,
with the third dimension corresponding to the equations.

The modularity of mtable() through the use of the generic
getSummary() function allows other kinds of extensions, e.g.
adapting it to the use of “sandwich” estimators of standard errors. This
can be achieved, first, by defining yet another method function of
getSummary(), e.g. the one defined in the R available
here. As a second step, one
marks model estimation results such that this newly defined method
function is applied to them by attaching the appropriate class
attribute. For example to get sandwich estimators of standard errors for
“lm”” or “glm”” objects one can attach the classes “lm_sandwich” or
“glm_sandwich”, respectively, as in the following example: