Fits a Multivariate Adaptive Regression Splines (MARS) model
(Friedman 1991). A forward pass adds pairs of hinge basis functions
of the form max(0, +/-(x - knot)); a backward pass prunes terms by
minimising the GCV criterion (or a K-fold CV criterion if requested).
Usage
ares(x, ...)
# S3 method for class 'formula'
ares(x, data = NULL, subset = NULL, ..., y = NULL)
# Default S3 method
ares(
x,
y,
degree = 1L,
nk = NULL,
penalty = NULL,
thresh = 0.001,
minspan = 0L,
endspan = 0L,
adjust.endspan = 1L,
auto.linpreds = FALSE,
fast.k = 10L,
fast.beta = 1,
nprune = NULL,
pmethod = c("backward", "none", "cv"),
nfold = 0L,
ncross = 1L,
stratify = TRUE,
seed.cv = NULL,
cv.1se = FALSE,
autotune = FALSE,
autotune.speed = c("balanced", "quality", "fast"),
autotune.warmstart = TRUE,
n.boot = 0L,
na.action = c("impute", "omit"),
family = c("gaussian", "binomial", "poisson", "gamma"),
weights = NULL,
varmod = c("none", "const", "lm"),
trace = 0L,
nthreads = 0L,
...
)Arguments
- x
A numeric matrix or data frame of predictors, or a model formula when calling the formula method.
- ...
Currently ignored.
- data
A data frame. Used only by the formula method.
- subset
Optional row-subsetting vector for the formula method (integer indices or logical), passed through to
stats::model.frame(). Ignored by the default (matrix/data-frame) method.- y
A numeric response vector with length
nrow(x). Forfamily = "binomial", a 0/1 numeric, logical, or 2-level factor.- degree
Maximum interaction degree. Default
1(additive). Use2or3for two- or three-way interactions.- nk
Maximum number of basis terms in the forward pass. Default scales with
ncol(x):min(200, max(20, 2 * ncol(x))) + 1.- penalty
GCV penalty per knot. Default
2fordegree = 1,3otherwise. Larger values produce sparser fits.- thresh
Forward-pass early-stop threshold on relative RSS improvement. Default
0.001.- minspan
Minimum gap between knots along a variable.
0(default) picks an automatic value fromnandp.- endspan
Distance from the data ends within which knots are forbidden.
0(default) picks an automatic value.- adjust.endspan
Multiplier applied to
endspanwhen the candidate hinge would deepen an existing interaction. Default1forgaussian,poisson,gamma;2forbinomial. Pass an explicit value to override.- auto.linpreds
If
TRUE, hinge pairs whose best knot sits at a variable's range boundary are replaced by a linear term. DefaultFALSEforgaussian,poisson,gamma;TRUEforbinomial. Pass an explicit value to override.- fast.k
Size of the Fast-MARS candidate cache (Friedman 1993). Larger values rescore more candidates each step (slower, slightly more accurate);
0rescores every candidate every step. Default10.- fast.beta
Age penalty for stale entries in the Fast-MARS cache. Default
1.0. Only relevant whenfast.k > 0.- nprune
Maximum number of terms after backward pruning. Default
nk.- pmethod
Pruning method.
"backward"(default): minimise GCV over backward-elimination subsets."none": keep all forward-pass terms."cv": K-fold cross-validated subset selection. Requiresnfold > 0(setsnfold = 5if not specified).
- nfold
Number of CV folds for
pmethod = "cv". Default0(no CV; GCV-based pruning). Whennfold > 0,pmethodis promoted to"cv".- ncross
Number of CV repetitions (each builds a fresh fold partition; per-size mean MSE is averaged). Default
1.- stratify
If
TRUE(default), CV folds are quantile-stratified ony. Useful for smallnor skewed responses.- seed.cv
Optional integer seed for the CV fold partition. Pass an integer for reproducible CV;
NULL(default) uses the current RNG state.- cv.1se
If
TRUE, applies the one-standard-error rule underpmethod = "cv": among sizes within one SE of the minimum mean CV-MSE, pick the smallest. DefaultFALSE(pick the argmin).- autotune
If
TRUE, runs an inner cross-validated grid search over(degree, penalty, nk, fast.k)and refits the winner on the full data. The chosen settings and full grid scores are returned in$autotune. DefaultFALSE.- autotune.speed
Speed/quality trade-off for
autotune. Only used whenautotune = TRUE."balanced"(default): explores a moderatefast.krange and picks the smallest whose CV-MSE is within 1% of the best."quality": disables the Fast-MARS cache (most thorough, slowest)."fast": forces an aggressive cache (cheapest, slight accuracy trade-off).
- autotune.warmstart
If
TRUE(default),autotunefirst tunes on a small subsample. If one cell wins decisively, the full grid is skipped and the winner is refit on all rows. Only applies whenautotune = TRUEandn >= 200.- n.boot
Number of bootstrap replicate fits for bagging. Default
0(no bagging). When> 0,predict()averages over the replicates plus the central fit and (withse.fit = TRUE) returns a per-prediction bag standard deviation. Composes withautotune: each replicate reuses the central fit's chosen hyperparameters.- na.action
Strategy for missing values in
x."impute"(default) replaces each column'sNAs with that column's training median and stores the medians forpredict()to reuse."omit"drops rows with anyNA. Either action warns. Missing values iny, orNaN/Infanywhere inx, are always rejected.- family
Response family for the final coefficient fit.
"gaussian"(default): identity link, OLS."binomial": logit link.ymay be 0/1 numeric, logical, or a 2-level factor."poisson": log link.ymust be non-negative integer-valued."gamma": log link.ymust be strictly positive. For non-gaussian families, term selection runs on the numericy(fast); the selected basis is then refit on the response scale viastats::glm.fit()with the requested family.
- weights
Optional non-negative numeric vector of length
nrow(x)for weighted least-squares fitting. DefaultNULL(unweighted). Term selection, GCV / CV pruning, and autotune all respect the weights. Negative,NA, orNaNweights are rejected.- varmod
Residual variance model used by
predict()for prediction intervals (gaussian only)."none"(default): no variance model is stored;interval = "pint"is unavailable."const": stores a single residual SD; intervals areyhat +/- qt(level, df) * sigma."lm": fits a small linear model of|resid|onyhatto allow simple yhat-dependent heteroscedasticity. Captures residual scale that changes linearly with the fitted mean. Does NOT capture residual scale that depends on a predictor whose contribution toyhatis small – in that case the |resid| ~ yhat slope is close to zero and"lm"collapses to roughly the same as"const". If you suspect x-driven heteroscedasticity (e.g. variance depends on a covariate orthogonal to the mean structure),varmod = "lm"will not help and coverage will degrade in high-variance regions. Ignored for non-gaussian families.
- trace
Trace level.
0(default) is silent;1reports forward-pass progress.- nthreads
Number of threads.
0(default) usesRcppParallel::defaultNumThreads(). Examples and the vignette cap this at2for CRAN compliance.
Value
An object of class "ares": a list containing the fitted
coefficients, the basis matrix bx, the term directions dirs
and knots cuts, the indices of selected.terms, training rss
and gcv, fitted.values and residuals, predictor names
namesx, the call, and echoed control parameters. Non-gaussian
fits additionally include $family, $glm (with deviance,
null.deviance, df.null, df.residual, aic, converged,
iter), and $linear.predictor; $fitted.values is on the
response scale (probabilities for binomial, positive means for
poisson and gamma). Autotune fits carry $autotune and bagged
fits carry $boot.
Details
Two interfaces are provided. The formula method takes a model formula
and a data frame. The default method takes a numeric predictor matrix
x and a numeric response vector y.
Fits are deterministic across thread counts: at a fixed seed.cv,
results are bit-for-bit identical regardless of nthreads.
References
Friedman, J. H. (1991). Multivariate Adaptive Regression Splines. Annals of Statistics 19(1):1-67.
Friedman, J. H. (1993). Fast MARS. Stanford University Department of Statistics Technical Report 110.
Examples
fit <- ares(as.matrix(mtcars[, -1]), mtcars$mpg, nthreads = 2)
print(fit)
#> Call:
#> ares.default(x = as.matrix(mtcars[, -1]), y = mtcars$mpg, nthreads = 2)
#>
#> MARS fit: family = gaussian
#> Selected terms: 6 of 21 forward-pass terms
#> RSS: 91.81
#> GCV: 6.662
#> Degree: 1 Penalty: 2 nthreads: 2
p <- predict(fit, as.matrix(mtcars[, -1]))