Skip to contents
loading...

Create the model equation of several self-starting nonlinear models available in the stats package.

Usage

# S3 method for nls
equation(
  object,
  ital_vars = FALSE,
  use_coefs = FALSE,
  coef_digits = 2L,
  fix_signs = TRUE,
  var_names = NULL,
  op_latex = c("\\cdot", "\\times"),
  ...
)

# S3 method for summary.nls
equation(
  object,
  ital_vars = FALSE,
  use_coefs = FALSE,
  coef_digits = 2L,
  fix_signs = TRUE,
  var_names = NULL,
  op_latex = c("\\cdot", "\\times"),
  ...
)

Arguments

object

An nls or summary.nls object.

ital_vars

Logical, defaults to FALSE. Should the variable names not be wrapped in the \operatorname command?

use_coefs

Logical, defaults to FALSE. Should the actual model estimates be included in the equation instead of math symbols? If TRUE, var_names= is ignored.

coef_digits

Integer, defaults to 2. The number of decimal places to round to when displaying model estimates with use_coefs = TRUE.

fix_signs

Logical, defaults to TRUE. 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)).

var_names

A named character vector as c(old_var_name = "new name")

op_latex

The LaTeX product operator character to use in fancy scientific notation, either \\cdot (by default), or \\times.

...

Additional arguments (not used yet).

Value

A character string with a LaTeX equation.

Examples

equation <- tabularise::equation
chick1 <- ChickWeight[ChickWeight$Chick == 1, ]
chick1_nls <- nls(data = chick1, weight ~ SSlogis(Time, Asym, xmid, scal))
summary(chick1_nls)
#> 
#> Formula: weight ~ SSlogis(Time, Asym, xmid, scal)
#> 
#> Parameters:
#>      Estimate Std. Error t value Pr(>|t|)    
#> Asym 937.0300   465.8677   2.011  0.07516 .  
#> xmid  35.2230     8.3120   4.238  0.00218 ** 
#> scal  11.4052     0.9052  12.599 5.08e-07 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 2.919 on 9 degrees of freedom
#> 
#> Number of iterations to convergence: 0 
#> Achieved convergence tolerance: 7.343e-06
#> 

equation(chick1_nls)
#> $$
#> \begin{aligned} \operatorname{weight} = \frac{Asym}{1 + e^{(xmid - \operatorname{Time}) /scal}} + \epsilon \end{aligned}
#> $$
equation(summary(chick1_nls))
#> $$
#> \begin{aligned} \operatorname{weight} = \frac{Asym}{1 + e^{(xmid - \operatorname{Time}) /scal}} + \epsilon \end{aligned}
#> $$

chick1_nls2 <- nls(data = chick1,
  weight ~ SSlogis(Time, Asym = A, xmid = x, scal = scale))
summary(chick1_nls2)
#> 
#> Formula: weight ~ SSlogis(Time, Asym = A, xmid = x, scal = scale)
#> 
#> Parameters:
#>       Estimate Std. Error t value Pr(>|t|)    
#> A     937.0300   465.8677   2.011  0.07516 .  
#> x      35.2230     8.3120   4.238  0.00218 ** 
#> scale  11.4052     0.9052  12.599 5.08e-07 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 2.919 on 9 degrees of freedom
#> 
#> Number of iterations to convergence: 0 
#> Achieved convergence tolerance: 7.343e-06
#> 

equation(chick1_nls2)
#> $$
#> \begin{aligned} \operatorname{weight} = \frac{A}{1 + e^{(x - \operatorname{Time}) /scale}} + \epsilon \end{aligned}
#> $$
equation(summary(chick1_nls2))
#> $$
#> \begin{aligned} \operatorname{weight} = \frac{A}{1 + e^{(x - \operatorname{Time}) /scale}} + \epsilon \end{aligned}
#> $$

equation(summary(chick1_nls2), var_names = c(
  weight = "Body weight [gm]",
  Time = "Number of days"))
#> $$
#> \begin{aligned} \operatorname{Body weight [gm]} = \frac{A}{1 + e^{(x - \operatorname{Number of days}) /scale}} + \epsilon \end{aligned}
#> $$