equation: Print Closed-Form Fitted Equation from lgspline Model

View source: R/methods.R

equationR Documentation

Print Closed-Form Fitted Equation from lgspline Model

Description

Displays the closed-form polynomial equation for each partition of a fitted lgspline model, along with partition boundary or cluster center information. Optionally prints the first derivative, second derivative, or antiderivative of the fitted equation with respect to a single specified variable.

Usage

equation(object, ...)

## S3 method for class 'lgspline'
equation(
  object,
  digits = 4,
  scientific = FALSE,
  show_bounds = TRUE,
  predictor_names = NULL,
  response_name = NULL,
  collapse_zero = TRUE,
  first_derivative = NULL,
  second_derivative = NULL,
  antiderivative = NULL,
  ...
)

## S3 method for class 'additive_lgspline'
equation(
  object,
  digits = 4,
  scientific = FALSE,
  show_bounds = TRUE,
  predictor_names = NULL,
  response_name = NULL,
  collapse_zero = TRUE,
  first_derivative = NULL,
  second_derivative = NULL,
  antiderivative = NULL,
  ...
)

## S3 method for class 'equation'
print(x, ...)

## S3 method for class 'lgspline'
equation(
  object,
  digits = 4,
  scientific = FALSE,
  show_bounds = TRUE,
  predictor_names = NULL,
  response_name = NULL,
  collapse_zero = TRUE,
  first_derivative = NULL,
  second_derivative = NULL,
  antiderivative = NULL,
  ...
)

## S3 method for class 'equation'
print(x, ...)

Arguments

object

A fitted lgspline model object.

...

Not used.

digits

Integer; decimal places for coefficient display. Default 4.

scientific

Logical; use scientific notation for coefficients with absolute value < 1e-3 or > 1e4. Default FALSE.

show_bounds

Logical; display partition bounds (1D) or knot midpoint boundaries (multi-D). Default TRUE.

predictor_names

Character vector; custom names for predictor variables. If NULL (default), uses original column names or "_j_" labels.

response_name

Character; label for response. If NULL (default), uses "y" for identity link Gaussian, or "link(E[y])" otherwise.

collapse_zero

Logical; omit terms with coefficient exactly 0. Default TRUE.

first_derivative

Default: NULL. Character name or integer index of the predictor variable with respect to which the first derivative is printed. Only one variable at a time is supported. When non-NULL, the printed equations show df/dx_j for each partition.

second_derivative

Default: NULL. Character name or integer index of the predictor variable with respect to which the second derivative is printed. Only one variable at a time is supported. When non-NULL, the printed equations show d^2f/dx_j^2 for each partition. Ignored if first_derivative is also non-NULL.

antiderivative

Default: NULL. Character name or integer index of the predictor variable with respect to which the antiderivative (indefinite integral) is printed. Only one variable at a time is supported. When non-NULL, the printed equations show \int f\, dx_j for each partition, with an unspecified constant of integration C. Ignored if first_derivative or second_derivative is also non-NULL.

x

An object returned by equation() for printing.

Details

For 1D models with K knots, partition boundaries are displayed as intervals on the predictor scale. For multi-predictor models, partition boundaries are computed as the midpoints between adjacent cluster centers along each predictor dimension. When the model's make_partition_list contains knots (midpoint boundaries between clusters), those are used directly. Otherwise, cluster centers are displayed.

For additive_lgspline objects, equations are printed term-by-term because each smooth may have its own partitioning scheme. The internal additive offset column is suppressed from the displayed term equations.

Coefficients are displayed on the original (unstandardized) predictor scale. For GLMs with non-identity link, the left-hand side shows the link function applied to the expected response.

Derivative and antiderivative modes. Only one of first_derivative, second_derivative, or antiderivative may be non-NULL. If more than one is supplied, the priority order is: first derivative, second derivative, antiderivative.

Derivatives and antiderivatives are computed symbolically from the polynomial coefficients. For a term a x^n, the first derivative is n a x^{n-1}, the second derivative is n(n-1) a x^{n-2}, and the antiderivative is a x^{n+1}/(n+1). Cross-terms (interactions) involving the target variable are differentiated or integrated with respect to that variable only, treating all other variables as constants.

A warning is emitted if the user attempts to differentiate or integrate with respect to more than one variable simultaneously. Multi-variable calculus operations should be performed one variable at a time by calling equation() repeatedly.

Value

Invisibly returns a list with components:

formulas

Character vector of equation strings per partition.

bounds

Matrix or list of partition boundary information.

link

Character; link function name.

mode

Character; one of "equation", "first_derivative", "second_derivative", or "antiderivative".

variable

Character; the variable name for the calculus operation, or NULL if mode is "equation".

See Also

lgspline, plot.lgspline, coef.lgspline

Examples


## 1D example
set.seed(1234)
t <- runif(500, -5, 5)
y <- 2*sin(t) + 0.1*t^2 + rnorm(length(t), 0, 0.5)
fit <- lgspline(t, y, K = 2)
equation(fit)
equation(fit, digits = 2, predictor_names = "time")

## First derivative with respect to predictor
equation(fit, first_derivative = 1)

## Second derivative
equation(fit, second_derivative = 1)

## Antiderivative
equation(fit, antiderivative = 1)

## 2D example with named predictors
x1 <- runif(300, 0, 10)
x2 <- runif(300, 0, 10)
y <- x1 + 0.5*x2 + 0.1*x1*x2 + rnorm(300)
fit2d <- lgspline(cbind(x1, x2), y, K = 3)
equation(fit2d, predictor_names = c("Length", "Width"))

## Derivative w.r.t. first variable only
equation(fit2d, first_derivative = "Length",
         predictor_names = c("Length", "Width"))

## GLM example
y_bin <- rbinom(500, 1, plogis(0.5*t))
fit_glm <- lgspline(t, y_bin, family = binomial(), K = 1)
equation(fit_glm)


lgspline documentation built on Aug. 5, 2026, 1:10 a.m.