% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/methods.R
\name{equation}
\alias{equation}
\alias{equation.lgspline}
\alias{print.equation}
\title{Print Closed-Form Fitted Equation from lgspline Model}
\usage{
equation(object, ...)

\method{equation}{lgspline}(
  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,
  ...
)

\method{print}{equation}(x, ...)

\method{equation}{lgspline}(
  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,
  ...
)

\method{print}{equation}(x, ...)
}
\arguments{
\item{object}{A fitted lgspline model object.}

\item{...}{Not used.}

\item{digits}{Integer; decimal places for coefficient display. Default 4.}

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

\item{show_bounds}{Logical; display partition bounds (1D) or knot midpoint
boundaries (multi-D). Default TRUE.}

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

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

\item{collapse_zero}{Logical; omit terms with coefficient exactly 0.
Default TRUE.}

\item{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 \eqn{df/dx_j} for each partition.}

\item{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 \eqn{d^2f/dx_j^2} for each partition.
Ignored if \code{first_derivative} is also non-NULL.}

\item{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
\eqn{\int f\, dx_j} for each partition, with an unspecified
constant of integration \eqn{C}. Ignored if \code{first_derivative}
or \code{second_derivative} is also non-NULL.}

\item{x}{An object returned by \code{equation()} for printing.}
}
\value{
Invisibly returns a list with components:
\describe{
  \item{formulas}{Character vector of equation strings per partition.}
  \item{bounds}{Matrix or list of partition boundary information.}
  \item{link}{Character; link function name.}
  \item{mode}{Character; one of "equation", "first_derivative",
        "second_derivative", or "antiderivative".}
  \item{variable}{Character; the variable name for the calculus operation,
        or NULL if mode is "equation".}
}
}
\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.
}
\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 \code{make_partition_list} contains
\code{knots} (midpoint boundaries between clusters), those are used directly.
Otherwise, cluster centers are displayed.

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.

\strong{Derivative and antiderivative modes.}
Only one of \code{first_derivative}, \code{second_derivative}, or
\code{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 \eqn{a x^n}, the first derivative is
\eqn{n a x^{n-1}}, the second derivative is \eqn{n(n-1) a x^{n-2}}, and
the antiderivative is \eqn{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
\code{equation()} repeatedly.
}
\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)

}
\seealso{
\code{\link{lgspline}}, \code{\link{plot.lgspline}},
  \code{\link{coef.lgspline}}
}
