Alternative display and output formats

In the documentation, we have presented the output from MixedModels.jl in the same format you will see when working in the REPL. You may have noticed, however, that output from other packages received pretty printing. For example, DataFrames are converted into nice HTML tables. In MixedModels.jl, we recently (v3.2.0) introduced limited support for such pretty printing. (For more details on how the print and display system in Julia works, check out this NextJournal post.)

In particular, we have defined Markdown, HTML and LaTeX output, i.e. show methods, for our types. Note that the Markdown output can also be easily and more flexibly translated into HTML, LaTeX (e.g. with booktabs) or even a MS Word Document using tools such as pandoc. Packages like IJulia and Documenter can often detect the presence of these display options and use them automatically.

using MixedModels, MixedModelsDatasets
form = @formula(rt_trunc ~ 1 + spkr * prec * load +
                          (1 + load | item) +
                          (1 + spkr + prec + load | subj))
contr = Dict(:spkr => EffectsCoding(),
             :prec => EffectsCoding(),
             :load => EffectsCoding(),
             :item => Grouping(),
             :subj => Grouping())
kbm = fit(MixedModel, form, MixedModelsDatasets.dataset(:kb07); contrasts=contr)
Est.SEzpσ_subjσ_item
(Intercept)2182.068978.191227.91<1e-99318.9719358.3653
spkr: old67.965919.08023.560.000467.0723
prec: maintain-333.703818.5951-17.95<1e-7158.9519
load: yes78.374119.16504.09<1e-0463.202519.7985
spkr: old & prec: maintain-21.569516.8439-1.280.2004
spkr: old & load: yes18.166916.84401.080.2808
prec: maintain & load: yes4.316516.84400.260.7977
spkr: old & prec: maintain & load: yes23.211216.84391.380.1682
Residual712.4072

Note that the display here is more succinct than the standard REPL display:

using DisplayAs
kbm |> DisplayAs.Text
Linear mixed model fit by maximum likelihood
 rt_trunc ~ 1 + spkr + prec + load + spkr & prec + spkr & load + prec & load + spkr & prec & load + (1 + load | item) + (1 + spkr + prec + load | subj)
    logLik   -2 logLik      AIC         AICc        BIC     
 -14400.6879  28801.3758  28845.3758  28845.9489  28966.1429

Variance components:
             Column       Variance  Std.Dev.   Corr.
subj     (Intercept)     101743.0782 318.9719
         spkr: old         4498.6890  67.0723 +0.70
         prec: maintain    3475.3252  58.9519 -0.71 -0.00
         load: yes         3994.5582  63.2025 +0.28 +0.88 +0.47
item     (Intercept)     128425.6959 358.3653
         load: yes          391.9798  19.7985 +0.79
Residual                 507524.0610 712.4072
 Number of obs: 1789; levels of grouping factors: 56, 32

  Fixed-effects parameters:
───────────────────────────────────────────────────────────────────────────────
                                            Coef.  Std. Error       z  Pr(>|z|)
───────────────────────────────────────────────────────────────────────────────
(Intercept)                             2182.07       78.1912   27.91    <1e-99
spkr: old                                 67.9659     19.0802    3.56    0.0004
prec: maintain                          -333.704      18.5951  -17.95    <1e-71
load: yes                                 78.3741     19.165     4.09    <1e-04
spkr: old & prec: maintain               -21.5695     16.8439   -1.28    0.2004
spkr: old & load: yes                     18.1669     16.844     1.08    0.2808
prec: maintain & load: yes                 4.3165     16.844     0.26    0.7977
spkr: old & prec: maintain & load: yes    23.2112     16.8439    1.38    0.1682
───────────────────────────────────────────────────────────────────────────────

This brevity is intentional: we wanted these types to work well with traditional academic publishing constraints on tables. The summary for a model fit presented in the REPL does not mesh well with being treated as a single table (with columns shared between the random and fixed effects). In our experience, this leads to difficulties in typesetting the resulting tables. We nonetheless encourage users to report fit statistics such as the log likelihood or AIC as part of the caption of their table. If the correlation parameters in the random effects are of interest, then VarCorr can also be pretty printed:

VarCorr(kbm)
Column VarianceStd.DevCorr.
subj(Intercept)101743.0782318.9719
spkr: old4498.689067.0723+0.70
prec: maintain3475.325258.9519-0.71-0.00
load: yes3994.558263.2025+0.28+0.88+0.47
item(Intercept)128425.6959358.3653
load: yes391.979819.7985+0.79
Residual 507524.0610712.4072

