| lgspline.fit | R Documentation |
The core function for fitting Lagrangian smoothing splines with
less user-friendliness. Called internally by lgspline after
formula parsing, factor encoding, and correlation-structure setup.
lgspline.fit(
predictors,
y = NULL,
standardize_response = TRUE,
standardize_predictors_for_knots = TRUE,
standardize_expansions_for_fitting = TRUE,
family = gaussian(),
glm_weight_function = default_glm_weight_function,
schur_correction_function = function(X, y, B, dispersion, order_list, K, family,
observation_weights, ...) {
lapply(1:(K + 1), function(k) 0)
},
need_dispersion_for_estimation = FALSE,
dispersion_function = function(mu, y, order_indices, family, observation_weights,
VhalfInv, ...) {
if (!is.null(VhalfInv)) {
VhalfInv <-
VhalfInv[order_indices, order_indices]
c(mean((tcrossprod(VhalfInv, t(y -
mu)))^2/family$variance(mu)))
}
else {
c(mean((y -
mu)^2/family$variance(mu)))
}
},
K = NULL,
custom_knots = NULL,
cluster_on_indicators = FALSE,
make_partition_list = NULL,
previously_tuned_penalties = NULL,
smoothing_spline_penalty = NULL,
opt = TRUE,
use_custom_bfgs = TRUE,
delta = NULL,
tol = 10 * sqrt(.Machine$double.eps),
tuning_criterion = "loo",
gcv_gamma = 1.4,
initial_wiggle = c(2e-12, 2e-07, 2e-04, 0.2),
initial_flat = c(0.5, 5),
wiggle_penalty = 2e-07,
flat_ridge_penalty = 0.5,
unique_penalty_per_partition = TRUE,
unique_penalty_per_predictor = TRUE,
meta_penalty = 1e-08,
predictor_penalties = NULL,
partition_penalties = NULL,
include_quadratic_terms = TRUE,
include_cubic_terms = TRUE,
include_quartic_terms = FALSE,
include_2way_interactions = TRUE,
include_3way_interactions = TRUE,
include_quadratic_interactions = FALSE,
offset = c(),
just_linear_with_interactions = NULL,
just_linear_without_interactions = NULL,
exclude_interactions_for = NULL,
exclude_these_expansions = NULL,
custom_basis_fxn = NULL,
include_constrain_fitted = TRUE,
include_constrain_first_deriv = TRUE,
include_constrain_second_deriv = TRUE,
include_constrain_interactions = TRUE,
add_first_and_second_derivative_constraints = NULL,
qr_pivot_smoothing_constraints = TRUE,
cl = NULL,
chunk_size = NULL,
parallel_eigen = TRUE,
parallel_trace = FALSE,
parallel_aga = FALSE,
parallel_matmult = FALSE,
parallel_qr = FALSE,
parallel_bfgs = FALSE,
parallel_grideval = TRUE,
parallel_qr_qp = FALSE,
parallel_unconstrained = FALSE,
parallel_find_neighbors = TRUE,
parallel_penalty = FALSE,
parallel_make_constraint = TRUE,
unconstrained_fit_fxn = unconstrained_fit_default,
keep_weighted_Lambda = FALSE,
iterate_tune = TRUE,
iterate_final_fit = TRUE,
blockfit = TRUE,
qp_score_function = function(X, y, mu, order_list, dispersion, VhalfInv,
observation_weights, ...) {
default_qp_score_function(X, y, mu, order_list,
dispersion, VhalfInv, observation_weights, family, ...)
},
qp_observations = NULL,
qp_Amat = NULL,
qp_bvec = NULL,
qp_meq = 0,
qp_positive_derivative = FALSE,
qp_negative_derivative = FALSE,
qp_positive_2ndderivative = FALSE,
qp_negative_2ndderivative = FALSE,
qp_monotonic_increase = FALSE,
qp_monotonic_decrease = FALSE,
qp_range_upper = NULL,
qp_range_lower = NULL,
qr_pivot_inequality_constraints = FALSE,
qp_Amat_fxn = NULL,
qp_bvec_fxn = NULL,
qp_meq_fxn = NULL,
constraint_values = cbind(),
constraint_vectors = cbind(),
return_G = TRUE,
return_Ghalf = TRUE,
return_U = TRUE,
estimate_dispersion = TRUE,
unbias_dispersion = TRUE,
return_varcovmat = TRUE,
exact_varcovmat = FALSE,
return_lagrange_multipliers = FALSE,
custom_penalty_mat = NULL,
cluster_args = c(custom_centers = NA, nstart = 10),
dummy_dividor = 1.2345672152894e-22,
dummy_adder = 2.234567210529e-18,
verbose = FALSE,
verbose_tune = FALSE,
dummy_fit = FALSE,
auto_encode_factors = TRUE,
observation_weights = NULL,
do_not_cluster_on_these = c(),
neighbor_tolerance = 1 + 1e-16,
no_intercept = FALSE,
VhalfInv = NULL,
Vhalf = NULL,
include_warnings = TRUE,
og_cols = NULL,
factor_groups = NULL,
spline_groups = NULL,
additive_spline_interaction_pairs = NULL,
...
)
predictors |
Numeric matrix or data frame of predictor variables on the
low-level input scale expected by |
y |
Default: NULL. Numeric response variable vector. |
standardize_response |
Default: TRUE. Logical indicator controlling whether the response variable should be centered and scaled before model fitting. Only offered for identity link functions. |
standardize_predictors_for_knots |
Default: TRUE. Logical flag controlling
whether predictors are internally standardized for partitioning / knot
placement. The exact transformation is handled inside
|
standardize_expansions_for_fitting |
Default: TRUE. Logical switch to
standardize polynomial basis expansions during model fitting. Design matrices,
variance-covariance matrices, and coefficients are backtransformed after fitting.
|
family |
Default: |
glm_weight_function |
Default: GLM working weight
|
schur_correction_function |
Default: function returning list of zeros.
Computes Schur complements |
need_dispersion_for_estimation |
Default: FALSE. Logical indicator specifying whether a dispersion parameter is required for coefficient estimation (e.g. Weibull AFT). |
dispersion_function |
Default: function returning mean squared residuals. Custom function for estimating the exponential dispersion parameter. |
K |
Default: NULL. Integer specifying the number of knot locations. Intuitively, total partitions minus 1. |
custom_knots |
Default: NULL. Optional matrix providing user-specified knot locations in 1-D. |
cluster_on_indicators |
Default: FALSE. Logical flag for whether indicator variables should be used for clustering knot locations. |
make_partition_list |
Default: NULL. Optional list allowing direct specification
of custom partition assignments. The |
previously_tuned_penalties |
Default: NULL. Optional list of pre-computed penalty components from a previous model fit. |
smoothing_spline_penalty |
Default: NULL. Optional custom smoothing spline penalty matrix. |
opt |
Default: TRUE. Logical switch controlling automatic penalty optimization. |
use_custom_bfgs |
Default: TRUE. Selects between a native damped-BFGS implementation with closed-form gradients or base R's BFGS with finite-difference gradients. The native path is usually faster, while the finite-difference fallback can be preferable when tuning under exact LOO and the leverage derivative is numerically noisy. |
delta |
Default: NULL. Numeric pseudocount for stabilizing optimization in non-identity link function scenarios. |
tol |
Default: |
tuning_criterion |
Default: |
gcv_gamma |
Default: 1.4. Numeric scalar, at least 1, used only when
|
initial_wiggle |
Default: |
initial_flat |
Default: |
wiggle_penalty |
Default: 2e-7. Numeric penalty on the integrated squared second derivative, governing function smoothness. |
flat_ridge_penalty |
Default: 0.5. Numeric flat ridge penalty for intercepts and
linear terms only. Multiplied by |
unique_penalty_per_partition |
Default: TRUE. Logical flag allowing penalty magnitude to differ across partitions. |
unique_penalty_per_predictor |
Default: TRUE. Logical flag allowing penalty magnitude to differ between predictors. |
meta_penalty |
Default: 1e-8. Numeric regularization coefficient for predictor- and partition-specific penalties during tuning. On the raw scale, the implemented meta-penalty shrinks these penalty multipliers toward 1; the wiggle penalty receives only a tiny stabilizing penalty by default. |
predictor_penalties |
Default: NULL. Optional vector of custom penalties per predictor, on the raw (positive) scale. |
partition_penalties |
Default: NULL. Optional vector of custom penalties per partition, on the raw (positive) scale. |
include_quadratic_terms |
Default: TRUE. Logical switch to include squared predictor terms. |
include_cubic_terms |
Default: TRUE. Logical switch to include cubic predictor terms. |
include_quartic_terms |
Default: FALSE. Logical switch to include quartic predictor terms at this low-level interface. |
include_2way_interactions |
Default: TRUE. Logical switch for linear two-way interactions. |
include_3way_interactions |
Default: TRUE. Logical switch for three-way interactions. |
include_quadratic_interactions |
Default: FALSE. Logical switch for linear-quadratic interaction terms. |
offset |
Default: Empty vector. Column indices/names to include as offsets. Coefficients for offset terms are automatically constrained to 1. |
just_linear_with_interactions |
Default: NULL. Integer or character vector specifying predictors to retain as linear terms while still allowing interactions. |
just_linear_without_interactions |
Default: NULL. Integer or character vector specifying predictors to retain only as linear terms without interactions. Eligible for blockfitting. |
exclude_interactions_for |
Default: NULL. Integer or character vector of predictors to exclude from all interaction terms. |
exclude_these_expansions |
Default: NULL. Character vector of basis expansions to
exclude. Named columns of data, or in the form |
custom_basis_fxn |
Default: NULL. Optional user-defined function for custom basis
expansions. See |
include_constrain_fitted |
Default: TRUE. Logical switch to constrain fitted values at knot points. |
include_constrain_first_deriv |
Default: TRUE. Logical switch to constrain first derivatives at knot points. |
include_constrain_second_deriv |
Default: TRUE. Logical switch to constrain second derivatives at knot points. |
include_constrain_interactions |
Default: TRUE. Logical switch to constrain interaction terms at knot points. |
add_first_and_second_derivative_constraints |
Default: NULL. Logical
switch controlling how first- and second-derivative smoothness constraints
are assembled. If |
qr_pivot_smoothing_constraints |
Default: TRUE. Logical switch to reduce the smoothness/equality constraint matrix to a linearly independent set before fitting. Disabling this keeps the original equality columns. |
cl |
Default: NULL. Parallel processing cluster object
(use |
chunk_size |
Default: NULL. Integer specifying custom chunk size for parallel processing. |
parallel_eigen |
Default: TRUE. Logical flag for parallel eigenvalue
decomposition. Ignored inside tuning fits when |
parallel_trace |
Default: FALSE. Logical flag for parallel trace computation. |
parallel_aga |
Default: FALSE. Logical flag for parallel |
parallel_matmult |
Default: FALSE. Logical flag for parallel block-diagonal matrix multiplication. |
parallel_qr |
Default: FALSE. Logical flag for the tall-skinny
least-squares and rank-reduction steps that arise in transformed
constraint solves. When active and a cluster is supplied, these steps use
row-chunked cross-products with small dense fallback solves instead of
relying entirely on base QR; unstable cases fall back automatically to
|
parallel_bfgs |
Default: False. Logical flag for parallel evaluation of damped BFGS step candidates during penalty tuning. When active and a cluster is supplied, multiple damping factors are evaluated across workers and inner parallel flags are ignored for those fits. |
parallel_grideval |
Default: TRUE. Logical flag for parallel evaluation of the initial tuning grid. When active and a cluster is supplied, grid points are distributed across workers and inner parallel flags are ignored for those fits. |
parallel_qr_qp |
Default: FALSE. Logical flag for parallel QR pivot reduction of partition-local inequality constraint columns. When active and a cluster is supplied, each partition's reducible QP block is handled independently across workers before the final QP matrix is assembled. |
parallel_unconstrained |
Default: FALSE. Logical flag for parallel unconstrained MLE for non-identity-link-Gaussian models. |
parallel_find_neighbors |
Default: TRUE. Logical flag for parallel neighbor identification. |
parallel_penalty |
Default: FALSE. Logical flag for parallel penalty matrix construction. |
parallel_make_constraint |
Default: TRUE. Logical flag for parallel constraint matrix generation. |
unconstrained_fit_fxn |
Default: |
keep_weighted_Lambda |
Default: FALSE. Logical flag to retain GLM weights in penalty constraints using Tikhonov parameterization. Advised for non-canonical GLMs. |
iterate_tune |
Default: TRUE. Logical switch for iterative optimization during penalty tuning. |
iterate_final_fit |
Default: TRUE. Logical switch for iterative optimization in final model fitting. |
blockfit |
Default: TRUE. Logical switch for backfitting with mixed
spline and non-interactive linear terms. When the blockfit conditions are
met, both tuning and the final fit use |
qp_score_function |
Default: GLM score using
|
qp_observations |
Default: NULL. Either a numeric vector of
observation indices at which every active built-in QP constraint is
evaluated, or a named list keyed by |
qp_Amat |
Default: NULL. Optional pre-built QP constraint matrix.
In the current pipeline its presence marks QP handling as active, but the
built-in constructor does not merge it into the assembled constraint set;
use |
qp_bvec |
Default: NULL. Optional pre-built QP right-hand side paired
with |
qp_meq |
Default: 0. Optional number of equality constraints paired
with |
qp_positive_derivative |
Default: FALSE. Require nonnegative first
derivatives. Accepts |
qp_negative_derivative |
Default: FALSE. Require nonpositive first
derivatives. Same input types as |
qp_positive_2ndderivative |
Default: FALSE. Require nonnegative second
derivatives (convexity). Same input types as
|
qp_negative_2ndderivative |
Default: FALSE. Require nonpositive second
derivatives (concavity). Same input types as
|
qp_monotonic_increase |
Default: FALSE. Logical only. Require fitted values to be nondecreasing in observation order. |
qp_monotonic_decrease |
Default: FALSE. Logical only. Require fitted values to be nonincreasing in observation order. |
qp_range_upper |
Default: NULL. Optional upper bound on constrained fitted values. |
qp_range_lower |
Default: NULL. Optional lower bound on constrained fitted values. |
qr_pivot_inequality_constraints |
Default: FALSE. Logical switch to reduce partition-local inequality constraint columns to QR pivot columns before solving. Built-in range and monotonicity constraints are left unchanged, and more generally any inequality columns spanning multiple partitions are also left unchanged. |
qp_Amat_fxn |
Default: NULL. Custom function generating Amat. |
qp_bvec_fxn |
Default: NULL. Custom function generating bvec. |
qp_meq_fxn |
Default: NULL. Custom function generating meq. |
constraint_values |
Default: |
constraint_vectors |
Default: |
return_G |
Default: TRUE. Logical switch to return the unscaled unconstrained
variance-covariance matrix |
return_Ghalf |
Default: TRUE. Logical switch to return
|
return_U |
Default: TRUE. Logical switch to return the constraint projection
matrix |
estimate_dispersion |
Default: TRUE. Logical flag to estimate dispersion after fitting. |
unbias_dispersion |
Default: TRUE. Logical switch to multiply
dispersion by |
return_varcovmat |
Default: TRUE. Logical switch to return the variance-covariance matrix of estimated coefficients. Needed for Wald inference. |
exact_varcovmat |
Default: FALSE. Logical switch to replace the default
asymptotic (Bayesian posterior) variance-covariance matrix with the exact
frequentist variance-covariance matrix of the constrained estimator. The
asymptotic version uses the Hessian of
the penalized log-likelihood:
When a correlation structure is present ( |
return_lagrange_multipliers |
Default: FALSE. Logical switch to return the Lagrangian multiplier vector. |
custom_penalty_mat |
Default: NULL. Optional |
cluster_args |
Default: |
dummy_dividor |
Default: 0.00000000000000000000012345672152894. Small numeric constant to prevent division by zero. |
dummy_adder |
Default: 0.000000000000000002234567210529. Small numeric constant to prevent division by zero. |
verbose |
Default: FALSE. Logical flag to print general progress messages. |
verbose_tune |
Default: FALSE. Logical flag to print detailed progress during penalty tuning. |
dummy_fit |
Default: FALSE. Runs the full pipeline but sets coefficients to zero,
allowing inspection of design matrix structure, penalty matrices, and partitioning.
Replaces the deprecated |
auto_encode_factors |
Default: TRUE. Compatibility flag carried through
from higher-level preprocessing. Direct calls to |
observation_weights |
Default: NULL. Numeric vector of observation-specific weights for generalized least squares estimation. |
do_not_cluster_on_these |
Default: |
neighbor_tolerance |
Default: |
no_intercept |
Default: FALSE. Logical flag to constrain intercept to 0.
Formulas with |
VhalfInv |
Default: NULL. Fixed custom |
Vhalf |
Default: NULL. Fixed custom |
include_warnings |
Default: TRUE. Logical switch to control display of warnings. |
og_cols |
Default: NULL. Original predictor names |
factor_groups |
Named list mapping original factor variable names to
integer vectors of their corresponding one-hot indicator column positions
within the predictor matrix. Each element enforces a sum-to-zero equality
constraint on the linear-term coefficients of its indicator columns within
every partition, ensuring identifiability when all factor levels are
included without a reference/dropped level. For a group with indicator
columns at positions |
spline_groups |
Default: NULL. Optional list of predictor groups to fit
as separate additive spline terms. For example, |
additive_spline_interaction_pairs |
Default: NULL. Optional internal
list of two-element index vectors marking explicit parametric interactions
between separate additive spline terms, e.g. the |
... |
Additional arguments passed to the unconstrained model fitting
function. For additive spline groups, |
lgspline.fit performs the following steps:
Polynomial expansion and predictor standardization.
Knot placement and partitioning (k-means or custom).
Constraint matrix \mathbf{A} construction. Only a linearly
independent subset of columns is retained via pivoted QR decomposition.
Penalty tuning via exact leave-one-out by default, or generalized
cross-validation when tuning_criterion = "gcv", or use of
previously tuned penalties.
If spline_groups contains more than one group, each group is
fit as an additive spline term with its own partitioning and
constraints. Each conditional update is an ordinary
lgspline.fit Newton/SQP fit with the other terms held fixed
on the link scale.
Final coefficient estimation via one of three paths:
Blockfit option (when blockfit = TRUE,
flat columns are non-empty, K > 0, and no correlation
structure): Routes through blockfit_solve for
backfitting with mixed spline and non-interactive linear terms.
Falls back to get_B on failure.
Standard get_B path: Three internal
computational paths: GEE (damped SQP with correlation
structures), Gaussian identity (closed-form OLS projection),
and general GLM (unconstrained fit + Lagrangian projection
with Newton/Lagrangian projection updates).
Post-fit inference: \mathbf{U}, trace, dispersion,
variance-covariance matrix, and optionally Lagrange multipliers.
When VhalfInv is non-NULL, these are computed from
the whitened Gram matrices
\mathbf{X}^{\top}\mathbf{V}^{-1}\mathbf{X} via the full
penalized GLS information
\mathbf{G}_{\mathrm{correct}} =
(\mathbf{X}^{\top}\mathbf{V}^{-1}\mathbf{X} +
\boldsymbol{\Lambda})^{-1}.
Dummy fit. When dummy_fit = TRUE, an early-return path
skips the expensive fitting steps (compute_G_eigen, get_B,
trace computation, variance-covariance matrix) while retaining all
penalty, partitioning, and design matrix information. Coefficients are
set to zero. This replaces the deprecated expansions_only argument.
A list containing the fitted model components, forming the core
structure used internally by lgspline and its associated methods.
This function is primarily intended for internal use or advanced users needing
direct access to fitting components. The returned list contains numerous elements,
typically including:
The original response vector provided.
The fitted values on the original response scale. Set to
rep(0, N) when dummy_fit = TRUE.
A list, with each element the design matrix (\mathbf{X}_{k})
for partition k, on the unstandardized expansion scale.
The constraint matrix (\mathbf{A}) encoding smoothness and
any other linear equality constraints. Reduced to linearly independent
columns via pivoted QR decomposition.
A list of the final fitted coefficient vectors
(\boldsymbol{\beta}_{k}) for each partition k, on the original
predictor/response scale.
A list of fitted coefficient vectors on the internally standardized scale used during fitting.
Key dimensions: number of internal knots (K), basis functions per partition (p), original predictors (q), total coefficients (P), and sample size (N).
A list containing the final penalty components used
(e.g., Lambda, L1, L2, L_predictor_list,
L_partition_list). See compute_Lambda.
Functions to transform predictors to/from the scale used for knot placement.
Matrix or vector of knot locations on the original predictor scale (NULL if K=0 or q > 1).
Vector assigning each original observation to a partition.
Internal representation of partition boundaries.
List containing centers, knot midpoints, neighbor
info, and assignment function from partitioning (NULL if K=0 or 1D).
See make_partitions.
Internal functions for
partitioning data. See knot_expand_list.
The primary function embedded in the object for generating
predictions on new data. For multi-predictor models,
take_first_derivatives = TRUE returns derivatives as a named list
of per-variable derivative vectors rather than a concatenated vector.
See predict.lgspline.
The family object or custom list used.
Logical flags related to dispersion estimation settings.
The estimated (or fixed) dispersion parameter
\tilde{\sigma}^{2}. For Gaussian identity fits with
VhalfInv non-NULL, this is computed from whitened
residuals \mathbf{V}^{-1/2}(\mathbf{y} - \hat{\mathbf{y}}),
multiplied by the observation weights and the optional
bias-correction factor. When estimate_dispersion = FALSE,
set to 1. Omitted when dummy_fit = TRUE.
Functions to convert coefficients between standardized and original scales.
Mean and standard deviation used for standardizing the response.
Information mapping original data order to partitioned order.
User-supplied additional linear equality constraints.
Scaling factors applied to basis expansions during
fitting (if standardize_expansions_for_fitting = TRUE).
Functions related to computing derivatives of the fitted spline.
Integer vectors storing column indices identifying different types of terms in the basis expansion.
Logical indicating if variance matrix calculation was requested.
Not returned as a standalone component; this
argument only controls whether varcovmat, when requested, is
left as the default asymptotic/Laplace version or replaced by the
exact frequentist correction available for Gaussian identity fits.
Original generated names for basis expansion columns (before potential renaming if input predictors had names).
Functions to standardize/unstandardize design matrices
according to expansion_scales.
Logical indicating if a parallel cluster was used.
The original observation weights provided (potentially reformatted).
The fixed \mathbf{V}^{-1/2} matrix if supplied.
List containing components related to quadratic programming constraints, if used.
List of unscaled variance-covariance matrices
\mathbf{G}_{k} per partition, returned if return_G = TRUE.
When VhalfInv is non-NULL, recomputed from whitened Gram
matrices. Omitted when dummy_fit = TRUE.
List of \mathbf{G}_{k}^{1/2} matrices, returned if
return_Ghalf = TRUE. When VhalfInv is non-NULL,
the full \mathbf{G}_{\mathrm{correct}}^{1/2} is used for
posterior draws and variance-covariance computation.
Omitted when dummy_fit = TRUE.
Constraint projection matrix \mathbf{U}, returned if
return_U = TRUE. Omitted when dummy_fit = TRUE.
The effective degrees-of-freedom trace term.
When VhalfInv is non-NULL, it is computed from the
full penalized GLS information rather than the block-diagonal
approximation. Omitted when dummy_fit = TRUE.
The final variance-covariance matrix of the estimated
coefficients. Computed via the outer-product form
\sigma^{2}(\mathbf{U}\mathbf{G}^{1/2})(\mathbf{U}\mathbf{G}^{1/2})^{\top}
for numerical stability. When VhalfInv is non-NULL, uses
the full \mathbf{G}_{\mathrm{correct}}^{1/2} in place of
block-diagonal \mathbf{G}^{1/2}. Returned if
return_varcovmat = TRUE. By default this is the asymptotic
(Laplace/posterior) variance-covariance matrix; when
exact_varcovmat = TRUE, it is replaced in-place by the exact
frequentist correction available for Gaussian identity fits.
Omitted when dummy_fit = TRUE.
Vector of Lagrangian multipliers if
return_lagrange_multipliers = TRUE. For equality-only fits these
follow the formulation
(\mathbf{A}^{\top}\mathbf{G}\mathbf{A})^{-1}\mathbf{A}^{\top}(\hat{\boldsymbol{\beta}} - \boldsymbol{\beta_0}).
When quadratic-programming constraints are active they are taken
directly from solve.QP and therefore refer to the combined
equality/inequality constraint system. NULL if no constraints
are active
.
Note that the exact components returned depend heavily on the function
arguments (e.g., values of return_G, return_varcovmat, etc.)
and whether dummy_fit = TRUE.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.