
Displaying nestedLogit Models as LaTeX Equations
Michael Friendly
2026-03-24
Source:vignettes/latex-equations.Rmd
latex-equations.RmdOverview
The equatiomatic package (Anderson, Heiss, & Sumners, 2023) provides
a general mechanism for converting fitted statistical models into LaTeX
equations, via the function extract_eq(). For any model
class that has a broom::tidy() method,
extract_eq() can generate both the symbolic form of the
model equation and a version with fitted coefficient values substituted
in.
The nestedLogit package (Fox
& Friendly, 2026) supports extract_eq() for
objects of class "nestedLogit" through a dedicated S3
method, extract_eq.nestedLogit. Because a nested logit
model is represented internally as a collection of binary logit
sub-models — one for each dichotomy — extract_eq()
generates a separate equation for each dichotomy and returns them as a
named list.
To use these features, load both packages:
Fitting a nested logit model
We use the Womenlf data (R-carData?), which records the
labor-force participation of married women and is included in the
carData package. The three-category response
partic (not working, part-time, full-time) is decomposed
into two nested binary dichotomies:
- work: not working vs. working (part-time or full-time)
- full: part-time vs. full-time, among those who work
Symbolic equations
Calling extract_eq() on a "nestedLogit"
object with submodel = "name" returns the equation for a
single dichotomy in symbolic (Greek-letter) form, labeled by its name.
The equation renders automatically in R Markdown and Quarto
documents.
Work dichotomy (not working vs. working)
extract_eq(wlf.nested, submodel = "work")Full-time dichotomy (part-time vs. full-time)
extract_eq(wlf.nested, submodel = "full")Using extract_eq() options
There are a wide variety of options you can pass to
extract_eq() to control the details of how the equations
are rendered in LateX. These include:
-
use_coefs: Use the model estimates in the equations instead of symbols, a nice way to display a fitted model - Options for coloring symbols in the equations:
greek_colors,var_colors,subscript_colorsand others. -
ital_vars: Logical, defaults toFALSE. Should the variable names not be wrapped in the\operatorname{}command so they appear in Roman text?
Equations with fitted coefficients
Passing use_coefs = TRUE substitutes the fitted
coefficient values into the equations.
extract_eq(wlf.nested, use_coefs = TRUE, submodel = "work")
extract_eq(wlf.nested, use_coefs = TRUE, submodel = "full")Coloring symbols
The greek_colors and var_colors arguments
control the color of the Greek coefficient symbols and the variable
names, respectively. This can help distinguish the structural parameters
from the predictors when displaying equations in presentations or
documents.
extract_eq(wlf.nested,
greek_colors = "blue",
submodel = "work")The color arguments accept any R color name or hex code, and can be a
vector to color each symbol differently. var_colors needs a
named vector of the variable names.
extract_eq(wlf.nested,
greek_colors = c("black", "blue", "blue"),
var_colors = c(hincome = "red", children="darkgreen"),
submodel = "work")Equations for individual sub-models
The individual binary logit sub-models (objects of class
"glm") can also be passed directly to
extract_eq(). This can be useful when you want to work with
a single dichotomy in isolation.
mod.work <- models(wlf.nested, "work")
extract_eq(mod.work)Note that the response is rendered as ..y — the internal
variable name used when fitting the sub-model — rather than as a
meaningful label. The extract_eq() method for a
"nestedLogit" model fixes this infelicity.
Using the raw LaTeX
Each equation returned by extract_eq() is an object of
class "equation" (from equatiomatic), which is
a character string containing the LaTeX source. This renders
automatically in R Markdown and Quarto documents. To access the raw
LaTeX — for example to paste it into a paper or to render it with
another tool such as katex — use
as.character():
cat(as.character(extract_eq(wlf.nested, submodel = "work")), "\n\n")
#> \log\left[ \frac { P( \operatorname{work} ) }{ 1 - P( \operatorname{work} ) } \right] = \alpha + \beta_{1}(\operatorname{hincome}) + \beta_{2}(\operatorname{children}_{\operatorname{present}})
cat(as.character(extract_eq(wlf.nested, submodel = "full")), "\n")
#> \log\left[ \frac { P( \operatorname{full} ) }{ 1 - P( \operatorname{full} ) } \right] = \alpha + \beta_{1}(\operatorname{hincome}) + \beta_{2}(\operatorname{children}_{\operatorname{present}})Alligator food choice: gators data
The gators data (built into nestedLogit)
records the primary food choice of alligators — Other, Fish, or
Invertebrates — as a function of body length. The three-category
response is decomposed into two dichotomies using
logits():
- other: {Other} vs. {Fish, Invertebrates}
- fish_inv: {Fish} vs. {Invertebrates}, among those not eating Other
data(gators)
gators.dichots <- logits(
other = dichotomy("Other", c("Fish", "Invertebrates")),
fish_inv = dichotomy("Fish", "Invertebrates")
)
gators.dichots
#> other: {Other} vs. {Fish, Invertebrates}
#> fish_inv: {Fish} vs. {Invertebrates}
gators.nested <- nestedLogit(food ~ length,
dichotomies = gators.dichots,
data = gators)Note that the dichotomy name fish_inv contains an
underscore. Because _ is the subscript operator in LaTeX,
extract_eq() replaces it with . in the
displayed equation (the submodel argument still uses the
original R name).
extract_eq(gators.nested, submodel = "other")
extract_eq(gators.nested, submodel = "fish_inv")With fitted coefficients:
extract_eq(gators.nested, use_coefs = TRUE, submodel = "other")
extract_eq(gators.nested, use_coefs = TRUE, submodel = "fish_inv")