Similarly for BlockDescription, OptSummary and MixedModels.likelihoodratiotest:

BlockDescription(kbm)
rowssubjitemfixed
224BlkDiag
64DenseBlkDiag/Dense
9DenseDenseDense
kbm.optsum
Initialization
Initial parameter vector[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
Initial objective value29340.042234597688
Optimizer settings
OptimizerLN_NEWUOA
Backendnlopt
ftol_rel1.0e-12
ftol_abs1.0e-8
xtol_rel0.0
xtol_abs[1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10, 1.0e-10]
initial_step[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
maxfeval-1
maxtime-1.0
xtol_zero_abs0.001
ftol_zero_abs1.0e-5
pirls_maxiter10
pirls_ftol_rel1.4901161193847656e-8
pirls_ftol_abs1.0e-5
pirls_maxhalfstep10
Result
Function evaluations351
Final parameter vector[0.4477, 0.066, -0.0592, 0.0249, 0.0672, 0.0579, 0.0852, 0.0, -0.0, 0.0, 0.503, 0.022, 0.017]
Final objective value28801.3758
Return codeFTOL_REACHED
m0 = fit(MixedModel, @formula(reaction ~ 1 + (1|subj)), MixedModelsDatasets.dataset(:sleepstudy))
m1 = fit(MixedModel, @formula(reaction ~ 1 + days + (1+days|subj)), MixedModelsDatasets.dataset(:sleepstudy))
MixedModels.likelihoodratiotest(m0,m1)
model-dof-2 logLikχ²χ²-dofP(>χ²)
reaction ~ 1 + (1 | subj)3-1911
reaction ~ 1 + days + (1 + days | subj)6-17521593<1e-33

To explicitly invoke this behavior, we must specify the right show method. (The raw and not rendered output is intentionally shown here.)

show(MIME("text/markdown"), m1)
|                                        |      Est. |      SE |      z |      p |   σ_subj |   σ_item |
|:-------------------------------------- | ---------:| -------:| ------:| ------:| --------:| --------:|
| (Intercept)                            | 2182.0689 | 78.1912 |  27.91 | <1e-99 | 318.9719 | 358.3653 |
| spkr: old                              |   67.9659 | 19.0802 |   3.56 | 0.0004 |  67.0723 |          |
| prec: maintain                         | -333.7038 | 18.5951 | -17.95 | <1e-71 |  58.9519 |          |
| load: yes                              |   78.3741 | 19.1650 |   4.09 | <1e-04 |  63.2025 |  19.7985 |
| spkr: old & prec: maintain             |  -21.5695 | 16.8439 |  -1.28 | 0.2004 |          |          |
| spkr: old & load: yes                  |   18.1669 | 16.8440 |   1.08 | 0.2808 |          |          |
| prec: maintain & load: yes             |    4.3165 | 16.8440 |   0.26 | 0.7977 |          |          |
| spkr: old & prec: maintain & load: yes |   23.2112 | 16.8439 |   1.38 | 0.1682 |          |          |
| Residual                               |  712.4072 |         |        |        |          |          |
show(MIME("text/html"), m1)
<table><tr><th align="left"></th><th align="right">Est.</th><th align="right">SE</th><th align="right">z</th><th align="right">p</th><th align="right">σ_subj</th><th align="right">σ_item</th></tr><tr><td align="left">&#40;Intercept&#41;</td><td align="right">2182.0689</td><td align="right">78.1912</td><td align="right">27.91</td><td align="right">&lt;1e-99</td><td align="right">318.9719</td><td align="right">358.3653</td></tr><tr><td align="left">spkr: old</td><td align="right">67.9659</td><td align="right">19.0802</td><td align="right">3.56</td><td align="right">0.0004</td><td align="right">67.0723</td><td align="right"> </td></tr><tr><td align="left">prec: maintain</td><td align="right">-333.7038</td><td align="right">18.5951</td><td align="right">-17.95</td><td align="right">&lt;1e-71</td><td align="right">58.9519</td><td align="right"> </td></tr><tr><td align="left">load: yes</td><td align="right">78.3741</td><td align="right">19.1650</td><td align="right">4.09</td><td align="right">&lt;1e-04</td><td align="right">63.2025</td><td align="right">19.7985</td></tr><tr><td align="left">spkr: old &amp; prec: maintain</td><td align="right">-21.5695</td><td align="right">16.8439</td><td align="right">-1.28</td><td align="right">0.2004</td><td align="right"> </td><td align="right"> </td></tr><tr><td align="left">spkr: old &amp; load: yes</td><td align="right">18.1669</td><td align="right">16.8440</td><td align="right">1.08</td><td align="right">0.2808</td><td align="right"> </td><td align="right"> </td></tr><tr><td align="left">prec: maintain &amp; load: yes</td><td align="right">4.3165</td><td align="right">16.8440</td><td align="right">0.26</td><td align="right">0.7977</td><td align="right"> </td><td align="right"> </td></tr><tr><td align="left">spkr: old &amp; prec: maintain &amp; load: yes</td><td align="right">23.2112</td><td align="right">16.8439</td><td align="right">1.38</td><td align="right">0.1682</td><td align="right"> </td><td align="right"> </td></tr><tr><td align="left">Residual</td><td align="right">712.4072</td><td align="right"></td><td align="right"></td><td align="right"></td><td align="right"></td><td align="right"></td></tr></table>

Note for that LaTeX, the column labels for the random effects are slightly changed: σ is placed into math mode and escaped and the grouping variable is turned into a subscript. Similarly for the likelihood ratio test, the χ² is escaped into math mode. This transformation improves pdfLaTeX and journal compatibility, but also means that XeLaTeX and LuaTeX may use a different font at this point.

show(MIME("text/latex"), m1)
\begin{tabular}
{l | r | r | r | r | r | r}
 & Est. & SE & z & p & $\sigma_\text{subj}$ & $\sigma_\text{item}$ \\
\hline
(Intercept) & 2182.0689 & 78.1912 & 27.91 & <1e-99 & 318.9719 & 358.3653 \\
spkr: old & 67.9659 & 19.0802 & 3.56 & 0.0004 & 67.0723 &   \\
prec: maintain & -333.7038 & 18.5951 & -17.95 & <1e-71 & 58.9519 &   \\
load: yes & 78.3741 & 19.1650 & 4.09 & <1e-04 & 63.2025 & 19.7985 \\
spkr: old \& prec: maintain & -21.5695 & 16.8439 & -1.28 & 0.2004 &   &   \\
spkr: old \& load: yes & 18.1669 & 16.8440 & 1.08 & 0.2808 &   &   \\
prec: maintain \& load: yes & 4.3165 & 16.8440 & 0.26 & 0.7977 &   &   \\
spkr: old \& prec: maintain \& load: yes & 23.2112 & 16.8439 & 1.38 & 0.1682 &   &   \\
Residual & 712.4072 &  &  &  &  &  \\
\end{tabular}

This escaping behavior can be disabled by specifying "text/xelatex" as the MIME type. (Note that other symbols may still be escaped, as the internal conversion uses the Markdown module from the standard library, which performs some escaping on its own.)

show(MIME("text/xelatex"), m1)
\begin{tabular}
{l | r | r | r | r | r | r}
 & Est. & SE & z & p & σ\_subj & σ\_item \\
\hline
(Intercept) & 2182.0689 & 78.1912 & 27.91 & <1e-99 & 318.9719 & 358.3653 \\
spkr: old & 67.9659 & 19.0802 & 3.56 & 0.0004 & 67.0723 &   \\
prec: maintain & -333.7038 & 18.5951 & -17.95 & <1e-71 & 58.9519 &   \\
load: yes & 78.3741 & 19.1650 & 4.09 & <1e-04 & 63.2025 & 19.7985 \\
spkr: old \& prec: maintain & -21.5695 & 16.8439 & -1.28 & 0.2004 &   &   \\
spkr: old \& load: yes & 18.1669 & 16.8440 & 1.08 & 0.2808 &   &   \\
prec: maintain \& load: yes & 4.3165 & 16.8440 & 0.26 & 0.7977 &   &   \\
spkr: old \& prec: maintain \& load: yes & 23.2112 & 16.8439 & 1.38 & 0.1682 &   &   \\
Residual & 712.4072 &  &  &  &  &  \\
\end{tabular}

This output can also be written directly to file:

open("model.md", "w") do io
    show(io, MIME("text/markdown"), kbm)
end