extract_eq(
model,
intercept = "alpha",
greek = "beta",
raw_tex = FALSE,
ital_vars = FALSE,
show_distribution = FALSE,
wrap = FALSE,
terms_per_line = 4,
operator_location = "end",
align_env = "aligned",
use_coefs = FALSE,
coef_digits = 2,
fix_signs = TRUE,
font_size,
mean_separate,
...
)

## Arguments

model A fitted model How should the intercept be displayed? Default is "alpha", but can also accept "beta", in which case the it will be displayed as beta zero. What notation should be used for coefficients? Currently only accepts "beta" (with plans for future development). Can be used in combination with raw_tex to use any notation, e.g., "\hat{\beta}". Logical. Is the greek code being passed to denote coefficients raw tex code? Logical, defaults to FALSE. Should the variable names not be wrapped in the \operatorname{} command? Logical. When fitting a logistic or probit regression, should the binomial distribution be displayed? Defaults to FALSE. Logical, defaults to FALSE. Should the terms on the right-hand side of the equation be split into multiple lines? This is helpful with models with many terms. Integer, defaults to 4. The number of right-hand side terms to include per line. Used only when wrap is TRUE. Character, one of “end” (the default) or “start”. When terms are split across multiple lines, they are split at mathematical operators like +. If set to “end”, each line will end with a trailing operator (+ or -). If set to “start”, each line will begin with an operator. TeX environment to wrap around equation. Must be one of aligned, aligned*, align, or align*. Defaults to aligned. Logical, defaults to FALSE. Should the actual model estimates be included in the equation instead of math symbols? Integer, defaults to 2. The number of decimal places to round to when displaying model estimates. Logical, defaults to FALSE. If disabled, coefficient estimates that are negative are preceded with a "+" (e.g. 5(x) + -3(z)). If enabled, the "+ -" is replaced with a "-" (e.g. 5(x) - 3(z)). The font size of the equation. Defaults to default of the output format. Takes any of the standard LaTeX arguments (see here). Currently only support for lmer models. Should the mean structure be inside or separated from the normal distribution? Defaults to NULL, in which case it will become TRUE if there are more than three fixed-effect parameters. If TRUE, the equation will be displayed as, for example, outcome ~ N(mu, sigma); mu = alpha + beta_1(wave). If FALSE, this same equation would be outcome ~ N(alpha + beta, sigma). Additional arguments (for future development; not currently used).

## Value

A character of class “equation”.

## Details

Extract the variable names from a model to produce a 'LaTeX' equation, which is output to the screen. Supports any model supported by broom::tidy.

## Examples

