| gareg_subset | R Documentation |
Runs a GA-based search over variable subsets using a user-specified
objective (default: subsetBIC) and returns a compact
"gareg" S4 result with method = "subset".
The engine can be ga (single population) or
gaisl (islands), selected via gaMethod.
gareg_subset(
y,
X,
ObjFunc = NULL,
gaMethod = "ga",
gacontrol = NULL,
monitoring = FALSE,
seed = NULL,
...
)
y |
Numeric response vector (length |
X |
Numeric matrix of candidate predictors ( |
ObjFunc |
Objective function or its name. Defaults to |
gaMethod |
GA backend to call: |
gacontrol |
Optional named list of GA engine controls (e.g., |
monitoring |
Logical; if |
seed |
Optional RNG seed (convenience alias for |
... |
Additional arguments forwarded to |
The fitness passed to GA is ObjFunc itself. Because the engine expects
a function with signature f(chrom, ...), your ObjFunc must interpret
chrom as a 0/1 mask over the columns of X. The function then computes a score
(e.g., negative BIC) using y, X, and any extra arguments supplied via ....
With the default subsetBIC, the returned value is -BIC, so we set
max = TRUE in the GA call to maximize fitness. If you switch to an objective that
returns a quantity to minimize, either negate it in your objective or change
the engine setting to max = FALSE.
Engine controls belong in gacontrol; objective-specific options belong in ....
This separation prevents accidental name collisions between GA engine parameters and
objective arguments.
An object of S4 class "gareg" (with method = "subset") containing:
call – the matched call.
N – number of observations.
objFunc – the objective function used.
gaMethod – "ga" or "gaisl".
gaFit – the GA fit object returned by GA (if your class allows it).
featureNames – column names of X (or empty).
bestFitness – best fitness value (GA::ga@fitnessValue).
bestChrom – c(m, idx): number of selected variables and their indices.
bestnumbsol – m, number of selected variables.
bestsol – vector of selected column indices in X.
subsetBIC,
ga,
gaisl
if (requireNamespace("GA", quietly = TRUE)) {
set.seed(1)
n <- 100
p <- 12
X <- matrix(rnorm(n * p), n, p)
y <- 1 + X[, 1] - 0.7 * X[, 4] + rnorm(n, sd = 0.5)
# Default: subsetBIC (Gaussian – negative BIC), engine = GA::ga
fit1 <- gareg_subset(y, X,
gaMethod = "ga",
gacontrol = list(popSize = 60, maxiter = 80, run = 40, parallel = FALSE)
)
summary(fit1)
# Island model: GA::gaisl
fit2 <- gareg_subset(y, X,
gaMethod = "gaisl",
gacontrol = list(popSize = 40, maxiter = 60, numIslands = 4, parallel = FALSE)
)
summary(fit2)
# Logistic objective (subsetBIC handles GLM via ...):
ybin <- rbinom(n, 1, plogis(0.3 + X[, 1] - 0.5 * X[, 2]))
fit3 <- gareg_subset(ybin, X,
gaMethod = "ga",
family = stats::binomial(), # <- passed to subsetBIC via ...
gacontrol = list(popSize = 60, maxiter = 80, parallel = FALSE)
)
summary(fit3)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.