Skip to contents

Overview

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
data(Womenlf, package = "carData")

comparisons <- logits(
  work = dichotomy("not.work", working = c("parttime", "fulltime")),
  full = dichotomy("parttime", "fulltime")
)

wlf.nested <- nestedLogit(partic ~ hincome + children,
                           dichotomies = comparisons,
                           data = Womenlf)

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")

log[P(work)1P(work)]=α+β1(hincome)+β2(childrenpresent) \log\left[ \frac { P( \operatorname{work} ) }{ 1 - P( \operatorname{work} ) } \right] = \alpha + \beta_{1}(\operatorname{hincome}) + \beta_{2}(\operatorname{children}_{\operatorname{present}})

Full-time dichotomy (part-time vs. full-time)

extract_eq(wlf.nested, submodel = "full")

log[P(full)1P(full)]=α+β1(hincome)+β2(childrenpresent) \log\left[ \frac { P( \operatorname{full} ) }{ 1 - P( \operatorname{full} ) } \right] = \alpha + \beta_{1}(\operatorname{hincome}) + \beta_{2}(\operatorname{children}_{\operatorname{present}})

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_colors and others.
  • ital_vars: Logical, defaults to FALSE. 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")

log[P(work)̂1P(work)̂]=1.340.04(hincome)1.58(childrenpresent) \log\left[ \frac { \widehat{P( \operatorname{work} )} }{ 1 - \widehat{P( \operatorname{work} )} } \right] = 1.34 - 0.04(\operatorname{hincome}) - 1.58(\operatorname{children}_{\operatorname{present}})

extract_eq(wlf.nested, use_coefs = TRUE, submodel = "full")

log[P(full)̂1P(full)̂]=3.480.11(hincome)2.65(childrenpresent) \log\left[ \frac { \widehat{P( \operatorname{full} )} }{ 1 - \widehat{P( \operatorname{full} )} } \right] = 3.48 - 0.11(\operatorname{hincome}) - 2.65(\operatorname{children}_{\operatorname{present}})

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")

log[P(work)1P(work)]=α+β1(hincome)+β2(childrenpresent) \log\left[ \frac { P( \operatorname{work} ) }{ 1 - P( \operatorname{work} ) } \right] = {\color{blue}{\alpha}} + {\color{blue}{\beta}}_{1}(\operatorname{hincome}) + {\color{blue}{\beta}}_{2}(\operatorname{children}_{\operatorname{present}})

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")

log[P(work)1P(work)]=α+β1(hincome)+β2(childrenpresent) \log\left[ \frac { P( \operatorname{work} ) }{ 1 - P( \operatorname{work} ) } \right] = {\color{black}{\alpha}} + {\color{blue}{\beta}}_{1}({\color{red}{\operatorname{hincome}}}) + {\color{blue}{\beta}}_{2}({\color{darkgreen}{\operatorname{children}}}_{\operatorname{present}})

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)

log[P(..y=1)1P(..y=1)]=α+β1(hincome)+β2(childrenpresent) \log\left[ \frac { P( \operatorname{..y} = \operatorname{1} ) }{ 1 - P( \operatorname{..y} = \operatorname{1} ) } \right] = \alpha + \beta_{1}(\operatorname{hincome}) + \beta_{2}(\operatorname{children}_{\operatorname{present}})

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")

log[P(other)1P(other)]=α+β1(length) \log\left[ \frac { P( \operatorname{other} ) }{ 1 - P( \operatorname{other} ) } \right] = \alpha + \beta_{1}(\operatorname{length})

extract_eq(gators.nested, submodel = "fish_inv")

log[P(fish.inv)1P(fish.inv)]=α+β1(length) \log\left[ \frac { P( \operatorname{fish.inv} ) }{ 1 - P( \operatorname{fish.inv} ) } \right] = \alpha + \beta_{1}(\operatorname{length})

With fitted coefficients:

extract_eq(gators.nested, use_coefs = TRUE, submodel = "other")

log[P(other)̂1P(other)̂]=3.130.57(length) \log\left[ \frac { \widehat{P( \operatorname{other} )} }{ 1 - \widehat{P( \operatorname{other} )} } \right] = 3.13 - 0.57(\operatorname{length})

extract_eq(gators.nested, use_coefs = TRUE, submodel = "fish_inv")

log[P(fish.inv)̂1P(fish.inv)̂]=4.322.48(length) \log\left[ \frac { \widehat{P( \operatorname{fish.inv} )} }{ 1 - \widehat{P( \operatorname{fish.inv} )} } \right] = 4.32 - 2.48(\operatorname{length})

References

Anderson, D., Heiss, A., & Sumners, J. (2023). Equatiomatic: Transform models into LaTeX equations.
Fox, J., & Friendly, M. (2026). nestedLogit: Nested dichotomy logistic regression models. Retrieved from https://github.com/friendly/nestedLogit