## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.align = "center"
)

## ----load-package-------------------------------------------------------------
library(riemannianStats)

## ----locate-data, include=FALSE-----------------------------------------------
data.path <- system.file(
  "extdata",                  # Folder inside the package where the CSV file is stored
  "Data10D_250.csv",         # Name of the CSV file
  package = "riemannianStats" # Package where the file is searched
)

## ----file-path----------------------------------------------------------------
data.path

## ----read-data----------------------------------------------------------------
original.data<- read.csv(
  data.path, # It must be replaced with the path to the CSV file.
  sep = ",",
  dec = "."
)

original.data$cluster<- as.factor(original.data$cluster)

str(original.data)

## ----prepare-data-------------------------------------------------------------
clusters <- original.data$cluster

data.analysis <- original.data[, setdiff(names(original.data), "cluster"), drop = FALSE]

data.analysis.scaled <- scale(data.analysis)

data.analysis.scaled <- as.data.frame(data.analysis.scaled)

head(data.analysis.scaled)

## ----choose-neighbors---------------------------------------------------------
expected.groups <- 5

n.neighbors <- as.integer(nrow(data.analysis) / expected.groups)
n.neighbors

## ----calculate-similarities---------------------------------------------------
umap.similarities <- riem.similarities.umap(
  data = data.analysis,
  n.neighbors = n.neighbors,
  min.dist = 0.1,
  metric = "euclidean"
)
umap.similarities[1:5, 1:5]

## ----calculate-rho------------------------------------------------------------
rho <- riem.rho(umap.similarities)

rho[1:5, 1:5]

## ----calculate-riemannian-diff------------------------------------------------
riemannian.diff <- riem.diff(
  data = data.analysis,
  rho = rho
)

riemannian.diff[1, 2, ]

## ----calculate-distance-matrix------------------------------------------------
umap.distance.matrix <- riem.dist(riemannian.diff)

umap.distance.matrix[1:5, 1:5]

## ----riemannian-correlation---------------------------------------------------
correlation.matrix <- riem.cor(
  data = data.analysis,
  rho = rho,
  umap.distance.matrix = umap.distance.matrix
)

correlation.matrix[1:5, 1:5]

## ----riemannian-covariance----------------------------------------------------
covariance.matrix <- riem.cov(
  data = data.analysis,
  rho = rho,
  umap.distance.matrix = umap.distance.matrix
)

covariance.matrix[1:5, 1:5]

## ----riemannian-components----------------------------------------------------
components <- riem.ind.coord(
  data = data.analysis,
  correlation.matrix = correlation.matrix,
  rho = rho,
  umap.distance.matrix = umap.distance.matrix
)

components[1:5, 1:5]
dim(components)

## ----explained-inertia--------------------------------------------------------
inertia <- riem.inertia(
  correlation.matrix = correlation.matrix,
  component1 = 1,
  component2 = 2
) * 100

inertia

## ----variable-component-correlations------------------------------------------
correlations <- riem.var.coord(
  data = data.analysis,
  components = components,
  rho = rho,
  umap.distance.matrix = umap.distance.matrix
)

correlations

## ----plot-principal-plane-function, fig.width=7, fig.height=5-----------------
riem.plot(
  data = data.analysis,
  choix = "ind",
  components = components,
  clusters = clusters,
  explained.inertia = inertia,
  show.labels = TRUE
)

## ----plot-principal-plane-function-interactive, fig.width=7, fig.height=5-----
riem.plot(
  data = data.analysis,
  choix = "ind",
  components = components,
  clusters = clusters,
  explained.inertia = inertia,
  show.labels = TRUE,
  title = "Data10D_250",
  interactive = TRUE
)

## ----plot-correlation-circle, fig.width=7, fig.height=7-----------------------
riem.plot(
  data = data.analysis,
  choix = "var",
  correlations = correlations,
  explained.inertia = inertia,
  title = "Data10D_250"
)

## ----biplot, fig.width=7, fig.height=7----------------------------------------
riem.biplot(
  data = data.analysis,
  components = components,
  correlations = correlations,
  clusters = clusters,
  explained.inertia = inertia,
  title = "Data10D_250"
)

## ----biplot-interactive, fig.width=7, fig.height=7----------------------------
riem.biplot(
  data = data.analysis,
  components = components,
  correlations = correlations,
  clusters = clusters,
  show.ind.labels = FALSE,
  show.var.labels = TRUE,
  var.color = "red",
  interactive = TRUE
)

## ----prepare-visualization-data-----------------------------------------------
data.viz <- original.data

data.viz$Riemannian.Component1 <- components[, 1]
data.viz$Riemannian.Component2 <- components[, 2]
data.viz$Riemannian.Component3 <- components[, 3]

head(data.viz)

## ----plot-3d-components, eval=FALSE-------------------------------------------
# riem.plot.3d(
#   data = data.viz,
#   x.col = "Riemannian.Component1",
#   y.col = "Riemannian.Component2",
#   z.col = "Riemannian.Component3",
#   cluster.col = "cluster",
#   title = "Data10D_250 - Riemannian Components",
#   explained.inertia = inertia
# )

