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, 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
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, MixedModels.dataset(:kb07); contrasts=contr)
Est. | SE | z | p | σ_subj | σ_item | |
---|---|---|---|---|---|---|
(Intercept) | 2182.0689 | 78.1899 | 27.91 | <1e-99 | 318.9465 | 358.3692 |
spkr: old | 67.9659 | 19.0799 | 3.56 | 0.0004 | 67.0683 | |
prec: maintain | -333.7038 | 18.5949 | -17.95 | <1e-71 | 58.9477 | |
load: yes | 78.3742 | 19.1656 | 4.09 | <1e-04 | 63.2025 | 19.8156 |
spkr: old & prec: maintain | -21.5695 | 16.8440 | -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.8440 | 1.38 | 0.1682 | ||
Residual | 712.4085 |
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) 101726.8398 318.9465
spkr: old 4498.1586 67.0683 +0.70
prec: maintain 3474.8275 58.9477 -0.71 -0.00
load: yes 3994.5501 63.2025 +0.28 +0.88 +0.47
item (Intercept) 128428.4804 358.3692
load: yes 392.6594 19.8156 +0.79
Residual 507525.9267 712.4085
Number of obs: 1789; levels of grouping factors: 56, 32
Fixed-effects parameters:
────────────────────────────────────────────────────────────────────────────────
Coef. Std. Error z Pr(>|z|)
────────────────────────────────────────────────────────────────────────────────
(Intercept) 2182.07 78.1899 27.91 <1e-99
spkr: old 67.9659 19.0799 3.56 0.0004
prec: maintain -333.704 18.5949 -17.95 <1e-71
load: yes 78.3742 19.1656 4.09 <1e-04
spkr: old & prec: maintain -21.5695 16.844 -1.28 0.2004
spkr: old & load: yes 18.1669 16.844 1.08 0.2808
prec: maintain & load: yes 4.31649 16.844 0.26 0.7977
spkr: old & prec: maintain & load: yes 23.2112 16.844 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 | Variance | Std.Dev | Corr. | |||
---|---|---|---|---|---|---|
subj | (Intercept) | 101726.8398 | 318.9465 | |||
spkr: old | 4498.1586 | 67.0683 | +0.70 | |||
prec: maintain | 3474.8275 | 58.9477 | -0.71 | -0.00 | ||
load: yes | 3994.5501 | 63.2025 | +0.28 | +0.88 | +0.47 | |
item | (Intercept) | 128428.4804 | 358.3692 | |||
load: yes | 392.6594 | 19.8156 | +0.79 | |||
Residual | 507525.9267 | 712.4085 |
Similarly for BlockDescription
, OptSummary
and MixedModels.likelihoodratiotest
:
BlockDescription(kbm)
rows | subj | item | fixed |
---|---|---|---|
224 | BlkDiag | ||
64 | Dense | BlkDiag/Dense | |
9 | Dense | Dense | Dense |
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 value | 29340.042234597673 |
Optimizer settings | |
Optimizer (from NLopt) | LN_BOBYQA |
Lower bounds | [0.0, -Inf, -Inf, -Inf, 0.0, -Inf, -Inf, 0.0, -Inf, 0.0, 0.0, -Inf, 0.0] |
ftol_rel | 1.0e-12 |
ftol_abs | 1.0e-8 |
xtol_rel | 0.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 | [0.75, 1.0, 1.0, 1.0, 0.75, 1.0, 1.0, 0.75, 1.0, 0.75, 0.75, 1.0, 0.75] |
maxfeval | -1 |
maxtime | -1.0 |
Result | |
Function evaluations | 372 |
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 value | 28801.3758 |
Return code | FTOL_REACHED |
m0 = fit(MixedModel, @formula(reaction ~ 1 + (1|subj)), MixedModels.dataset(:sleepstudy))
m1 = fit(MixedModel, @formula(reaction ~ 1 + days + (1+days|subj)), MixedModels.dataset(:sleepstudy))
MixedModels.likelihoodratiotest(m0,m1)
model-dof | deviance | χ² | χ²-dof | P(>χ²) | |
---|---|---|---|---|---|
reaction ~ 1 + (1 | subj) | 3 | 1911 | |||
reaction ~ 1 + days + (1 + days | subj) | 6 | 1752 | 159 | 3 | <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.1899 | 27.91 | <1e-99 | 318.9465 | 358.3692 |
| spkr: old | 67.9659 | 19.0799 | 3.56 | 0.0004 | 67.0683 | |
| prec: maintain | -333.7038 | 18.5949 | -17.95 | <1e-71 | 58.9477 | |
| load: yes | 78.3742 | 19.1656 | 4.09 | <1e-04 | 63.2025 | 19.8156 |
| spkr: old & prec: maintain | -21.5695 | 16.8440 | -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.8440 | 1.38 | 0.1682 | | |
| Residual | 712.4085 | | | | | |
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">(Intercept)</td><td align="right">2182.0689</td><td align="right">78.1899</td><td align="right">27.91</td><td align="right"><1e-99</td><td align="right">318.9465</td><td align="right">358.3692</td></tr><tr><td align="left">spkr: old</td><td align="right">67.9659</td><td align="right">19.0799</td><td align="right">3.56</td><td align="right">0.0004</td><td align="right">67.0683</td><td align="right"> </td></tr><tr><td align="left">prec: maintain</td><td align="right">-333.7038</td><td align="right">18.5949</td><td align="right">-17.95</td><td align="right"><1e-71</td><td align="right">58.9477</td><td align="right"> </td></tr><tr><td align="left">load: yes</td><td align="right">78.3742</td><td align="right">19.1656</td><td align="right">4.09</td><td align="right"><1e-04</td><td align="right">63.2025</td><td align="right">19.8156</td></tr><tr><td align="left">spkr: old & prec: maintain</td><td align="right">-21.5695</td><td align="right">16.8440</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 & 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 & 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 & prec: maintain & load: yes</td><td align="right">23.2112</td><td align="right">16.8440</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.4085</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.1899 & 27.91 & <1e-99 & 318.9465 & 358.3692 \\
spkr: old & 67.9659 & 19.0799 & 3.56 & 0.0004 & 67.0683 & \\
prec: maintain & -333.7038 & 18.5949 & -17.95 & <1e-71 & 58.9477 & \\
load: yes & 78.3742 & 19.1656 & 4.09 & <1e-04 & 63.2025 & 19.8156 \\
spkr: old \& prec: maintain & -21.5695 & 16.8440 & -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.8440 & 1.38 & 0.1682 & & \\
Residual & 712.4085 & & & & & \\
\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.1899 & 27.91 & <1e-99 & 318.9465 & 358.3692 \\
spkr: old & 67.9659 & 19.0799 & 3.56 & 0.0004 & 67.0683 & \\
prec: maintain & -333.7038 & 18.5949 & -17.95 & <1e-71 & 58.9477 & \\
load: yes & 78.3742 & 19.1656 & 4.09 & <1e-04 & 63.2025 & 19.8156 \\
spkr: old \& prec: maintain & -21.5695 & 16.8440 & -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.8440 & 1.38 & 0.1682 & & \\
Residual & 712.4085 & & & & & \\
\end{tabular}
This output can also be written directly to file:
open("model.md", "w") do io
show(io, MIME("text/markdown"), kbm)
end