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

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

## ----create-data--------------------------------------------------------------
students <- data.frame(
  Student = c(
    "Lucia", "Pedro", "Ines", "Luis", "Andres",
    "Ana", "Carlos", "Jose", "Sonia", "Maria"
  ),
  Mathematics = c(7, 7.5, 7.6, 5, 6, 7.8, 6.3, 7.9, 6, 6.8),
  Sciences = c(6.5, 9.4, 9.2, 6.5, 6, 9.6, 6.4, 9.7, 6, 7.2),
  Spanish = c(9.2, 7.3, 8, 6.5, 7.8, 7.7, 8.2, 7.5, 6.5, 8.7),
  History = c(8.6, 7, 8, 7, 8.9, 8, 9, 8, 5.5, 9),
  PhysicalEducation = c(8, 7, 7.5, 9, 7.3, 6.5, 7.2, 6, 8.7, 7)
)

students

## ----prepare-data-------------------------------------------------------------

data.analysis <- students[, c(
  "Mathematics",
  "Sciences",
  "Spanish",
  "History",
  "PhysicalEducation"
)]

student.names <- students$Student

rownames(data.analysis) <- student.names

data.analysis

## ----calculate-similarities---------------------------------------------------
n.neighbors <- 3

umap.similarities <- riem.similarities.umap(
  data = data.analysis,
  n.neighbors = n.neighbors,
  min.dist = 0.1,
  metric = "euclidean"
)

round(umap.similarities, 3)

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

round(rho, 3)

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

dim(riemannian.diff)

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

round(distance.matrix, 3)

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

round(covariance.matrix, 3)

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

round(correlation.matrix, 3)

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

round(components, 3)

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

inertia

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

round(loadings, 3)

## ----principal-plane, fig.width=7, fig.height=5-------------------------------
riem.plot(
  data = data.analysis,
  choix = "ind",
  components = components,
  explained.inertia = inertia,
  title = "Student grades"
)

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

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

