Knots for hinge terms by MARS
mars_knots.RdA forward stepwise search over hinge pairs max(0, x - k) and
max(0, k - x) in the manner of R earth with degree = 1: each step adds
the pair on the variable and knot that most reduce the residual sum of
squares (weighted when weights are given), with earth's minimum span
between knots and end span, its stopping rules and its GCV with penalty
penalty. With prune = TRUE a backward pass drops terms while that
lowers the GCV. With family, each step scores candidates by weighted
least squares on the GLM's working response and the deviance takes the
residual sum of squares' place. Default runs on
examples/data/chalk_portfolio.csv match earth's knots, RSS and GCV
(validation/scripts/r_mars_earth.R). The knots enter glm_fit() as
formula terms; mars_formula() writes them.
Usage
mars_knots(
x,
y,
weights = NULL,
max_terms = NULL,
threshold = 0.001,
penalty = 2,
min_span = NULL,
end_span = NULL,
prune = FALSE,
family = NULL,
link = NULL
)
mars_formula(fit)Arguments
- x
A data frame or named list of numeric predictors.
- y
The response.
- weights
Optional prior weights.
- max_terms
The most terms, the intercept included (earth's
nk);NULLfor earth's defaultmin(200, max(20, 2p)) + 1.- threshold
Stop when a step raises the R-squared by less (earth's
thresh).- penalty
The GCV's cost per knot.
- min_span, end_span
Observations between knots and at the ends;
NULLfor earth's defaults from the number of predictors and rows.- prune
Run the backward pass by GCV.
- family, link
A GLM family and link as in
glm_fit()for the deviance criterion;NULLfor least squares.- fit
A
mars_knotslist.
Value
mars_knots() a list with terms (a data frame of variable,
knot and direction, one of "up", "down" or "linear"),
forward_terms (the same before pruning), knots and linear (per
variable), coefficients (the intercept first), rss, gcv, rsq,
grsq, termination and steps (a data frame of terms, rss,
rsq and gcv per forward step); mars_formula() the right-hand
side terms as a character vector, x for a linear term and
I(pmax(0, x - k)) for each knot.
Examples
x <- data.frame(a = 1:40)
y <- pmax(0, x$a - 20) + sin(x$a)
fit <- mars_knots(x, y, min_span = 1, end_span = 1)
fit$knots$a
#> [1] 20
mars_formula(fit)
#> [1] "a" "I(pmax(0, a - 20))"