# Simple model
mod1 <- lm(mpg ~ cyl + disp, mtcars)
extract_eq(mod1)#> $$#> \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \epsilon #>$$
# Include all variables
mod2 <- lm(mpg ~ ., mtcars)
extract_eq(mod2)#> $$#> \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \beta_{3}(\operatorname{hp}) + \beta_{4}(\operatorname{drat}) + \beta_{5}(\operatorname{wt}) + \beta_{6}(\operatorname{qsec}) + \beta_{7}(\operatorname{vs}) + \beta_{8}(\operatorname{am}) + \beta_{9}(\operatorname{gear}) + \beta_{10}(\operatorname{carb}) + \epsilon #>$$
# Works for categorical variables too, putting levels as subscripts
mod3 <- lm(body_mass_g ~ bill_length_mm + species, penguins)
extract_eq(mod3)#> $$#> \operatorname{body\_mass\_g} = \alpha + \beta_{1}(\operatorname{bill\_length\_mm}) + \beta_{2}(\operatorname{species}_{\operatorname{Chinstrap}}) + \beta_{3}(\operatorname{species}_{\operatorname{Gentoo}}) + \epsilon #>$$
set.seed(8675309)
d <- data.frame(
cat1 = rep(letters[1:3], 100),
cat2 = rep(LETTERS[1:3], each = 100),
cont1 = rnorm(300, 100, 1),
cont2 = rnorm(300, 50, 5),
out = rnorm(300, 10, 0.5)
)
mod4 <- lm(out ~ ., d)
extract_eq(mod4)#> $$#> \operatorname{out} = \alpha + \beta_{1}(\operatorname{cat1}_{\operatorname{b}}) + \beta_{2}(\operatorname{cat1}_{\operatorname{c}}) + \beta_{3}(\operatorname{cat2}_{\operatorname{B}}) + \beta_{4}(\operatorname{cat2}_{\operatorname{C}}) + \beta_{5}(\operatorname{cont1}) + \beta_{6}(\operatorname{cont2}) + \epsilon #>$$
# Don't italicize terms
extract_eq(mod1, ital_vars = FALSE)#> $$#> \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \epsilon #>$$
# Wrap equations in an "aligned" environment
extract_eq(mod2, wrap = TRUE)#> #> \begin{aligned} #> \operatorname{mpg} &= \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \beta_{3}(\operatorname{hp})\ + \\ #> &\quad \beta_{4}(\operatorname{drat}) + \beta_{5}(\operatorname{wt}) + \beta_{6}(\operatorname{qsec}) + \beta_{7}(\operatorname{vs})\ + \\ #> &\quad \beta_{8}(\operatorname{am}) + \beta_{9}(\operatorname{gear}) + \beta_{10}(\operatorname{carb}) + \epsilon #> \end{aligned} #>
# Wider equation wrapping
extract_eq(mod2, wrap = TRUE, terms_per_line = 4)#> #> \begin{aligned} #> \operatorname{mpg} &= \alpha + \beta_{1}(\operatorname{cyl}) + \beta_{2}(\operatorname{disp}) + \beta_{3}(\operatorname{hp})\ + \\ #> &\quad \beta_{4}(\operatorname{drat}) + \beta_{5}(\operatorname{wt}) + \beta_{6}(\operatorname{qsec}) + \beta_{7}(\operatorname{vs})\ + \\ #> &\quad \beta_{8}(\operatorname{am}) + \beta_{9}(\operatorname{gear}) + \beta_{10}(\operatorname{carb}) + \epsilon #> \end{aligned} #>
# Include model estimates instead of Greek letters
extract_eq(mod2, wrap = TRUE, terms_per_line = 2, use_coefs = TRUE)#> #> \begin{aligned} #> \operatorname{\widehat{mpg}} &= 12.3 - 0.11(\operatorname{cyl})\ + \\ #> &\quad 0.01(\operatorname{disp}) - 0.02(\operatorname{hp})\ + \\ #> &\quad 0.79(\operatorname{drat}) - 3.72(\operatorname{wt})\ + \\ #> &\quad 0.82(\operatorname{qsec}) + 0.32(\operatorname{vs})\ + \\ #> &\quad 2.52(\operatorname{am}) + 0.66(\operatorname{gear})\ - \\ #> &\quad 0.2(\operatorname{carb}) #> \end{aligned} #>
# Don't fix doubled-up "+ -" signs
extract_eq(mod2, wrap = TRUE, terms_per_line = 4, use_coefs = TRUE, fix_signs = FALSE)#> #> \begin{aligned} #> \operatorname{\widehat{mpg}} &= 12.3 + -0.11(\operatorname{cyl}) + 0.01(\operatorname{disp}) + -0.02(\operatorname{hp})\ + \\ #> &\quad 0.79(\operatorname{drat}) + -3.72(\operatorname{wt}) + 0.82(\operatorname{qsec}) + 0.32(\operatorname{vs})\ + \\ #> &\quad 2.52(\operatorname{am}) + 0.66(\operatorname{gear}) + -0.2(\operatorname{carb}) #> \end{aligned} #>
# Use other model types, like glm
set.seed(8675309)
d <- data.frame(
out = sample(0:1, 100, replace = TRUE),
cat1 = rep(letters[1:3], 100),
cat2 = rep(LETTERS[1:3], each = 100),
cont1 = rnorm(300, 100, 1),
cont2 = rnorm(300, 50, 5)
)
mod5 <- glm(out ~ ., data = d, family = binomial(link = "logit"))
extract_eq(mod5, wrap = TRUE)#> #> \begin{aligned} #> \log\left[ \frac { P( \operatorname{out} = \operatorname{1} ) }{ 1 - P( \operatorname{out} = \operatorname{1} ) } \right] &= \alpha + \beta_{1}(\operatorname{cat1}_{\operatorname{b}}) + \beta_{2}(\operatorname{cat1}_{\operatorname{c}}) + \beta_{3}(\operatorname{cat2}_{\operatorname{B}})\ + \\ #> &\quad \beta_{4}(\operatorname{cat2}_{\operatorname{C}}) + \beta_{5}(\operatorname{cont1}) + \beta_{6}(\operatorname{cont2}) #> \end{aligned} #>