---
title: "Comparison with FactoMineR"
output: rmarkdown::html_vignette
description: >
  Start here if this is your first time using booklet.
  This document deals with the differences between FactoMineR and booklet.
vignette: >
  %\VignetteIndexEntry{Comparison with FactoMineR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r echo = FALSE, message = FALSE}
knitr::opts_chunk$set(collapse = T, comment = "#>")
```

`booklet` was designed to return same result as `FactoMineR` but with a more modern and tidyverse-friendly syntax. This document aims to show the differences between the two packages.


```{r message = FALSE, warning=FALSE}
library(FactoMineR)
library(booklet)
data(decathlon)
```


## Eigs

```{r}
# Get eigvalues and eigvectors with FactoMineR
X <- decathlon[, -c(11:13)]
res_pca <- PCA(X, quanti.sup = 10, ind.sup = 1, graph = FALSE)
head(res_pca$eig)
```


```{r}
# Get eigvalues and eigvectors with booklet
X_active <- X[-1, -10]
X_active_scaled <- pca_standardize(X_active, scale = TRUE)

eigs <- pca_weighted_eigen(X_active_scaled)

df_eigs <- data.frame(
  eigenvalue = eigs$values,
  `percentage of variance` = eigs$values / sum(eigs$values) * 100,
  `cumulative percentage of variance` = cumsum(eigs$values / sum(eigs$values)) * 100
)

rownames(df_eigs) <- paste0("comp ", 1:nrow(df_eigs))

df_eigs |> head()
```

## PCA

### Individuals

```{r}
# Get principal components with FactoMineR
head(res_pca$ind$coord)
```

```{r}
# Get principal components with booklet
ind_coords <- pca_ind_coords(eigs)
head(ind_coords[, 1:5])
```

```{r}
# Get individual cos2 with FactoMineR
head(res_pca$ind$cos2)
```

```{r}
# Get individual cos2 with booklet
ind_cos2 <- pca_ind_cos2(ind_coords)
head(ind_cos2[, 1:5])
```

```{r}
# Get individual contributions with FactoMineR
head(res_pca$ind$contrib)
```

```{r}
# Get individual contributions with booklet
ind_contrib <- pca_ind_contrib(ind_coords, eigs)
head(ind_contrib[, 1:5])
```

### Supplementary individuals

```{r}
# Get supplementary individuals with FactoMineR
res_pca$ind.sup$coord
```

```{r}
# Get supplementary individuals with booklet
weights <- rep(1, nrow(X_active)) / nrow(X_active)
center <- colMeans(X_active)
std <- sqrt(as.vector(crossprod(weights, as.matrix(X_active^2)) - center^2))

X_sup <- X[1, -10]
X_sup_scaled <- (X_sup - center) / std
ind_sup_coords <- as.data.frame(as.matrix(X_sup_scaled) %*% eigs$vectors)
rownames(ind_sup_coords) <- rownames(X_sup)
ind_sup_coords[, 1:5]
```

```{r}
# Get supplementary individuals cos2 with FactoMineR
res_pca$ind.sup$cos2
```

```{r}
# Get supplementary individuals cos2 with booklet
ind_sup_cos2 <- pca_ind_cos2(ind_sup_coords)
ind_sup_cos2[, 1:5]
```


### Variables

```{r}
# Get variable coordinates with FactoMineR
head(res_pca$var$coord)
```

```{r}
# Get variable coordinates with booklet
var_coords <- pca_var_coords(eigs)
head(var_coords[, 1:5])
```

```{r}
# Get variable cos2 with FactoMineR
head(res_pca$var$cos2)
```

```{r}
# Get variable cos2 with booklet
var_cos2 <- pca_var_cos2(var_coords)
head(var_cos2[, 1:5])
```

```{r}
# Get variable contributions with FactoMineR
head(res_pca$var$contrib)
```

```{r}
# Get variable contributions with booklet
var_contrib <- pca_var_contrib(var_cos2, eigs)
head(var_contrib[, 1:5])
```

### Supplementary variables

```{r}
# Get supplementary variables coordinates with FactoMineR
res_pca$quanti.sup$coord
```
```{r}
# Get supplementary CONTINUOUS variables coordinates with booklet
X_sup <- X[-1, 10, drop = FALSE]
X_sup_scaled <- pca_standardize(X_sup, scale = TRUE)

var_sup_coords <- as.data.frame(t(X_sup_scaled * weights) %*% eigs$U)
rownames(var_sup_coords) <- colnames(X_sup)
var_sup_coords[, 1:5]
```

```{r}
# Get supplementary variables cos2 with FactoMineR
res_pca$quanti.sup$cos2
```

```{r}
# Get supplementary variables cos2 with booklet
var_sup_cos2 <- pca_var_cos2(var_sup_coords)
var_sup_cos2[, 1:5]
```