## ----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

## ----fit-riem-pca-------------------------------------------------------------
modelo <- riem.pca(
  data = data.analysis,
  scale.unit = FALSE,
  ncp = 5,
  axes = c(1, 2),
  n.neighbors = 3,
  min.dist = 0.1,
  metric = "euclidean",
  method = "umap"
)

modelo

## ----eigenvalues--------------------------------------------------------------
round(modelo$eig, 4)

## ----explained-inertia-two-axes-----------------------------------------------
inertia <- sum(modelo$eig[c("comp 1", "comp 2"), "percentage of variance"])

inertia

## ----umap-similarities--------------------------------------------------------
umap.similarities <- modelo$ind$similarities

round(umap.similarities, 3)

## ----rho-matrix---------------------------------------------------------------
rho <- modelo$ind$rho

round(rho, 3)

## ----riemannian-differences---------------------------------------------------
riemannian.diff <- modelo$ind$differences

round(riemannian.diff, 3)

## ----riemannian-distances-----------------------------------------------------
distance.matrix <- modelo$ind$distance.matrix

round(distance.matrix, 3)

## ----riemannian-covariance----------------------------------------------------
covariance.matrix <- modelo$var$cov

round(covariance.matrix, 3)

## ----riemannian-correlation---------------------------------------------------
correlation.matrix <- modelo$var$cor

round(correlation.matrix, 3)

## ----individual-coordinates---------------------------------------------------
individual.coordinates <- modelo$ind$coord

round(individual.coordinates, 3)

## ----variable-coordinates-----------------------------------------------------
variable.coordinates <- modelo$var$coord

round(variable.coordinates, 3)

## ----plot-individuals-object, fig.width=7, fig.height=5-----------------------
riem.plot(
  modelo,
  choix = "ind",
  title = "Student grades"
)

## ----plot-variables-object, fig.width=7, fig.height=7-------------------------
riem.plot(
  modelo,
  choix = "var",
  title = "Student grades"
)

## ----biplot-object, fig.width=7, fig.height=7---------------------------------
riem.biplot(
  modelo,
  title = "Student grades"
)

## ----plot-individuals-manual, fig.width=7, fig.height=5-----------------------
riem.plot(
  data = modelo$data,
  choix = "ind",
  components = modelo$ind$coord,
  explained.inertia = inertia,
  title = "Riemannian PCA"
)

## ----plot-variables-manual, fig.width=7, fig.height=7-------------------------
riem.plot(
  data = modelo$data,
  choix = "var",
  correlations = modelo$var$coord,
  explained.inertia = inertia,
  title = "Riemannian PCA"
)

