| Title: | B-cell Hybrid Immune Variant Engine |
|---|---|
| Description: | The bHIVE package implements an Artificial Immune Network (AI-Net) algorithm for clustering and classification tasks. Inspired by biological immune systems, it employs clonal selection, mutation, and network suppression to analyze and model datasets. This package provides flexible functionality, including affinity metrics, mutation strategies, and hyperparameter tuning. |
| Authors: | Nick Borcherding [aut, cre] |
| Maintainer: | Nick Borcherding <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.99.4 |
| Built: | 2026-06-09 19:17:49 UTC |
| Source: | https://github.com/BiocStaging/bHIVE |
Two-signal activation gate implementing the immunological principle that immune cell activation requires both antigen-specific recognition (Signal 1) AND costimulatory context (Signal 2).
Prevents spurious activation on isolated outliers. An antibody is only allowed to clone if both signals exceed their thresholds. This is biologically-principled regularization.
Signal 2 options:
"density": Local data density around the antibody
"danger": User-provided danger signal vector
"entropy": Local label entropy (classification only)
signal2_typeType of costimulatory signal.
threshold1Minimum affinity for Signal 1 (antigen recognition).
threshold2Minimum costimulatory signal for Signal 2.
danger_signalsUser-provided danger signal vector (for "danger" type).
new()
Create a new ActivationGate.
ActivationGate$new( signal2_type = "density", threshold1 = 0.1, threshold2 = 0.3, danger_signals = NULL )
signal2_typeCharacter. "density", "danger", or "entropy".
threshold1Numeric. Minimum affinity threshold.
threshold2Numeric. Minimum Signal 2 threshold.
danger_signalsNumeric vector. Per-data-point danger scores.
evaluate()
Evaluate which antibody-data interactions pass the two-signal gate.
ActivationGate$evaluate(affinity_matrix, X, A, y = NULL, task = "clustering")
affinity_matrixNumeric matrix (n x m) of affinities.
XNumeric matrix of data (n x d).
ANumeric matrix of antibodies (m x d).
yTarget vector or NULL.
taskCharacter. Task type.
Logical matrix (n x m) where TRUE means the interaction is activated.
print()
Print summary.
ActivationGate$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
ActivationGate$clone(deep = FALSE)
deepWhether to make a deep clone.
# Two-signal activation gate data(iris) X <- as.matrix(iris[, 1:4]) A <- X[sample(150, 10), ] rep <- ImmuneRepertoire$new(A) gate <- ActivationGate$new(signal2_type = "density", threshold2 = 0.3) aff <- rep$affinity_matrix(X, "gaussian") activated <- gate$evaluate(aff, X, A) sum(activated) # number of activated interactions# Two-signal activation gate data(iris) X <- as.matrix(iris[, 1:4]) A <- X[sample(150, 10), ] rep <- ImmuneRepertoire$new(A) gate <- ActivationGate$new(signal2_type = "density", threshold2 = 0.3) aff <- rep$affinity_matrix(X, "gaussian") activated <- gate$evaluate(aff, X, A) sum(activated) # number of activated interactions
R6 implementation of the Artificial Immune Network algorithm. This is the core bHIVE algorithm using C++ backends for performance-critical operations. Supports composable modules for somatic hypermutation, idiotypic network regulation, germinal center selection, and more.
bHIVE::ImmuneAlgorithm -> AINet
new()
Create a new AINet algorithm instance.
AINet$new( nAntibodies = 20, beta = 5, epsilon = 0.01, maxIter = 50, k = 3, affinityFunc = "gaussian", distFunc = "euclidean", affinityParams = list(alpha = 1, c = 1, p = 2, Sigma = NULL), mutationDecay = 1, mutationMin = 0.01, maxClones = Inf, stopTolerance = 0, noImprovementLimit = Inf, initMethod = "sample", shm = NULL, init = NULL, activation = NULL, idiotypic = NULL, germinalCenter = NULL, microenvironment = NULL, memory = NULL, classSwitcher = NULL, verbose = TRUE )
nAntibodiesInteger. Initial antibody population size.
betaNumeric. Clone multiplier.
epsilonNumeric. Suppression distance threshold.
maxIterInteger. Maximum iterations.
kInteger. Top-k antibodies to clone per data point.
affinityFuncCharacter. Affinity function name.
distFuncCharacter. Distance function name.
affinityParamsList. Parameters for affinity/distance functions.
mutationDecayNumeric. Per-iteration mutation rate decay.
mutationMinNumeric. Minimum mutation rate.
maxClonesNumeric. Maximum clones per antibody.
stopToleranceNumeric. Early stopping tolerance.
noImprovementLimitInteger. Early stopping patience.
initMethodCharacter. Initialization method.
shmAn SHMEngine instance or NULL for default uniform mutation.
initA VDJLibrary instance or NULL for default initialization.
activationAn ActivationGate instance or NULL.
idiotypicAn IdiotypicNetwork instance or NULL.
germinalCenterA GerminalCenter instance or NULL.
microenvironmentA Microenvironment instance or NULL.
memoryA MemoryPool instance or NULL.
classSwitcherA ClassSwitcher instance or NULL.
verboseLogical. Print progress.
fit()
Fit the AINet algorithm to data.
AINet$fit(X, y = NULL, task = NULL, ...)
XNumeric matrix or data frame (n x d).
yOptional factor target for classification.
taskCharacter: "clustering" or "classification". Inferred from y if NULL.
...Additional arguments (currently unused).
Invisible self, with result populated.
clone()
The objects of this class are cloneable with this method.
AINet$clone(deep = FALSE)
deepWhether to make a deep clone.
# Clustering with Iris data data(iris) X <- as.matrix(iris[, 1:4]) model <- AINet$new(nAntibodies = 15, maxIter = 10, verbose = FALSE) model$fit(X, task = "clustering") table(model$result$assignments) # Classification model2 <- AINet$new(nAntibodies = 20, maxIter = 10, verbose = FALSE) model2$fit(X, iris$Species, task = "classification") mean(model2$result$assignments == as.character(iris$Species)) # Predict on new data preds <- model2$predict(X[1:10, ])# Clustering with Iris data data(iris) X <- as.matrix(iris[, 1:4]) model <- AINet$new(nAntibodies = 15, maxIter = 10, verbose = FALSE) model$fit(X, task = "clustering") table(model$result$assignments) # Classification model2 <- AINet$new(nAntibodies = 20, maxIter = 10, verbose = FALSE) model2$fit(X, iris$Species, task = "classification") mean(model2$result$assignments == as.character(iris$Species)) # Predict on new data preds <- model2$predict(X[1:10, ])
Implements an artificial immune network algorithm for clustering and classification tasks. The algorithm evolves a population of "antibodies" via clonal selection and mutation, applies network suppression to maintain diversity, and assigns data points based on affinity or distance metrics.
bHIVE( X, y = NULL, task = NULL, nAntibodies = 20, beta = 5, epsilon = 0.01, maxIter = 50, affinityFunc = "gaussian", distFunc = "euclidean", affinityParams = list(alpha = 1, c = 1, p = 2, Sigma = NULL), mutationDecay = 1, mutationMin = 0.01, maxClones = Inf, stopTolerance = 0, noImprovementLimit = Inf, initMethod = c("sample", "random", "random_uniform", "kmeans++"), k = 3, verbose = TRUE )bHIVE( X, y = NULL, task = NULL, nAntibodies = 20, beta = 5, epsilon = 0.01, maxIter = 50, affinityFunc = "gaussian", distFunc = "euclidean", affinityParams = list(alpha = 1, c = 1, p = 2, Sigma = NULL), mutationDecay = 1, mutationMin = 0.01, maxClones = Inf, stopTolerance = 0, noImprovementLimit = Inf, initMethod = c("sample", "random", "random_uniform", "kmeans++"), k = 3, verbose = TRUE )
X |
A numeric matrix or data frame of input features, with rows as observations and columns as features. |
y |
Optional. A factor target vector for classification. If NULL, clustering will be performed. |
task |
Character. Specifies the task to perform: |
nAntibodies |
Integer. The initial population size of antibodies. |
beta |
Numeric. Clone multiplier (controls how many clones are generated for top-matching antibodies). |
epsilon |
Numeric. Similarity threshold used in network suppression;
antibodies closer than |
maxIter |
Integer. Maximum number of iterations to run the AI-Net algorithm. |
affinityFunc |
Character. Specifies the affinity (similarity) function
to use for antibody-data matching. One of |
distFunc |
Character. Specifies the distance function for clustering
and suppression. One of |
affinityParams |
A list of optional parameters for the chosen affinity or distance function.
|
mutationDecay |
Numeric. Factor by which the mutation rate decays each
iteration (should be |
mutationMin |
Numeric. Minimum mutation rate, preventing the mutation scale from shrinking to zero. |
maxClones |
Numeric. Maximum number of clones per top-matching antibody;
defaults to |
stopTolerance |
Numeric. If the change in the number of antibodies
(repertoire size) is |
noImprovementLimit |
Integer. Stops the algorithm early if there is no
further improvement in antibody count (beyond |
initMethod |
Character. Method for initializing antibodies. Can be:
|
k |
Integer. Number of top-matching antibodies (by affinity) to consider cloning for each data point. |
verbose |
Logical. If |
A list:
antibodies: Final antibody vectors (nAntibodies x nFeatures).
assignments:
- For clustering: integer cluster IDs in [1..#Antibodies].
- For classification: predicted labels.
task: The chosen task.
# Example 1: Clustering with the Iris dataset data(iris) X <- as.matrix(iris[, 1:4]) # Numeric features only res <- bHIVE(X = X, task = "clustering", nAntibodies = 30, beta = 5, epsilon = 0.01, maxIter = 20, k = 3, verbose = FALSE) table(res$assignments) # Example 2: Classification with Iris species y <- iris$Species res <- bHIVE(X = X, y = y, task = "classification", nAntibodies = 30, beta = 5, epsilon = 0.01, maxIter = 20, k = 3, verbose = FALSE) table(res$assignments, y)# Example 1: Clustering with the Iris dataset data(iris) X <- as.matrix(iris[, 1:4]) # Numeric features only res <- bHIVE(X = X, task = "clustering", nAntibodies = 30, beta = 5, epsilon = 0.01, maxIter = 20, k = 3, verbose = FALSE) table(res$assignments) # Example 2: Classification with Iris species y <- iris$Species res <- bHIVE(X = X, y = y, task = "classification", nAntibodies = 30, beta = 5, epsilon = 0.01, maxIter = 20, k = 3, verbose = FALSE) table(res$assignments, y)
A wrapper for integrating the B-cell Hybrid Immune Variant Engine
(bHIVE) algorithm with the caret package. Supports classification
tasks, providing compatibility with caret::train()
for model training and validation.
bHIVEmodelbHIVEmodel
A list containing the components required for integration with
the caret package.
The bHIVEmodel wrapper facilitates the use of bHIVE for classification.
It defines the model label, parameter grid, fitting function,
and prediction methods to conform to the caret model specification.
labelCharacter string. Identifies the model as "B-cell-based Hybrid Immune Virtual Evolution".
libraryCharacter string. Specifies the R package containing the bHIVE implementation. Default is "customPackage".
typeCharacter vector. Specifies the supported tasks: "Classification".
parametersA data.frame describing the tunable
parameters:
parameter: Name of the parameter.
class: Data type of the parameter ("numeric").
label: Short description of the parameter.
gridFunction. Generates a grid of tuning parameters for hyperparameter optimization.
fitFunction. Trains the bHIVE model using specified hyperparameters and task type.
predictFunction. Generates predictions for new data (classification labels).
probFunction. Calculates class probabilities for classification tasks.
nAntibodiesNumber of initial antibodies in the bHIVE algorithm.
betaClone multiplier. Controls the number of clones generated for top-matching antibodies.
epsilonSimilarity threshold for antibody suppression. Smaller values encourage more diversity in the repertoire.
grid(x, y, len): Generates a grid of tuning parameters.
Accepts:
x: Feature matrix or data frame.
y: Factor target vector for classification.
len: Number of grid points for each parameter.
fit(x, y, wts, param, lev, last, classProbs, ...): Trains
the bHIVE model. Key arguments:
x: Feature matrix or data frame.
y: Target vector.
param: List of hyperparameters (nAntibodies,
beta, epsilon).
...: Additional arguments passed to the bHIVE function.
predict(modelFit, newdata, submodels): Generates predictions
for new data.
modelFit: Trained bHIVE model.
newdata: New feature data for prediction.
prob(modelFit, newdata, submodels): Calculates class
probabilities (classification only).
library(caret)
# Simulated classification dataset
set.seed(123)
X <- matrix(rnorm(100 * 5), ncol = 5)
y <- factor(sample(c("Class1", "Class2"), 100, replace = TRUE))
# Train bHIVE model using caret
trainControl <- trainControl(method = "cv", number = 5, classProbs = TRUE)
tunedModel <- train(
x = X,
y = y,
method = bHIVEmodel,
trControl = trainControl,
tuneLength = 3
)
# Predictions
predictions <- predict(tunedModel, newdata = X)
probabilities <- predict(tunedModel, newdata = X, type = "prob")
# View model structure bHIVEmodel$label bHIVEmodel$parameters# View model structure bHIVEmodel$label bHIVEmodel$parameters
Implements antibody isotype/class switching, allowing antibodies to change their matching breadth. Inspired by real B cell class switching from IgM (broad, pentameric) to IgG (specific, monomeric) to IgA (mucosal).
In bHIVE, class switching modifies the effective affinity kernel width:
IgM: Broad matching (large kernel width) – good for initial
exploration and capturing general patterns
IgG: Specific matching (small kernel width) – good for
fine-grained discrimination after patterns are identified
IgA: Boundary patrol (medium kernel width) – good for
maintaining coverage at decision boundaries
alpha_IgMKernel width for IgM mode (broad).
alpha_IgGKernel width for IgG mode (specific).
alpha_IgAKernel width for IgA mode (boundary).
new()
Create a new ClassSwitcher.
ClassSwitcher$new(alpha_IgM = 0.1, alpha_IgG = 5, alpha_IgA = 1)
alpha_IgMNumeric. Kernel width for broad matching.
alpha_IgGNumeric. Kernel width for specific matching.
alpha_IgANumeric. Kernel width for boundary matching.
switch_isotypes()
Determine appropriate isotype for each antibody based on its microenvironment zone.
ClassSwitcher$switch_isotypes(repertoire, zones)
repertoireAn ImmuneRepertoire.
zonesCharacter vector from Microenvironment assessment.
Named numeric vector of alpha values per antibody.
get_alpha()
Get alpha value for a given isotype.
ClassSwitcher$get_alpha(isotype)
isotypeCharacter. "IgM", "IgG", or "IgA".
Numeric.
print()
Print summary.
ClassSwitcher$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
ClassSwitcher$clone(deep = FALSE)
deepWhether to make a deep clone.
# Switch antibody isotypes based on microenvironment zones A <- matrix(rnorm(50), nrow = 10, ncol = 5) rep <- ImmuneRepertoire$new(A) cs <- ClassSwitcher$new(alpha_IgM = 0.1, alpha_IgG = 5.0) zones <- sample(c("stable", "explore", "boundary"), 10, replace = TRUE) alphas <- cs$switch_isotypes(rep, zones) table(rep$metadata$isotype) # IgM, IgG, IgA distribution# Switch antibody isotypes based on microenvironment zones A <- matrix(rnorm(50), nrow = 10, ncol = 5) rep <- ImmuneRepertoire$new(A) cs <- ClassSwitcher$new(alpha_IgM = 0.1, alpha_IgG = 5.0) zones <- sample(c("stable", "explore", "boundary"), 10, replace = TRUE) alphas <- cs$switch_isotypes(rep, zones) table(rep$metadata$isotype) # IgM, IgG, IgA distribution
Identifies "public antibodies" shared across independent repertoires, implementing the concept of convergent selection from TCR/BCR immunology as a biologically-motivated ensemble method.
In real immunity, certain immune receptor sequences appear across multiple individuals (public clones), suggesting they are driven by common selection pressures. Similarly, antibodies that appear across multiple independent bHIVE runs represent the most robust patterns in the data.
toleranceDistance tolerance for matching antibodies across repertoires.
min_appearancesMinimum number of repertoires an antibody must appear in to be considered "public".
public_antibodiesThe identified public antibodies.
new()
Create a new ConvergentSelector.
ConvergentSelector$new(tolerance = 0.5, min_appearances = 2)
toleranceNumeric. Maximum distance for two antibodies to be considered the same across repertoires.
min_appearancesInteger. Minimum repertoires for an antibody to be public.
find_public()
Find public antibodies shared across multiple repertoires.
ConvergentSelector$find_public(repertoires, distFunc = "euclidean")
repertoiresList of ImmuneRepertoire objects or list of antibody matrices.
distFuncCharacter. Distance function for matching.
Numeric matrix of public (consensus) antibodies.
from_results()
Run convergent selection from multiple bHIVE results.
ConvergentSelector$from_results(results, distFunc = "euclidean")
resultsList of bHIVE result objects (each with $antibodies).
distFuncCharacter. Distance function.
Numeric matrix of consensus antibodies.
print()
Print summary.
ConvergentSelector$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
ConvergentSelector$clone(deep = FALSE)
deepWhether to make a deep clone.
# Find public antibodies across multiple runs data(iris) X <- as.matrix(iris[, 1:4]) results <- lapply(1:3, function(i) { m <- AINet$new(nAntibodies = 15, maxIter = 5, verbose = FALSE) m$fit(X, task = "clustering") m$result }) conv <- ConvergentSelector$new(tolerance = 1.0, min_appearances = 2) public <- conv$from_results(results) nrow(public) # consensus antibodies# Find public antibodies across multiple runs data(iris) X <- as.matrix(iris[, 1:4]) results <- lapply(1:3, function(i) { m <- AINet$new(nAntibodies = 15, maxIter = 5, verbose = FALSE) m$fit(X, task = "clustering") m$result }) conv <- ConvergentSelector$new(tolerance = 1.0, min_appearances = 2) public <- conv$from_results(results) nrow(public) # consensus antibodies
Models T follicular helper (Tfh) cell selection pressure on B cells within a germinal center reaction. Implements resource competition where antibodies compete for Tfh help, and only helped antibodies survive.
The germinal center is where B cells undergo affinity maturation through iterative cycles of mutation and selection. Tfh cells act as quality-control selectors:
Each Tfh evaluates B cell (antibody) quality using a task-aware metric
B cells compete for Tfh help (resource competition)
Only helped B cells survive to the next round
Selection pressure controls the stringency of the process
nTfhNumber of Tfh selectors (determines how many antibodies survive).
selectionPressureNumeric [0,1]. 0 = no selection (all survive), 1 = only the very best survive.
roundsNumber of selection rounds per call.
last_survivorsInteger vector of survivor indices (relative
to the input repertoire) from the most recent call to select().
new()
Create a new GerminalCenter.
GerminalCenter$new(nTfh = 10, selectionPressure = 0.5, rounds = 1)
nTfhInteger. Number of Tfh helper cells. Each helps one B cell.
selectionPressureNumeric [0,1]. Stringency of selection.
roundsInteger. Number of competition rounds.
select()
Run germinal center selection on a repertoire.
GerminalCenter$select( repertoire, X, y = NULL, task = "clustering", affinityFunc = "gaussian", affinityParams = list(alpha = 1, c = 1, p = 2) )
repertoireAn ImmuneRepertoire object.
XNumeric matrix of training data.
yFactor target vector or NULL for clustering.
taskCharacter: "clustering" or "classification".
affinityFuncCharacter. Affinity function for evaluation.
affinityParamsList. Parameters for affinity function.
Integer vector of survivor indices relative to the input
repertoire (composed across all selection rounds). Also stored on
self$last_survivors for inspection. Repertoire is modified
in place.
print()
Print summary.
GerminalCenter$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
GerminalCenter$clone(deep = FALSE)
deepWhether to make a deep clone.
# Germinal center selection on Iris data(iris) X <- as.matrix(iris[, 1:4]) gc <- GerminalCenter$new(nTfh = 5, selectionPressure = 0.5) rep <- ImmuneRepertoire$new(X[sample(150, 20), ]) gc$select(rep, X, iris$Species, "classification") rep$size() # fewer antibodies after selection# Germinal center selection on Iris data(iris) X <- as.matrix(iris[, 1:4]) gc <- GerminalCenter$new(nTfh = 5, selectionPressure = 0.5) rep <- ImmuneRepertoire$new(X[sample(150, 20), ]) gc$select(rep, X, iris$Species, "classification") rep$size() # fewer antibodies after selection
The honeycombHIVE function implements a multilayer artificial immune
system that iteratively refines a set of prototypes - referred to as
antibodies - to model the structure of the input data. In each layer, the
function first uses the bHIVE algorithm to generate or update
antibodies based on the current data representation and task (clustering or
classification). Optionally, it applies gradient-based
fine-tuning (via refineB) to these antibodies, allowing for
advanced refinement through various optimizers (e.g., SGD, Adam, RMSProp) and
customizable loss functions. The final output is a hierarchical set of layers
that encapsulate both the refined prototypes and the corresponding cluster
assignments or predictions for the original observations, making
honeycombHIVE a versatile tool for adaptive learning and pattern
recognition.
honeycombHIVE( X, y = NULL, task = c("clustering", "classification"), layers = 3, nAntibodies = 20, minAntibodies = 5, epsilon = 0.05, beta = 5, maxIter = 10, collapseMethod = c("centroid", "medoid", "median", "mode"), minClusterSize = NULL, distance = "euclidean", verbose = TRUE, refine = FALSE, refineLoss = "categorical_crossentropy", refineSteps = 5, refineLR = 0.01, refinePushAway = TRUE, refineOptimizer = "sgd", refineMomentumCoef = 0.9, refineBeta1 = 0.9, refineBeta2 = 0.999, refineRmspropDecay = 0.9, refineEpsilon = 1e-08, ... )honeycombHIVE( X, y = NULL, task = c("clustering", "classification"), layers = 3, nAntibodies = 20, minAntibodies = 5, epsilon = 0.05, beta = 5, maxIter = 10, collapseMethod = c("centroid", "medoid", "median", "mode"), minClusterSize = NULL, distance = "euclidean", verbose = TRUE, refine = FALSE, refineLoss = "categorical_crossentropy", refineSteps = 5, refineLR = 0.01, refinePushAway = TRUE, refineOptimizer = "sgd", refineMomentumCoef = 0.9, refineBeta1 = 0.9, refineBeta2 = 0.999, refineRmspropDecay = 0.9, refineEpsilon = 1e-08, ... )
X |
A numeric matrix or data frame of input features (rows = observations, columns = features). |
y |
Optional target factor vector for classification. |
task |
Character, one of "clustering" or "classification". |
layers |
Integer, how many layers (AIS iterations) to run. |
nAntibodies |
Integer, how many antibodies (prototypes) to generate initially in each layer. |
minAntibodies |
Integer, minimal number of antibodies to keep in each layer. |
epsilon |
Numeric, threshold param for |
beta |
Numeric, selection pressure param for |
maxIter |
Integer, maximum iterations for |
collapseMethod |
One of "centroid","medoid","median","mode". |
minClusterSize |
Minimum cluster size. Smaller clusters can be merged/discarded if not NULL. |
distance |
Distance metric for medoid calculation, e.g. "euclidean". |
verbose |
Logical, if TRUE prints progress at each layer. |
refine |
Logical, if TRUE apply gradient-based refinement via
|
refineLoss |
Character specifying the loss for |
refineSteps |
Integer, number of gradient steps in |
refineLR |
Numeric, learning rate for gradient updates. |
refinePushAway |
Logical, if TRUE and classification, push prototypes away from differently labeled points. |
refineOptimizer |
Character, one of |
refineMomentumCoef |
Numeric, momentum coefficient (if using momentum). |
refineBeta1 |
Numeric, first moment decay rate (if using Adam). |
refineBeta2 |
Numeric, second moment decay rate (if using Adam). |
refineRmspropDecay |
Numeric, decay rate for the moving average of squared gradients (if using RMSProp). |
refineEpsilon |
Numeric, a small constant for numerical stability (used in adaptive optimizers). |
... |
Additional arguments passed to |
A list of length layers. Each element (layer) includes:
antibodies: The prototypes in that layer.
assignments: Antibody index (in that layer) for each row
of current_X.
membership: For each original row in X,
which cluster/antibody it belongs to in this layer.
predictions: If classification, predicted label for each
original row in X.
task: The specified task.
# Clustering data(iris) X_iris <- iris[, 1:4] resC <- honeycombHIVE( X = X_iris, task = "clustering", layers = 3, nAntibodies = 15, beta = 5, maxIter = 10 )# Clustering data(iris) X_iris <- iris[, 1:4] resC <- honeycombHIVE( X = X_iris, task = "clustering", layers = 3, nAntibodies = 15, beta = 5, maxIter = 10 )
A caret wrapper for the honeycombHIVE function,
enabling seamless integration with the caret package for
hyperparameter tuning, cross-validation, and performance evaluation.
honeycombHIVEmodelhoneycombHIVEmodel
An object of class list of length 8.
A caret model definition list. Pass it to
train for model training and evaluation.
nAntibodies: Number of initial antibodies in the network.
beta: Clone multiplier controlling the number of clones per
antibody.
epsilon: Threshold for network suppression to remove
redundant antibodies.
layers: Number of hierarchical layers for iterative
refinement.
refineOptimizer: Optimizer for gradient-based refinement
(e.g. "sgd", "momentum", "adagrad", "adam", "rmsprop").
refineSteps: Number of gradient update steps in refinement.
refineLR: Learning rate for refinement.
"Classification": Assigns class labels to input observations.
"Clustering": Groups data points based on similarity
(though typically caret is used for supervised tasks).
## Not run: library(caret) # Example: Classification with Iris data(iris) X <- as.matrix(iris[, 1:4]) y <- iris$Species train_control <- trainControl(method = "cv", number = 5) set.seed(42) model <- train( x = X, y = y, method = honeycombHIVEmodel, trControl = train_control, tuneGrid = expand.grid( nAntibodies = c(10, 20), beta = c(3, 5), epsilon = c(0.01, 0.05), layers = c(1, 2), refineOptimizer = "adam", refineSteps = 5, refineLR = 0.01 ) ) print(model) ## End(Not run)## Not run: library(caret) # Example: Classification with Iris data(iris) X <- as.matrix(iris[, 1:4]) y <- iris$Species train_control <- trainControl(method = "cv", number = 5) set.seed(42) model <- train( x = X, y = y, method = honeycombHIVEmodel, trControl = train_control, tuneGrid = expand.grid( nAntibodies = c(10, 20), beta = c(3, 5), epsilon = c(0.01, 0.05), layers = c(1, 2), refineOptimizer = "adam", refineSteps = 5, refineLR = 0.01 ) ) print(model) ## End(Not run)
Implements Jerne's idiotypic network theory for antibody repertoire regulation. Replaces crude epsilon-threshold suppression with principled network dynamics based on Varela & Coutinho's (1991) second- generation immune network model.
The idiotypic network models antibody-antibody interactions where each antibody's variable region can be recognized by other antibodies. This creates a regulatory network with emergent properties:
- A bell-shaped (double-threshold) activation function: too little stimulation leads to cell death, moderate stimulation to activation, and excessive stimulation to suppression. - Population dynamics with source, decay, activation, and suppression terms. - Self-organized repertoire structure with memory and tolerance properties.
This is the single most novel contribution of the overhauled bHIVE package. No existing AIS implementation uses idiotypic network dynamics for repertoire regulation.
theta_lowLower activation threshold. Below this, cells die.
theta_highUpper activation threshold. Above this, cells are suppressed.
source_rateRate of new cell generation (basal production).
decay_rateNatural cell death rate.
dtTime step for Euler integration.
timeStepsNumber of simulation time steps.
survival_thresholdMinimum population level to survive.
last_dynamicsResult from the last regulation step.
new()
Create a new IdiotypicNetwork regulator.
IdiotypicNetwork$new( theta_low = 0.01, theta_high = 0.5, source_rate = 0.5, decay_rate = 0.1, dt = 0.1, timeSteps = 20, survival_threshold = 0.5 )
theta_lowLower activation threshold.
theta_highUpper activation threshold.
source_rateBasal cell production rate.
decay_rateNatural decay rate.
dtEuler integration time step.
timeStepsNumber of dynamics simulation steps.
survival_thresholdMinimum population to survive.
regulate()
Run idiotypic network dynamics on an antibody repertoire.
IdiotypicNetwork$regulate( repertoire, affinityFunc = "gaussian", affinityParams = list(alpha = 1, c = 1, p = 2) )
repertoireAn ImmuneRepertoire object.
affinityFuncCharacter. Affinity function for Ab-Ab interactions.
affinityParamsList. Parameters for the affinity function.
Invisible self. The repertoire is modified in place (dead
antibodies removed). Access $last_dynamics for full results.
get_network()
Get the Ab-Ab affinity matrix from the last regulation.
IdiotypicNetwork$get_network()
Numeric matrix (m x m) or NULL if not yet run.
get_population()
Get population levels from the last regulation.
IdiotypicNetwork$get_population()
Numeric vector or NULL if not yet run.
print()
Print summary.
IdiotypicNetwork$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
IdiotypicNetwork$clone(deep = FALSE)
deepWhether to make a deep clone.
# Create and run idiotypic regulation idi <- IdiotypicNetwork$new(theta_low = 0.01, theta_high = 0.5) A <- matrix(rnorm(50), nrow = 10, ncol = 5) rep <- ImmuneRepertoire$new(A) idi$regulate(rep, "gaussian", list(alpha = 0.5)) print(idi)# Create and run idiotypic regulation idi <- IdiotypicNetwork$new(theta_low = 0.01, theta_high = 0.5) A <- matrix(rnorm(50), nrow = 10, ncol = 5) rep <- ImmuneRepertoire$new(A) idi$regulate(rep, "gaussian", list(alpha = 0.5)) print(idi)
Abstract R6 base class for all immune-inspired algorithms.
Subclasses must implement the fit method.
repertoireAn ImmuneRepertoire object.
configNamed list of algorithm hyperparameters.
modulesNamed list of injected module instances (SHMEngine, IdiotypicNetwork, GerminalCenter, etc.).
historyList of per-iteration metrics.
resultThe result from the last call to fit().
new()
Create a new ImmuneAlgorithm.
ImmuneAlgorithm$new(config = list(), modules = list())
configNamed list of hyperparameters.
modulesNamed list of module instances.
fit()
Fit the algorithm to data. Must be overridden by subclasses.
ImmuneAlgorithm$fit(X, y = NULL, task = NULL, ...)
XNumeric matrix (n x d).
yOptional factor target for classification.
taskCharacter: "clustering" or "classification".
...Additional arguments.
The algorithm object (invisibly), with result populated.
predict()
Predict on new data using the trained repertoire.
ImmuneAlgorithm$predict(newdata)
newdataNumeric matrix (n_new x d).
Predictions (class labels, numeric values, or cluster IDs).
print()
Print summary of the algorithm.
ImmuneAlgorithm$print(...)
...Not used.
summary()
Get a summary of the fitting history.
ImmuneAlgorithm$summary()
Data frame of per-iteration metrics.
clone()
The objects of this class are cloneable with this method.
ImmuneAlgorithm$clone(deep = FALSE)
deepWhether to make a deep clone.
# ImmuneAlgorithm is abstract; use AINet for concrete instances algo <- ImmuneAlgorithm$new() print(algo)# ImmuneAlgorithm is abstract; use AINet for concrete instances algo <- ImmuneAlgorithm$new() print(algo)
R6 class representing a collection of antibodies (immune cells) with associated metadata. Core data structure for bHIVE algorithms.
An ImmuneRepertoire holds a matrix of antibody vectors (each row is one antibody in feature space) plus per-antibody metadata (isotype, state, age, lineage). All heavy computation is dispatched to C++ via RcppArmadillo.
cellsNumeric matrix (nAntibodies x nFeatures).
metadataData frame with per-antibody attributes.
new()
Create a new ImmuneRepertoire.
ImmuneRepertoire$new(cells, metadata = NULL)
cellsNumeric matrix (nAntibodies x nFeatures).
metadataOptional data frame with columns: isotype, state, age, lineage.
affinity_matrix()
Compute affinity matrix between data X and antibodies.
ImmuneRepertoire$affinity_matrix( X, method = "gaussian", params = list(alpha = 1, c = 1, p = 2) )
XNumeric matrix (n x d) of data points.
methodAffinity function: "gaussian", "laplace", "polynomial", "cosine", "hamming".
paramsList with alpha, c, p parameters.
Numeric matrix (n x m) of affinity values.
distance_matrix()
Compute distance matrix between data X and antibodies.
ImmuneRepertoire$distance_matrix( X, method = "euclidean", params = list(p = 2, Sigma = NULL) )
XNumeric matrix (n x d).
methodDistance function: "euclidean", "manhattan", "minkowski", "cosine", "mahalanobis", "hamming".
paramsList with p, Sigma parameters.
Numeric matrix (n x m) of distances.
suppress()
Network suppression: remove redundant antibodies.
ImmuneRepertoire$suppress( epsilon, method = "euclidean", params = list(p = 2, Sigma = NULL) )
epsilonDistance threshold for suppression.
methodDistance function for suppression.
paramsList with p, Sigma parameters.
Invisible self (modified in place).
size()
Get number of antibodies.
ImmuneRepertoire$size()
Integer.
n_features()
Get number of features.
ImmuneRepertoire$n_features()
Integer.
subset()
Subset the repertoire.
ImmuneRepertoire$subset(idx)
idxInteger vector of row indices to keep.
Invisible self (modified in place).
add()
Add antibodies to the repertoire.
ImmuneRepertoire$add(new_cells, new_metadata = NULL)
new_cellsNumeric matrix (k x d) of new antibodies.
new_metadataOptional data frame of metadata for new antibodies.
Invisible self (modified in place).
age_all()
Increment age of all antibodies.
ImmuneRepertoire$age_all()
Invisible self (modified in place).
as_matrix()
Convert to plain matrix.
ImmuneRepertoire$as_matrix()
Numeric matrix (nAntibodies x nFeatures).
print()
Print summary.
ImmuneRepertoire$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
ImmuneRepertoire$clone(deep = FALSE)
deepWhether to make a deep clone.
# Create a repertoire from random antibodies A <- matrix(rnorm(50), nrow = 10, ncol = 5) rep <- ImmuneRepertoire$new(A) print(rep) # Compute affinity to data X <- matrix(rnorm(100), nrow = 20, ncol = 5) aff <- rep$affinity_matrix(X, "gaussian", list(alpha = 1)) dim(aff) # 20 x 10 # Network suppression rep$suppress(epsilon = 1.5, method = "euclidean") rep$size() # fewer antibodies after suppression# Create a repertoire from random antibodies A <- matrix(rnorm(50), nrow = 10, ncol = 5) rep <- ImmuneRepertoire$new(A) print(rep) # Compute affinity to data X <- matrix(rnorm(100), nrow = 20, ncol = 5) aff <- rep$affinity_matrix(X, "gaussian", list(alpha = 1)) dim(aff) # 20 x 10 # Network suppression rep$suppress(epsilon = 1.5, method = "euclidean") rep$size() # fewer antibodies after suppression
Manages long-lived memory cells that can be recalled when distribution shifts are detected. Implements the immunological distinction between short-lived effector cells and long-lived memory cells.
memory_cellsNumeric matrix of archived memory antibodies.
memory_metadataData frame of metadata for memory cells.
archive_thresholdAffinity threshold for archiving (only high-quality antibodies become memory).
max_memoryMaximum number of memory cells to store.
recall_thresholdThreshold for triggering memory recall.
new()
Create a new MemoryPool.
MemoryPool$new( archive_threshold = 0.5, max_memory = 100, recall_threshold = 0.3 )
archive_thresholdNumeric. Minimum average affinity to archive.
max_memoryInteger. Maximum memory cells.
recall_thresholdNumeric. Minimum affinity to recall a memory.
archive()
Archive high-performing antibodies as memory cells.
MemoryPool$archive( repertoire, X, affinityFunc = "gaussian", affinityParams = list(alpha = 1, c = 1, p = 2) )
repertoireAn ImmuneRepertoire.
XTraining data matrix.
affinityFuncCharacter. Affinity function.
affinityParamsList. Affinity parameters.
Integer. Number of new memory cells archived.
recall()
Recall memory cells relevant to current data.
MemoryPool$recall( X, affinityFunc = "gaussian", affinityParams = list(alpha = 1, c = 1, p = 2) )
XData matrix to match against memory.
affinityFuncCharacter. Affinity function.
affinityParamsList. Affinity parameters.
Numeric matrix of recalled memory cells (may be empty).
size()
Get current memory pool size.
MemoryPool$size()
Integer.
print()
Print summary.
MemoryPool$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
MemoryPool$clone(deep = FALSE)
deepWhether to make a deep clone.
# Archive and recall memory cells data(iris) X <- as.matrix(iris[, 1:4]) A <- X[sample(150, 10), ] mp <- MemoryPool$new(archive_threshold = 0.01) rep <- ImmuneRepertoire$new(A) mp$archive(rep, X) mp$size() # number of archived memories recalled <- mp$recall(X[1:5, ]) nrow(recalled) # memories relevant to query# Archive and recall memory cells data(iris) X <- as.matrix(iris[, 1:4]) A <- X[sample(150, 10), ] mp <- MemoryPool$new(archive_threshold = 0.01) rep <- ImmuneRepertoire$new(A) mp$archive(rep, X) mp$size() # number of archived memories recalled <- mp$recall(X[1:5, ]) nrow(recalled) # memories relevant to query
Models local microenvironment cues that influence antibody behavior based on the density and structure of nearby data points.
In real immunity, B cell fate is strongly influenced by local signals: chemokines, cytokines, and interactions with stromal cells in specific tissue microenvironments. This module translates that concept into density-dependent adaptation:
High density zones: Promote memory formation (stabilize antibodies, reduce mutation rate)
Low density zones: Promote exploration (increase mutation rate for broader search)
Boundary zones: Trigger class switching (change matching breadth between IgM-like broad and IgG-like specific modes)
Chemokine-like gradients: Bias mutation direction toward higher-density regions
density_bandwidthBandwidth for kernel density estimation.
high_density_thresholdDensity percentile above which antibodies stabilize.
low_density_thresholdDensity percentile below which antibodies explore.
stabilization_factorMutation rate multiplier for high-density zones.
exploration_factorMutation rate multiplier for low-density zones.
last_densitiesPer-antibody local density from last assessment.
last_zonesPer-antibody zone classification from last assessment.
new()
Create a new Microenvironment module.
Microenvironment$new( density_bandwidth = NULL, high_density_threshold = 0.75, low_density_threshold = 0.25, stabilization_factor = 0.3, exploration_factor = 2 )
density_bandwidthNumeric. KDE bandwidth (NULL for auto).
high_density_thresholdNumeric [0,1]. Percentile threshold for stabilization.
low_density_thresholdNumeric [0,1]. Percentile threshold for exploration.
stabilization_factorNumeric. Mutation rate multiplier for stable zones.
exploration_factorNumeric. Mutation rate multiplier for exploration zones.
assess()
Assess the microenvironment around each antibody.
Microenvironment$assess( repertoire, X, affinityFunc = "gaussian", affinityParams = list(alpha = 1, c = 1, p = 2) )
repertoireAn ImmuneRepertoire object.
XNumeric matrix of training data (n x d).
affinityFuncCharacter. Affinity function.
affinityParamsList. Affinity parameters.
Named list with densities, zones, and mutation_modifiers per antibody.
print()
Print summary.
Microenvironment$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
Microenvironment$clone(deep = FALSE)
deepWhether to make a deep clone.
# Assess microenvironment around antibodies data(iris) X <- as.matrix(iris[, 1:4]) me <- Microenvironment$new() rep <- ImmuneRepertoire$new(X[sample(150, 15), ]) env <- me$assess(rep, X) table(env$zones) # stable, explore, boundary env$mutation_modifiers # per-antibody rate scaling# Assess microenvironment around antibodies data(iris) X <- as.matrix(iris[, 1:4]) me <- Microenvironment$new() rep <- ImmuneRepertoire$new(X[sample(150, 15), ]) env <- me$assess(rep, X) table(env$zones) # stable, explore, boundary env$mutation_modifiers # per-antibody rate scaling
After running bHIVE (or within honeycombHIVE), you have a set of
final antibody positions (A) in feature space. This function refines those
prototypes by iterating over the data points assigned to each antibody and
applying gradient-based updates using a user-chosen loss function and optimizer.
refineB( A, X, y = NULL, assignments, task = c("clustering", "classification"), loss = c("categorical_crossentropy", "binary_crossentropy", "kullback_leibler", "cosine", "mae"), steps = 5, lr = 0.01, push_away = TRUE, verbose = TRUE, optimizer = c("sgd", "momentum", "adagrad", "adam", "rmsprop"), momentum_coef = 0.9, beta1 = 0.9, beta2 = 0.999, rmsprop_decay = 0.9, epsilon = 1e-08 )refineB( A, X, y = NULL, assignments, task = c("clustering", "classification"), loss = c("categorical_crossentropy", "binary_crossentropy", "kullback_leibler", "cosine", "mae"), steps = 5, lr = 0.01, push_away = TRUE, verbose = TRUE, optimizer = c("sgd", "momentum", "adagrad", "adam", "rmsprop"), momentum_coef = 0.9, beta1 = 0.9, beta2 = 0.999, rmsprop_decay = 0.9, epsilon = 1e-08 )
A |
Numeric matrix (nAb x d) of antibody/prototype positions. |
X |
Matrix or data frame (nSamples x d) of feature data. |
y |
Optional. Factor (classification) or NULL (clustering). |
assignments |
Integer or character vector (length = nSamples), specifying the antibody index (or label) each sample belongs to. |
task |
One of |
loss |
One of |
steps |
Integer. How many gradient steps to run. |
lr |
Numeric. Learning rate for each update. |
push_away |
Logical (for classification). Whether to push prototypes away from differently-labeled samples. |
verbose |
Logical. If |
optimizer |
One of |
momentum_coef |
Numeric. Momentum coefficient (used if |
beta1 |
Numeric. Exponential decay rate for the first moment estimates (used in Adam). |
beta2 |
Numeric. Exponential decay rate for the second moment estimates (used in Adam). |
rmsprop_decay |
Numeric. Decay factor for the squared gradient moving average (used in RMSProp). |
epsilon |
Numeric. A small constant for numerical stability. |
The user must provide:
- A numeric matrix A of antibody/prototype positions (nAb x nFeatures).
- A numeric matrix/data frame X of data (nSamples x nFeatures).
- Optional y for classification. If task="clustering",
y can be NULL or ignored.
- An integer or character vector assignments (length=nSamples) giving the
antibody index (or label) to which each data point is assigned.
## Available Losses
### Classification (factor y) - **"categorical_crossentropy"**: Pull prototypes toward data points if they share the antibody's dominant label; push away otherwise. - **"binary_crossentropy"**: Similar to categorical CE, but we interpret factor y as binary (two classes). Pull for same label, push for different label. - **"kullback_leibler"**: Very rough approach that treats "dominant label vs. others" as p and q distributions, pushing/pulling prototypes. - **"cosine"**: Interpreted as trying to maximize cosine similarity for same-label points and minimize for different-label points. - **"mae"**: Sign-based pull/push.
## Available Optimizers - **"sgd"**: Basic stochastic gradient descent. - **"momentum"**: SGD with momentum. - **"adagrad"**: Adaptive gradient descent. - **"adam"**: Adaptive moment estimation. - **"rmsprop"**: Root Mean Square Propagation.
This function performs gradient-based updates on each antibody using the selected loss function.
Depending on the chosen optimizer, it may use plain SGD, momentum-based updates, Adagrad,
Adam, or RMSProp.
Updated matrix A of shape (nAb x d).
data(iris) X <- as.matrix(iris[, 1:4]) y <- iris$Species res <- bHIVE(X, y, task = "classification", nAntibodies = 10, maxIter = 5, verbose = FALSE) assignments <- as.integer(factor(res$assignments, levels = unique(res$assignments))) A_refined <- refineB(res$antibodies, X, y = y, assignments = assignments, task = "classification", loss = "categorical_crossentropy", optimizer = "adam", steps = 3, lr = 0.01, verbose = FALSE) dim(A_refined)data(iris) X <- as.matrix(iris[, 1:4]) y <- iris$Species res <- bHIVE(X, y, task = "classification", nAntibodies = 10, maxIter = 5, verbose = FALSE) assignments <- as.integer(factor(res$assignments, levels = unique(res$assignments))) A_refined <- refineB(res$antibodies, X, y = y, assignments = assignments, task = "classification", loss = "categorical_crossentropy", optimizer = "adam", steps = 3, lr = 0.01, verbose = FALSE) dim(A_refined)
Somatic Hypermutation Engine implementing five biologically- inspired mutation strategies for the bHIVE artificial immune system.
The five strategies are:
Classic random Gaussian noise. Mutation rate = (1-affinity) * decay^(iter-1). This is the original bHIVE behavior.
AIRS-style affinity-proportional mutation. Rate = c * exp(-affinity / T). From Watkins & Timmis (AIRS2), achieving 50% better data reduction than uniform.
Feature-importance-weighted mutation. Features with higher gradient magnitude mutate more, analogous to AID targeting WRCY motifs in real SHM.
Energy-budget-constrained mutation. Total mutation magnitude bounded by E = E_0 * (1-affinity)^2, inspired by Kleinstein's E_SHM ~ N_Mut^2 model.
Per-feature adaptive mutation rate with moment tracking, directly implementing SHM as the Adam optimizer.
methodCharacter. One of "uniform", "airs", "hotspot", "energy", "adaptive".
paramsNamed list of method-specific parameters.
m1_stateFirst moment state matrix (for adaptive method).
m2_stateSecond moment state matrix (for adaptive method).
new()
Create a new SHMEngine.
SHMEngine$new( method = "uniform", decay = 1, mutationMin = 0.01, c_rate = 1, temperature = 0.5, E_0 = 1, base_rate = 0.1, beta1 = 0.9, beta2 = 0.999, adam_epsilon = 1e-08 )
methodCharacter. Mutation strategy.
decayNumeric. Per-iteration mutation rate decay (uniform method).
mutationMinNumeric. Minimum mutation rate floor.
c_rateNumeric. Scaling constant (airs method).
temperatureNumeric. Temperature parameter (airs method).
E_0Numeric. Energy budget base (energy method).
base_rateNumeric. Base mutation rate (hotspot, adaptive methods).
beta1Numeric. First moment decay (adaptive method, like Adam).
beta2Numeric. Second moment decay (adaptive method, like Adam).
adam_epsilonNumeric. Numerical stability (adaptive method).
init_state()
Initialize internal state for adaptive method.
SHMEngine$init_state(nAntibodies, nFeatures)
nAntibodiesInteger. Number of antibodies.
nFeaturesInteger. Number of features.
reset_state()
Reset moment states (e.g., after suppression changes antibody count).
SHMEngine$reset_state(nAntibodies, nFeatures, kept_idx = NULL)
nAntibodiesInteger. New number of antibodies.
nFeaturesInteger. Number of features.
kept_idxInteger vector. Indices of antibodies that were kept.
print()
Print summary.
SHMEngine$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
SHMEngine$clone(deep = FALSE)
deepWhether to make a deep clone.
# Create different SHM engines shm_uniform <- SHMEngine$new(method = "uniform") shm_adaptive <- SHMEngine$new(method = "adaptive", base_rate = 0.1) shm_airs <- SHMEngine$new(method = "airs", temperature = 0.3) print(shm_adaptive)# Create different SHM engines shm_uniform <- SHMEngine$new(method = "uniform") shm_adaptive <- SHMEngine$new(method = "adaptive", base_rate = 0.1) shm_airs <- SHMEngine$new(method = "airs", temperature = 0.3) print(shm_adaptive)
Performs hyperparameter tuning for the bHIVE algorithm over a grid of hyperparameter values or an externally provided data frame of parameter combinations. Evaluates each combination using different metrics:
swarmbHIVE( X, y = NULL, task = c("clustering", "classification"), grid, metric = NULL, maxIter = 50, BPPARAM = SerialParam(), verbose = TRUE )swarmbHIVE( X, y = NULL, task = c("clustering", "classification"), grid, metric = NULL, maxIter = 50, BPPARAM = SerialParam(), verbose = TRUE )
X |
A numeric matrix or data frame of input features (rows = observations, columns = features). |
y |
Optional. A factor target vector for classification.
If |
task |
Character. One of |
grid |
A data frame specifying the hyperparameter combinations.
Should have columns: |
metric |
Character. Name of the evaluation metric. Options:
|
maxIter |
Integer. Maximum iterations for each |
BPPARAM |
Character. A BiocParallel::bpparam() object that can be used
for parallelization. The function supports |
verbose |
Logical. If |
- **Classification**: "accuracy", "balanced_accuracy", "f1", "kappa" - **Clustering**: "silhouette", "davies_bouldin", or "calinski_harabasz"
**Note**: Some metrics require additional packages or assumptions (e.g., multi-class classification for "f1" is calculated as a macro-average).
A list:
best_params: A list (row) of the best hyperparameters.
results: A data frame with the full grid search results,
including the metric_value for each combination.
data(iris) X <- as.matrix(iris[, 1:4]) y <- iris$Species # classification # Define hyperparameter grid grid <- expand.grid( nAntibodies = c(10, 20), beta = c(3, 5), epsilon = c(0.01, 0.05) ) # Perform hyperparameter tuning for classification tuning_results <- swarmbHIVE(X = X, y = y, task = "classification", grid = grid, metric = "balanced_accuracy", maxIter = 10) # For clustering with silhouette set.seed(42) X_clust <- matrix(rnorm(100 * 5), ncol = 5) grid_clust <- expand.grid(nAntibodies = c(5, 10), beta = c(3, 5), epsilon = c(0.01, 0.05)) res_clust <- swarmbHIVE(X_clust, task = "clustering", grid = grid_clust, metric = "silhouette") res_clust$best_paramsdata(iris) X <- as.matrix(iris[, 1:4]) y <- iris$Species # classification # Define hyperparameter grid grid <- expand.grid( nAntibodies = c(10, 20), beta = c(3, 5), epsilon = c(0.01, 0.05) ) # Perform hyperparameter tuning for classification tuning_results <- swarmbHIVE(X = X, y = y, task = "classification", grid = grid, metric = "balanced_accuracy", maxIter = 10) # For clustering with silhouette set.seed(42) X_clust <- matrix(rnorm(100 * 5), ncol = 5) grid_clust <- expand.grid(nAntibodies = c(5, 10), beta = c(3, 5), epsilon = c(0.01, 0.05)) res_clust <- swarmbHIVE(X_clust, task = "clustering", grid = grid_clust, metric = "silhouette") res_clust$best_params
V(D)J gene library initialization for antibody populations. Instead of random sampling, antibodies are generated by combinatorial assembly of gene segments, mimicking the V(D)J recombination that generates antibody diversity in real immune systems.
The feature space is decomposed into V, D, and J segments (subsets of dimensions). Each segment is discretized into "alleles" by clustering. New antibodies are generated by combinatorial sampling of one allele from each segment, producing structured coverage of the data manifold.
Methods:
"pca": PCA components define gene segments
"cluster": k-means within dimension groups creates alleles
"random_partition": Random feature grouping
nVNumber of V gene segments.
nDNumber of D gene segments.
nJNumber of J gene segments.
methodDecomposition method.
libraryThe computed gene library (list of allele matrices).
new()
Create a new VDJLibrary.
VDJLibrary$new(nV = 10, nD = 5, nJ = 4, method = "pca")
nVInteger. Number of V gene alleles.
nDInteger. Number of D gene alleles.
nJInteger. Number of J gene alleles.
methodCharacter. "pca", "cluster", or "random_partition".
build()
Build the gene library from training data.
VDJLibrary$build(X)
XNumeric matrix (n x d).
Invisible self.
generate()
Generate antibodies by combinatorial V(D)J assembly.
VDJLibrary$generate(nAntibodies, X)
nAntibodiesInteger. Number of antibodies to generate.
XNumeric matrix. Training data (used to build library if needed).
Numeric matrix (nAntibodies x d).
print()
Print summary.
VDJLibrary$print(...)
...Not used.
clone()
The objects of this class are cloneable with this method.
VDJLibrary$clone(deep = FALSE)
deepWhether to make a deep clone.
# Generate antibodies via V(D)J combinatorial assembly data(iris) X <- as.matrix(iris[, 1:4]) vdj <- VDJLibrary$new(nV = 5, nD = 3, nJ = 3, method = "pca") A <- vdj$generate(20, X) dim(A) # 20 x 4 print(vdj)# Generate antibodies via V(D)J combinatorial assembly data(iris) X <- as.matrix(iris[, 1:4]) vdj <- VDJLibrary$new(nV = 5, nD = 3, nJ = 3, method = "pca") A <- vdj$generate(20, X) dim(A) # 20 x 4 print(vdj)
This function produces publication-quality visualizations for the results
generated by the bHIVE or honeycombHIVE functions. Users can
specify one or more layers to visualize and choose from several plot types:
visualizeHIVE( result, X = NULL, plot_type = c("scatter", "boxplot", "violin", "density"), feature = NULL, transform = TRUE, transformation_method = c("PCA", "UMAP", "tSNE", "none"), title = "HIVE Results", layer = 1, task = c("clustering", "classification"), ... )visualizeHIVE( result, X = NULL, plot_type = c("scatter", "boxplot", "violin", "density"), feature = NULL, transform = TRUE, transformation_method = c("PCA", "UMAP", "tSNE", "none"), title = "HIVE Results", layer = 1, task = c("clustering", "classification"), ... )
result |
A list object produced by |
X |
Optional. A numeric matrix or data frame of the original input features. If provided, data points will be plotted along with the prototypes. |
plot_type |
Character string specifying the type of plot to generate. Options are:
|
feature |
Optional. For |
transform |
Logical. If |
transformation_method |
Character. The method used for dimensionality
reduction. Options are |
title |
Character. Title for the plot. |
layer |
Integer vector indicating which layer(s) of the result to visualize.
For bHIVE outputs, default is |
task |
Character. The prediction task for the result: one of
|
... |
Additional arguments passed to PCA, tSNE, UMAP, or ggplot functions. |
"scatter": A scatterplot of data points and prototypes.
When X has more than two columns and transform is
TRUE, a dimensionality reduction method is applied.
"boxplot": A boxplot of a selected feature by group with
prototype values overlaid.
"violin": A violin plot of a selected feature by group
with prototype values overlaid.
"density": Density plots of a selected feature by group
with prototype markers.
For scatterplots the transformation can be selected from "PCA",
"UMAP", "tSNE", or "none". When multiple layers are
visualized, the prototypes and the corresponding grouping information from
each layer are combined and faceted by layer.
A ggplot object representing the visualization.
data(iris) X <- as.matrix(iris[, 1:4]) # Run honeycombHIVE for clustering res <- honeycombHIVE(X = X, task = "clustering", epsilon = 0.05, layers = 3, nAntibodies = 30, beta = 5, maxIter = 10, verbose = FALSE) # Visualize layer 2 as a scatterplot (using membership from layer 2). visualizeHIVE(result = res, X = iris[, 1:4], plot_type = "scatter", title = "Layer 2: Scatterplot", layer = 2, task = "clustering") # For classification: assume res[[1]]$predictions holds class labels. visualizeHIVE(result = res, X = iris[, 1:4], plot_type = "violin", feature = "Sepal.Width", title = "Sepal Width by Group (Layer 1)", layer = 1, task = "classification")data(iris) X <- as.matrix(iris[, 1:4]) # Run honeycombHIVE for clustering res <- honeycombHIVE(X = X, task = "clustering", epsilon = 0.05, layers = 3, nAntibodies = 30, beta = 5, maxIter = 10, verbose = FALSE) # Visualize layer 2 as a scatterplot (using membership from layer 2). visualizeHIVE(result = res, X = iris[, 1:4], plot_type = "scatter", title = "Layer 2: Scatterplot", layer = 2, task = "clustering") # For classification: assume res[[1]]$predictions holds class labels. visualizeHIVE(result = res, X = iris[, 1:4], plot_type = "violin", feature = "Sepal.Width", title = "Sepal Width by Group (Layer 1)", layer = 1, task = "classification")