graph_weights: Convert a Data Matrix to an Adjacency Graph

View source: R/knn_weights.R

graph_weightsR Documentation

Convert a Data Matrix to an Adjacency Graph

Description

Convert a data matrix with n instances and p features to an n-by-n adjacency graph using specified neighbor and weight modes.

Usage

graph_weights(
  X,
  k = 5,
  neighbor_mode = c("knn", "epsilon"),
  weight_mode = c("heat", "normalized", "binary", "euclidean", "cosine", "correlation"),
  type = c("normal", "mutual", "asym"),
  sigma = NULL,
  eps = NULL,
  labels = NULL,
  ...
)

Arguments

X

A data matrix where each row represents an instance and each column represents a variable. Similarity is computed over instances.

k

An integer representing the number of neighbors (ignored when neighbor_mode is not 'epsilon').

neighbor_mode

A character string specifying the method for assigning weights to neighbors, either "supervised", "knn", "knearest_misses", or "epsilon".

weight_mode

A character string specifying the weight mode: binary (1 if neighbor, 0 otherwise), 'heat', 'normalized', 'euclidean', 'cosine', or 'correlation'.

type

A character string specifying the nearest neighbor policy, one of: normal, mutual, asym.

sigma

A numeric parameter for the heat kernel (exp(-dist/(2*sigma^2))).

eps

A numeric value representing the neighborhood radius when neighbor_mode is 'epsilon' (not implemented).

labels

A factor vector representing the class of the categories when weight_mode is 'supervised' with nrow(labels) equal to nrow(X).

...

Additional parameters passed to the internal functions.

Details

This function converts a data matrix with n instances and p features into an adjacency graph. The adjacency graph is created based on the specified neighbor and weight modes. The neighbor mode determines how neighbors are assigned weights, and the weight mode defines the method used to compute weights.

Value

An adjacency graph based on the specified neighbor and weight modes.

Examples

# Sample data
X <- matrix(rnorm(100*100), 100, 100)
sm <- graph_weights(X, neighbor_mode="knn", weight_mode="normalized", k=3)

labels <- factor(rep(letters[1:4], 5))
sm3 <- graph_weights(X, neighbor_mode="knn", k=3, labels=labels, weight_mode="cosine")
sm4 <- graph_weights(X, neighbor_mode="knn", k=100, labels=labels, weight_mode="cosine")

bbuchsbaum/graphweights documentation built on April 4, 2024, 7:19 p.m.