gareg_knots: Genetic-Algorithm–based Optimal Knot Selection

View source: R/gareg_knots.R

gareg_knotsR Documentation

Genetic-Algorithm–based Optimal Knot Selection

Description

Runs a GA-based search for changepoints/knots and returns a compact "gareg" S4 result that stores the backend GA fit ("cptga" or "cptgaisl") plus the essential run settings.

Usage

gareg_knots(
  y,
  x,
  ObjFunc = NULL,
  fixedknots = NULL,
  minDist = 3L,
  degree = 3L,
  type = c("ppolys", "ns", "bs"),
  intercept = TRUE,
  gaMethod = "cptga",
  cptgactrl = NULL,
  monitoring = FALSE,
  seed = NULL,
  ...
)

Arguments

y

Numeric vector of responses (length N).

x

Optional index/time vector aligned with y. If missing, it defaults to seq_along(y). Used to derive x_unique (candidate knot positions) and passed to the objective function; the GA backend itself does not use x directly.

ObjFunc

Objective function or its name. If NULL, a default is chosen:

  • fixknotsIC when fixedknots is supplied;

  • varyknotsIC otherwise.

A custom function must accept the chromosome and needed data via named arguments (see the defaults for a template function).

fixedknots

NULL (varying-knots search) or an integer giving the number of interior knots for a fixed-m search. If non-NULL, the method is "fixknots" and specialized operators are injected unless overridden in cptgactrl.

minDist

Integer minimum distance between adjacent changepoints. If omitted (missing() or NULL), the value in cptgactrl is used. If supplied here, it overrides the control value.

degree

Integer polynomial degree for "ppolys" and "bs". Ignored for "ns" (always cubic). Must be provided for "ppolys" and "bs".

type

One of c("ppolys", "ns", "bs"): piecewise polynomials, natural cubic, or B-spline. See splineX. The first option of 'ppolys' is taken by default.

intercept

Logical; include intercept column where applicable. Default: TRUE.

gaMethod

GA backend to call: function or name. Supports "cptga" (single population) and "cptgaisl" (islands).

cptgactrl

Control list built with cptgaControl() (or a named list of overrides). When gaMethod = "cptgaisl", island-specific knobs like numIslands and maxMig are recognized. Other genetic algorithm parameters can be found in cptga and cptgaisl.

monitoring

Logical; print short progress messages (also forwarded into the backend control).

seed

Optional RNG seed; also stored into the backend control.

...

Additional arguments passed to the GA backend. If the backend does not accept ..., unknown arguments are silently dropped (the call is filtered against the backend formals).

Details

Engine selection and controls. The function detects the engine from gaMethod and constructs a matching control via cptgaControl():

  • "cptga" uses .cptga.default.

  • "cptgaisl" uses .cptgaisl.default (supports numIslands, maxMig, etc.).

  • see other details in cptga and cptgaisl.

Top-level monitoring, seed, and minDist given to gareg_knots() take precedence over the control list.

Fix-knots operators. When fixedknots is provided and the control does not already override them, the following operators are injected: Popinitial_fixknots, crossover_fixknots, mutation_fixknots.

Spline basis options. To build spline design matrices (via splineX):

  • type = "ppolys": Degree-d regression spline via truncated-power piecewise polynomials.

  • type = "ns": Degree-3 natural cubic spline with zero second-derivative at boundaries.

  • type = "bs": Degree-d B-spline basis (unconstrained).

Value

An object of class "gareg" with key slots:

  • call, method ("varyknots" or "fixknots"), N.

  • objFunc, gaMethod, gaFit (class "cptga" or "cptgaisl"), ctrl.

  • fixedknots, minDist, polydegree, type, intercept.

  • bestFitness, bestChrom, bestnumbsol, bestsol.

Use summary(g) to print GA settings and the best solution (extracted from g@gaFit); show(g) prints a compact header.

Argument precedence

Values are combined as control < core < .... That is, cptgactrl provides defaults, then core arguments from gareg_knots() override those, and finally any matching names in ... override both.

See Also

cptgaControl, changepointGA::cptga, changepointGA::cptgaisl, fixknotsIC, varyknotsIC

Examples


set.seed(1)
N <- 120
y <- c(rnorm(40, 0), rnorm(40, 3), rnorm(40, 0))
x <- seq_len(N)

# 1) Varying-knots with single-pop GA
g1 <- gareg_knots(
  y, x,
  minDist = 5,
  gaMethod = "cptga",
  cptgactrl = cptgaControl(popSize = 150, pcrossover = 0.9, maxgen = 500)
)
summary(g1)

# 2) Fixed knots (operators auto-injected unless overridden)
g2 <- gareg_knots(
  y, x,
  fixedknots = 5,
  minDist = 5
)
summary(g2)

# 3) Island GA with island-specific controls
g3 <- gareg_knots(
  y, x,
  gaMethod = "cptgaisl",
  minDist = 6,
  cptgactrl = cptgaControl(
    engine = "cptgaisl",
    numIslands = 8, maxMig = 250,
    popSize = 120, pcrossover = 0.9
  )
)
summary(g3)



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