Extract or create a LaTeX equation to describe a model, or directly from
LaTeX code. All objects supported by equatiomatic::extract_eq()
are
supported by the default method description.
Usage
equation(object, ...)
# S3 method for default
equation(
object,
auto.labs = TRUE,
origdata = NULL,
labs = NULL,
swap_var_names = NULL,
...
)
# S3 method for character
equation(object, ...)
eq__(object, ...)
eq_(object, ...)
# S3 method for inline_equation
print(x, ...)
Arguments
- object
An object with a model whose the equation is constructed. If a character object is provided, it is concatenated into a single character string and the equation class, otherwise non transformed (it is supposed to be a valid LaTeX equation). Remember that backslashes must be doubled in regular R strings, or use the
r"(...)"
notation, which may be more comfortable here, see examples.- ...
Further parameters passed to
equatiomatic::extract_eq()
(see its man page).- auto.labs
If
TRUE
(by default), use labels (and units) automatically from data ororigdata=
.- origdata
The original data set this model was fitted to. By default it is
NULL
and no label is used.- labs
Labels to change the names of elements in the
term
column of the table. By default it isNULL
and no term is changed.- swap_var_names
Change the variable names for these values, regardless of the values in
auto.labs=
orlabs=
that are ignored if this argument is used. Provide a named character string with name being the variables and strings the new names. You can use^
or_
to indicate next character, or next integer should be super) or subscript in the equation.- x
An inline_equation object generated by
eq_()
.`
Details
There are slight differences between equation()
, eq_()
and eq__()
:
equation()
returns a string with LaTeX code and prints LaTeX code at the R console.eq_()
returns the LaTeX code surrounded by a dollar sign$...$
and is suitable to build inline equations in R Markdown/Quarto documents by using inline R code. It prints the rendered inline equation in the RStudio Viewer or in the browser. So it is advised to rapidly preview the resulting equation.eq__()
returns the LaTeX code not surrounded by dollar signs in a simple character string. It just prompts the LaTeX string in the R console. It should be used in an R inline chunk inside a$$...$$
construct in a Markdown text. The result is a display equation that can also be cross referenced in Quarto in the usual way if you label it, e.g., you use$$...$$ {#eq-label}
.
Examples
iris_lm <- lm(data = iris, Petal.Length ~ Sepal.Length + Species)
summary(iris_lm)
#>
#> Call:
#> lm(formula = Petal.Length ~ Sepal.Length + Species, data = iris)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -0.76390 -0.17875 0.00716 0.17461 0.79954
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -1.70234 0.23013 -7.397 1.01e-11 ***
#> Sepal.Length 0.63211 0.04527 13.962 < 2e-16 ***
#> Speciesversicolor 2.21014 0.07047 31.362 < 2e-16 ***
#> Speciesvirginica 3.09000 0.09123 33.870 < 2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 0.2826 on 146 degrees of freedom
#> Multiple R-squared: 0.9749, Adjusted R-squared: 0.9744
#> F-statistic: 1890 on 3 and 146 DF, p-value: < 2.2e-16
#>
equation(iris_lm)
#> $$
#> \operatorname{Petal.Length} = \alpha + \beta_{1}(\operatorname{Sepal.Length}) + \beta_{2}(\operatorname{Species}_{\operatorname{versicolor}}) + \beta_{3}(\operatorname{Species}_{\operatorname{virginica}}) + \epsilon
#> $$
# Providing directly the LaTeX code of the equation (variance of a sample)
equation("S^2_y = \\sum_{i=1}^{n-1} \\frac{(y_i - \\bar{Y})^2}{n}")
#> $$
#> S^2_y = \sum_{i=1}^{n-1} \frac{(y_i - \bar{Y})^2}{n}
#> $$
# Easier to write like this (avoiding the double backslashes):
eq1 <- equation(r"(S^2_y = \sum_{i=1}^{n-1} \frac{(y_i - \bar{Y})^2}{n})")
# Print raw text:
eq1
#> $$
#> S^2_y = \sum_{i=1}^{n-1} \frac{(y_i - \bar{Y})^2}{n}
#> $$
# Get a preview of the equation
eq__(eq1)
#> [1] "S^2_y = \\sum_{i=1}^{n-1} \\frac{(y_i - \\bar{Y})^2}{n}"
# The same function can be used inside a `$$...$$ {#eq-label}` construct in
# R Markdown or Quarto to calcule a display equation that is also recognized
# by the cross referencing system of Quarto.
# Get a string suitable for inclusion inline in R Markdown with `r eq_(eq1)`
# (inside Markdown text and without the dollar signs around it)
eq_(eq1)