| Title: | Riemannian Statistics for Dimensionality Reduction and Visualization |
|---|---|
| Description: | Provides tools for applying statistical methods on Riemannian manifolds using local geometry derived from Uniform Manifold Approximation and Projection (UMAP). The package enables dimensionality reduction, visualization, and analysis of complex data through Riemannian versions of principal component analysis and related multivariate methods. Methods are based on McInnes et al. (2018) <doi:10.21105/joss.00861>. |
| Authors: | Oldemar Rodríguez Rojas [aut, cre], Jennifer Lobo Vásquez [aut] |
| Maintainer: | Oldemar Rodríguez Rojas <[email protected]> |
| License: | BSD_3_clause + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-07-17 17:01:14 UTC |
| Source: | https://github.com/cran/riemannianStats |
Generates a biplot showing individuals on the principal plane and variables as arrows projected onto the same component space.
riem.biplot( data, components, correlations, clusters = NULL, explained.inertia = 0, title = "", var.scale = NULL, point.size = 2, alpha = 1, show.ind.labels = TRUE, show.var.labels = TRUE, ind.label.size = 3, var.label.size = 3, arrow.size = 0.4, var.color = "red", interactive = FALSE )riem.biplot( data, components, correlations, clusters = NULL, explained.inertia = 0, title = "", var.scale = NULL, point.size = 2, alpha = 1, show.ind.labels = TRUE, show.var.labels = TRUE, ind.label.size = 3, var.label.size = 3, arrow.size = 0.4, var.color = "red", interactive = FALSE )
data |
A data frame containing the original data. Row names are used as individual labels and column names are used as variable labels. |
components |
A matrix or data frame containing at least two components. The first two columns are used to represent individuals on the principal plane. |
correlations |
A matrix or data frame containing the correlations between variables and components. The first two columns are used to draw the variable arrows. |
clusters |
Optional vector of cluster labels used to color individuals according to the cluster they belong to. |
explained.inertia |
Numeric percentage of inertia explained by the plotted components. Defaults to '0'. |
title |
Optional character string added above the default title. |
var.scale |
Numeric scaling factor for variable arrows. Defaults to 'NULL', which automatically scales arrows to the individual plane. |
point.size |
Numeric size of the points. Defaults to '2'. |
alpha |
Numeric transparency value between 0 and 1. Defaults to '1'. |
show.ind.labels |
Logical. If 'TRUE', displays individual labels. Defaults to 'TRUE'. |
show.var.labels |
Logical. If 'TRUE', displays variable labels. Defaults to 'TRUE'. |
ind.label.size |
Numeric size of individual labels. Defaults to '3'. |
var.label.size |
Numeric size of variable labels. Defaults to '3'. |
arrow.size |
Numeric line width for variable arrows. Defaults to '0.4'. |
var.color |
Character string indicating the color of variable arrows and variable labels. Defaults to '"red"'. |
interactive |
Logical. If 'TRUE', returns an interactive 'plotly' plot. If 'FALSE', returns a static 'ggplot2' plot. Defaults to 'FALSE'. |
The biplot combines two types of information: the position of the individuals on the first two Riemannian components and the contribution of the variables represented as arrows. This allows the user to explore both the structure of the observations and the relationship between variables and components.
If 'interactive = TRUE', the static 'ggplot2' object is converted into an interactive 'plotly' object. In that case, hovering over the points displays information about the individuals, while hovering over the variable arrows or labels displays information about the corresponding variables.
If 'interactive = FALSE', a 'ggplot' object. If 'interactive = TRUE', a 'plotly' object.
# Static biplot data <- iris[, 1:4] components <- prcomp(data, scale. = TRUE)$x correlations <- cor(data, components[, 1:2]) riem.biplot( data = data, components = components, correlations = correlations, show.ind.labels = FALSE ) # Interactive biplot riem.biplot( data = data, components = components, correlations = correlations, show.ind.labels = FALSE, interactive = TRUE )# Static biplot data <- iris[, 1:4] components <- prcomp(data, scale. = TRUE)$x correlations <- cor(data, components[, 1:2]) riem.biplot( data = data, components = components, correlations = correlations, show.ind.labels = FALSE ) # Interactive biplot riem.biplot( data = data, components = components, correlations = correlations, show.ind.labels = FALSE, interactive = TRUE )
Centers a dataset with respect to the Riemannian mean observation, defined as the row with the smallest total UMAP-based distance to all other rows.
riem.center.data(data, rho, umap.distance.matrix)riem.center.data(data, rho, umap.distance.matrix)
data |
A numeric data frame or matrix. |
rho |
A square numeric Rho matrix. |
umap.distance.matrix |
A square numeric UMAP-based distance matrix. |
A numeric matrix of Riemannian-centered data.
data <- iris[1:5, 1:4] rho <- matrix(1, nrow = 5, ncol = 5) dist.matrix <- as.matrix(dist(data)) centered <- riem.center.data(data, rho, dist.matrix) head(centered)data <- iris[1:5, 1:4] rho <- matrix(1, nrow = 5, ncol = 5) dist.matrix <- as.matrix(dist(data)) centered <- riem.center.data(data, rho, dist.matrix) head(centered)
Calculates the Riemannian correlation matrix from a numeric data frame or matrix, a Rho matrix, and a UMAP-based distance matrix.
riem.cor(data, rho, umap.distance.matrix)riem.cor(data, rho, umap.distance.matrix)
data |
A numeric data frame or matrix where rows are observations and columns are variables. |
rho |
A numeric Rho matrix. |
umap.distance.matrix |
A numeric UMAP-based distance matrix. |
A numeric Riemannian correlation matrix.
data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data = data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix )data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data = data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix )
Calculates a covariance matrix using Riemannian-centered differences with respect to the Riemannian mean observation.
riem.cov(data, rho, umap.distance.matrix, unbiased = FALSE)riem.cov(data, rho, umap.distance.matrix, unbiased = FALSE)
data |
A numeric data frame or matrix. |
rho |
A square numeric Rho matrix. |
umap.distance.matrix |
A square numeric UMAP-based distance matrix. |
unbiased |
Logical. If 'TRUE', divides by 'n - 1'; otherwise divides by 'n'. Defaults to 'FALSE'. |
A numeric Riemannian covariance matrix.
data <- iris[1:10, 1:4] similarities <- diag(nrow(data)) rho <- riem.rho(similarities) diffs <- riem.diff(data, rho) dist.matrix <- riem.dist(diffs) riem.cov(data, rho, dist.matrix)data <- iris[1:10, 1:4] similarities <- diag(nrow(data)) rho <- riem.rho(similarities) diffs <- riem.diff(data, rho) dist.matrix <- riem.dist(diffs) riem.cov(data, rho, dist.matrix)
Calculates weighted pairwise vector differences between all rows of a numeric dataset using the Rho matrix.
riem.diff(data, rho)riem.diff(data, rho)
data |
A numeric data frame or matrix where rows are observations and columns are variables. |
rho |
A square numeric matrix with the same number of rows as 'data'. |
A three-dimensional numeric array. Entry '[i, j, ]' contains 'rho[i, j] * (data[i, ] - data[j, ])'.
data <- iris[1:5, 1:4] rho <- matrix(1, nrow = 5, ncol = 5) diffs <- riem.diff(data, rho) dim(diffs)data <- iris[1:5, 1:4] rho <- matrix(1, nrow = 5, ncol = 5) diffs <- riem.diff(data, rho) dim(diffs)
Calculates a pairwise distance matrix from a tensor of Riemannian vector differences.
riem.dist(riemannian.diff)riem.dist(riemannian.diff)
riemannian.diff |
A three-dimensional array where entry '[i, j, ]' contains the weighted vector difference between observations 'i' and 'j'. |
A square numeric distance matrix.
data <- iris[1:5, 1:4] rho <- matrix(1, nrow = 5, ncol = 5) diffs <- riem.diff(data, rho) riem.dist(diffs)data <- iris[1:5, 1:4] rho <- matrix(1, nrow = 5, ncol = 5) diffs <- riem.diff(data, rho) riem.dist(diffs)
Performs Riemannian principal component analysis from a numeric data frame or matrix, a Riemannian correlation matrix, a Rho matrix, and a UMAP-based distance matrix.
riem.ind.coord( data, correlation.matrix, rho, umap.distance.matrix, sign.convention = c("largest.negative", "largest.positive", "none") )riem.ind.coord( data, correlation.matrix, rho, umap.distance.matrix, sign.convention = c("largest.negative", "largest.positive", "none") )
data |
A numeric data frame or matrix where rows are observations and columns are variables. |
correlation.matrix |
A square numeric correlation matrix. |
rho |
A numeric Rho matrix. |
umap.distance.matrix |
A numeric UMAP-based distance matrix. |
sign.convention |
Character string indicating how to orient the signs of the eigenvectors. Options are '"largest.negative"', '"largest.positive"', and '"none"'. Defaults to '"largest.negative"'. |
The sign of principal components is arbitrary. This function uses a sign convention to make component orientations reproducible.
A numeric matrix of Riemannian principal components.
data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix ) components <- riem.ind.coord( data = data, correlation.matrix = correlation.matrix, rho = rho, umap.distance.matrix = distance.matrix ) head(components)data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix ) components <- riem.ind.coord( data = data, correlation.matrix = correlation.matrix, rho = rho, umap.distance.matrix = distance.matrix ) head(components)
Calculates the proportion of total inertia, or explained variance, associated with two selected principal components from a correlation matrix.
riem.inertia(correlation.matrix, component1, component2)riem.inertia(correlation.matrix, component1, component2)
correlation.matrix |
A square numeric correlation matrix. |
component1 |
Integer index of the first selected component. |
component2 |
Integer index of the second selected component. |
In R, component indices start at 1. Therefore, the first principal component is 'component1 = 1', not 'component1 = 0'.
A numeric value between 0 and 1.
data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix ) components <- riem.ind.coord( data = data, correlation.matrix = correlation.matrix, rho = rho, umap.distance.matrix = distance.matrix ) riem.inertia(correlation.matrix, component1 = 1, component2 = 2)data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix ) components <- riem.ind.coord( data = data, correlation.matrix = correlation.matrix, rho = rho, umap.distance.matrix = distance.matrix ) riem.inertia(correlation.matrix, component1 = 1, component2 = 2)
Returns the index of the observation with the smallest total distance to all other observations.
riem.mean.index(umap.distance.matrix)riem.mean.index(umap.distance.matrix)
umap.distance.matrix |
A square numeric distance matrix. |
An integer index. In R, indexing starts at 1.
d <- as.matrix(dist(iris[1:5, 1:4])) riem.mean.index(d)d <- as.matrix(dist(iris[1:5, 1:4])) riem.mean.index(d)
Calculates the Euclidean norm of a vector or the Euclidean distance between two vectors.
riem.norm(x, y = NULL)riem.norm(x, y = NULL)
x |
Numeric vector. |
y |
Optional numeric vector. If supplied, the norm of 'x - y' is computed. |
A single numeric value.
riem.norm(c(3, 4)) riem.norm(c(1, 2), c(4, 6))riem.norm(c(3, 4)) riem.norm(c(1, 2), c(4, 6))
Plots either the principal plane for individuals or the correlation circle for variables.
riem.plot( data, choix = c("ind", "var"), components = NULL, correlations = NULL, clusters = NULL, explained.inertia = 0, title = "", ... )riem.plot( data, choix = c("ind", "var"), components = NULL, correlations = NULL, clusters = NULL, explained.inertia = 0, title = "", ... )
data |
A numeric data frame or matrix. |
choix |
Character. Either '"ind"' for the principal plane or '"var"' for the correlation circle. |
components |
A matrix or data frame containing the coordinates of the individuals on the components. Required when 'choix = "ind"'. |
correlations |
A matrix or data frame containing the correlations between variables and components. Required when 'choix = "var"'. |
clusters |
Optional vector of cluster labels used to color individuals according to the cluster they belong to. Used only when 'choix = "ind"'. |
explained.inertia |
Numeric percentage of inertia explained by the plotted components. Defaults to '0'. |
title |
Optional character string added above the default title. |
... |
Additional arguments passed to the selected plotting function. |
A 'ggplot' object.
data <- iris[, 1:4] components <- prcomp(data, scale. = TRUE)$x correlations <- cor(data, components[, 1:2]) riem.plot( data = data, choix = "ind", components = components ) riem.plot( data = data, choix = "var", correlations = correlations )data <- iris[, 1:4] components <- prcomp(data, scale. = TRUE)$x correlations <- cor(data, components[, 1:2]) riem.plot( data = data, choix = "ind", components = components ) riem.plot( data = data, choix = "var", correlations = correlations )
Creates an interactive three-dimensional scatter plot from selected columns of a data frame. Points can optionally be colored according to a cluster column.
riem.plot.3d( data, x.col, y.col, z.col, cluster.col = NULL, label.col = NULL, title = "", explained.inertia = 0, point.size = 5, alpha = 1 )riem.plot.3d( data, x.col, y.col, z.col, cluster.col = NULL, label.col = NULL, title = "", explained.inertia = 0, point.size = 5, alpha = 1 )
data |
A data frame containing the variables to plot. |
x.col |
Character string with the name of the x-axis column. |
y.col |
Character string with the name of the y-axis column. |
z.col |
Character string with the name of the z-axis column. |
cluster.col |
Optional character string with the name of the cluster column. If provided, points are colored according to the cluster they belong to. Defaults to 'NULL'. |
label.col |
Optional character string with the name of the column used as individual labels. If 'NULL', row names are used. Defaults to 'NULL'. |
title |
Optional character string added above the default title. |
explained.inertia |
Numeric percentage of inertia explained by the plotted components. Defaults to '0'. |
point.size |
Numeric size of the markers. Defaults to '5'. |
alpha |
Numeric transparency value between 0 and 1. Defaults to '1'. |
This function requires the optional package 'plotly'.
A 'plotly' htmlwidget.
riem.plot.3d( data = iris, x.col = "Sepal.Length", y.col = "Sepal.Width", z.col = "Petal.Length" ) riem.plot.3d( data = iris, x.col = "Sepal.Length", y.col = "Sepal.Width", z.col = "Petal.Length", cluster.col = "Species" )riem.plot.3d( data = iris, x.col = "Sepal.Length", y.col = "Sepal.Width", z.col = "Petal.Length" ) riem.plot.3d( data = iris, x.col = "Sepal.Length", y.col = "Sepal.Width", z.col = "Petal.Length", cluster.col = "Species" )
Calculates the Rho matrix as one minus the UMAP similarity matrix.
riem.rho(umap.similarities)riem.rho(umap.similarities)
umap.similarities |
A numeric matrix of UMAP graph similarities. |
The Rho matrix is used to weight pairwise vector differences in the Riemannian analysis pipeline.
A numeric matrix computed as '1 - umap.similarities'.
similarities <- diag(3) riem.rho(similarities)similarities <- diag(3) riem.rho(similarities)
Calculates a dense matrix of UMAP graph similarities from the local neighborhood graph of a numeric dataset.
riem.similarities.umap( data, n.neighbors = 3, min.dist = 0.1, metric = "euclidean", sparse = FALSE )riem.similarities.umap( data, n.neighbors = 3, min.dist = 0.1, metric = "euclidean", sparse = FALSE )
data |
A numeric data frame or matrix where rows are observations and columns are variables. |
n.neighbors |
Integer. Number of nearest neighbors used to construct the local graph. Defaults to '3'. |
min.dist |
Numeric. Kept for API compatibility with UMAP workflows. The similarity graph itself is driven by local neighborhoods and the distance metric. Defaults to '0.1'. |
metric |
Character string. Distance metric used by UMAP. Defaults to '"euclidean"'. |
sparse |
Logical. If 'TRUE', returns the sparse graph returned by 'uwot'; if 'FALSE', returns a dense matrix. Defaults to 'FALSE'. |
This function is the R equivalent of extracting 'reducer.graph_' from 'umap-learn' in Python and converting it to a dense matrix.
A numeric matrix or sparse matrix of UMAP similarities.
similarities <- riem.similarities.umap( iris[, 1:4], n.neighbors = 5 ) dim(similarities)similarities <- riem.similarities.umap( iris[, 1:4], n.neighbors = 5 ) dim(similarities)
Calculates Riemannian correlations between the original variables and the first two principal components.
riem.var.coord(data, components, rho, umap.distance.matrix)riem.var.coord(data, components, rho, umap.distance.matrix)
data |
A numeric data frame or matrix where rows are observations and columns are variables. |
components |
A numeric matrix or data frame with at least two columns. |
rho |
A numeric Rho matrix. |
umap.distance.matrix |
A numeric UMAP-based distance matrix. |
A data frame with columns 'Component.1' and 'Component.2'.
data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix ) components <- riem.ind.coord( data = data, correlation.matrix = correlation.matrix, rho = rho, umap.distance.matrix = distance.matrix ) correlations <- riem.var.coord( data = data, components = components, rho = rho, umap.distance.matrix = distance.matrix )data <- iris[, 1:4] similarities <- riem.similarities.umap(data, n.neighbors = 5) rho <- riem.rho(similarities) riemannian.diff <- riem.diff(data, rho = rho) distance.matrix <- riem.dist(riemannian.diff) correlation.matrix <- riem.cor( data = data, rho = rho, umap.distance.matrix = distance.matrix ) components <- riem.ind.coord( data = data, correlation.matrix = correlation.matrix, rho = rho, umap.distance.matrix = distance.matrix ) correlations <- riem.var.coord( data = data, components = components, rho = rho, umap.distance.matrix = distance.matrix )