The equatiomatic package (Anderson, Heiss, and Sumners 2024) (https://github.com/datalorax/equatiomatic) developed by Daniel Anderson et al. provides a convenient and automatic way to show the equations corresponding to models fitted in R. We show a few brief examples below:
fit <- lm(mpg ~ cyl + disp, mtcars)
# show the theoretical model
equatiomatic::extract_eq(fit)
# display the actual coefficients
equatiomatic::extract_eq(fit, use_coefs = TRUE)
To display the actual math equations, you need the chunk option results = "asis"
(see Section 11.11 for the meaning of this option), otherwise the equations will be displayed as normal text output.
Please read the documentation and follow the development of this package on GitHub if you are interested in knowing more about it.
References
Anderson, Daniel, Andrew Heiss, and Jay Sumners. 2024. Equatiomatic: Transform Models into LaTeX Equations. https://github.com/datalorax/equatiomatic.