fixknotsIC: Information criterion for a fixed–knot spline regression

View source: R/gareg_knots.R

fixknotsICR Documentation

Information criterion for a fixed–knot spline regression

Description

Computes an information criterion (BIC, AIC, or AICc) for a regression of y on a spline basis of x when the number of interior knots is fixed. This is designed to be used as a fitness/objective function inside a GA search where the chromosome encodes the indices of the interior knots.

Usage

fixknotsIC(
  knot_bin,
  plen = 0,
  y,
  x,
  x_unique,
  x_base = NULL,
  fixedknots,
  degree = 3L,
  type = c("ppolys", "ns", "bs"),
  intercept = TRUE,
  ic_method = "BIC"
)

Arguments

knot_bin

Integer vector (chromosome). Gene 1 stores m, the number of interior knots. Genes 2:(1+m) are indices into x_unique selecting the m interior knots, followed by a sentinel equal to length(x_unique)+1. Only genes strictly before the first occurrence of length(x_unique)+1 are treated as interior indices; genes after the sentinel are ignored. Interior indices must be in 2:(length(x_unique)-1), finite, and non-duplicated.

plen

Unused placeholder kept for API compatibility with other objective functions. Ignored.

y

Numeric response vector of length n.

x

Numeric predictor (same length as y) on which the spline basis is built.

x_unique

Optional numeric vector of unique candidate knot locations. If NULL or missing, it defaults to sort(unique(x)). Must contain at least m + 2 values (interior + two boundaries).

x_base

Optional matrix (or vector) of additional covariates to include linearly alongside the spline basis. If supplied, it is coerced to a matrix and column-bound to the design.

fixedknots

Integer m: the number of interior knots to use. Internally this determines how many indices are read from knot_bin.

degree

Integer polynomial degree for type="ppolys" and type="bs" (default 3L). Ignored for type="ns" (cubic).

type

One of c("ppolys","ns","bs"); forwarded to [splineX()].

intercept

Logical; forwarded to [splineX()]. For m>0, the spline block is splineX(..., intercept=intercept) and no explicit 1-column is added here. If you add your own intercept in X, call splineX(..., intercept=FALSE).

ic_method

Character; which information criterion to return: "BIC", "AIC", or "AICc".

Details

We decode the interior indices up to the sentinel length(x_unique)+1, validate them (finite, interior, non-duplicated), sort the resulting knot locations internally, and build the design as X <- cbind(splineX(..., intercept=intercept), x_base). Invalid chromosomes/inputs return Inf.

Value

A single numeric value: the requested information criterion. Lower is better. Returns Inf for invalid chromosomes/inputs.

See Also

[varyknotsIC()], [splineX()], bs, ns

Examples

library(MASS)
y <- mcycle$accel
x <- mcycle$times
x_unique <- sort(unique(x))
# chromosome encoding 5 interior knot indices with sentinel:
chrom <- c(5, 24, 30, 46, 49, 69, length(x_unique) + 1)
fixknotsIC(chrom,
  y = y, x = x, x_unique = x_unique,
  fixedknots = 5, ic_method = "BIC"
)

GAReg documentation built on March 29, 2026, 5:08 p.m.