--- title: "Interactive MCA/PCA results exploration with explor" author: "Julien Barnier" date: "`r Sys.Date()`" output: rmarkdown::html_vignette: fig_width: 5 toc: true vignette: > %\VignetteIndexEntry{[en] Interactive MCA/PCA results exploration with explor} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ## explor `explor` is an R package to allow interactive exploration of multivariate analysis results. For now on, the following analyses are supported : Analysis | Function | Package | Notes ------------- | ------------- | ---------- | -------- Principal component analysis | PCA | [FactoMineR](http://factominer.free.fr/) | - Correspondance analysis | CA | [FactoMineR](http://factominer.free.fr/) | - Multiple correspondence analysis | MCA | [FactoMineR](http://factominer.free.fr/) | - Principal component analysis | dudi.pca | [ade4](https://cran.r-project.org/package=ade4) | Qualitative supplementary variables are ignored Correspondance analysis | dudi.coa | [ade4](https://cran.r-project.org/package=ade4) | - Multiple correspondence analysis | dudi.acm | [ade4](https://cran.r-project.org/package=ade4) | Quantitative supplementary variables are ignored Specific Multiple Correspondance Analysis | speMCA | [GDAtools](https://cran.r-project.org/package=GDAtools) | - Multiple Correspondance Analysis | mca | [MASS](https://cran.r-project.org/package=MASS) | Quantitative supplementary variables are not supported Principal Component Analysis | princomp | stats | Supplementary variables are ignored Principal Component Analysis | prcomp | stats | Supplementary variables are ignored Correspondance Analysis | textmodel_ca | [quanteda.textmodels](https://cran.r-project.org/package=quanteda.textmodels) | Only coordinates are available The philosophy behind `explor` is to only be an exploration interface which doesn't really do anything by itself : analysis and computations are made in your R script, and `explor` only helps you visualizing the results. As such it can not disrupt code execution and reproducibility. ## Features For each type of analysis, `explor` launches a `shiny` interactive Web interface which is displayed inside RStudio or in your system Web browser. This interface provides a series of tabs with interactive data and graphics. These data and graphics are displayed with several "interactive" features. Numerical results are shown as dynamic tables which are sortable and searchable thanks to the `DT` package. Most graphics are generated with the `scatterD3` package which provides the following features : - zoom with your mousewheel - pan with your mouse - tooltips when hovering points - points highlighting when hovering legend items - draggable labels - points selection with a lasso selection tool - ability to export the currently displayed plot as an SVG file - ability to get the R code to reproduce the displayed plot in a script or document - fully updatable plot, which means than any change in the interface controls leads to an animated transition, which can give some interesting visual clues. ## Usage Usage is very simple : you just apply the `explor()` function to the result object of one of the supported analysis functions. ### `prcomp`, `princomp` and `MASS::mca` To visualize and explore these functions results, just pass the result object to `explor()`. Here is an example for a sample PCA with `princomp` : ```r data(USArrests) pca <- princomp(USArrests, cor = TRUE) explor(pca) ``` `explor` supports the visualization of supplementary individuals whose scores have been computed with `predict`. You just have to add them as a `supi` element to your result object. Here is an example with `prcomp` : ```r pca <- prcomp(USArrests[6:50,], scale. = TRUE) pca$supi <- predict(pca, USArrests[1:5,]) explor(pca) ``` For `MASS::mca`, `explor()` also supports qualitative supplementary variables. You must include their predicted coordinates to a `supv` element. It's also best to manually add row names to the `supi`data, if any : ```r library(MASS) mca <- MASS::mca(farms[4:20, 2:4], nf = 11) supi_df <- farms[1:3, 2:4] supi <- predict(mca, supi_df, type="row") rownames(supi) <- rownames(supi_df) mca$supi <- supi mca$supv <- predict(mca, farms[4:20, 1, drop=FALSE], type="factor") explor(mca) ``` Note that the results of these three functions are quite limited : they provide variables and individuals coordinates, but no contributions or squared cosinus. ### `FactoMineR` functions Supported `FactoMineR` functions should work "out of the box". Just pass the result object to `explor()`. Example with a principal correspondence analysis from `FactoMineR::PCA` : ```r library(FactoMineR) data(decathlon) pca <- PCA(decathlon[,1:12], quanti.sup = 11:12) explor(pca) ``` Example with a simple correspondence analysis from `FactoMiner::CA` : ```r data(children) res.ca <- CA(children, row.sup = 15:18, col.sup = 6:8) explor(res.ca) ``` Example with a multiple correspondence analysis from `FactoMineR::MCA` : ```r library(FactoMineR) data(hobbies) mca <- MCA(hobbies[1:1000, c(1:8,21:23)], quali.sup = 9:10, quanti.sup = 11, ind.sup = 1:100) explor(mca) ``` ### `ade4` functions `ade4` functions should also work by directly passing the object result to `explor()`. For example, to visualize a simple PCA results : ```r library(ade4) data(deug) pca <- dudi.pca(deug$tab, scale = TRUE, scannf = FALSE, nf = 5) explor(pca) ``` There's a bit more work to be done if you want to display supplementary elements, as `ade4` don't include them directly in the results analysis. For a principal component analysis, you have to compute supplementary individuals (resp. variables) results with `suprow` (resp. `supcol`) and add them manually as a `supi` (resp. `supv`) element of your result object. Here is an example of how to do this : ```r data(deug) d <- deug$tab sup_var <- d[-(1:10), 8:9] sup_ind <- d[1:10, -(8:9)] pca <- dudi.pca(d[-(1:10), -(8:9)], scale = TRUE, scannf = FALSE, nf = 5) ## Supplementary individuals pca$supi <- suprow(pca, sup_ind) ## Supplementary variables pca$supv <- supcol(pca, dudi.pca(sup_var, scale = TRUE, scannf = FALSE)$tab) explor(pca) ``` You have to do the same thing for supplementary elements in a multiple correspondence analysis : ```r data(banque) d <- banque[-(1:100),-(19:21)] ind_sup <- banque[1:100, -(19:21)] var_sup <- banque[-(1:100),19:21] acm <- dudi.acm(d, scannf = FALSE, nf = 5) ## Supplementary variables acm$supv <- supcol(acm, dudi.acm(var_sup, scannf = FALSE, nf = 5)$tab) ## Supplementary individuals acm$supi <- suprow(acm, ind_sup) explor(acm) ``` For simple correspondence analysis, you can add supplementary rows or columns by adding their coordinates to `supr` and `supc` elements of your result object : ```r data(bordeaux) tab <- bordeaux row_sup <- tab[5,-4] col_sup <- tab[-5,4] coa <- dudi.coa(tab[-5,-4], nf = 5, scannf = FALSE) coa$supr <- suprow(coa, row_sup) coa$supc <- supcol(coa, col_sup) explor(coa) ``` ### `GDAtools` functions `GDAtools` functions should also work by directly passing the object result to `explor()`. For example, to visualize a `speMCA` results : ```r library(GDAtools) data(Music) mca <- speMCA(Music[,1:5], excl = c(3, 6, 9, 12, 15)) explor(mca) ``` To display supplementary individuals, you have to compute their data with the `indsup` function, and add them manually as a `supi` element of your result object : ```r mca <- speMCA(Music[3:nrow(Music), 1:5], excl = c(3, 6, 9, 12, 15)) mca$supi <- indsup(mca, Music[1:2, 1:5]) explor(mca) ``` To display supplementary variables, you have to compute their data with the `speMCA_varsup` function and add them manually as a `supv` element of your result object : ```r mca <- speMCA(Music[3:nrow(Music), 1:4], excl = c(3, 6, 9, 12)) mca$supi <- indsup(mca, Music[1:2, 1:4]) mca$supv <- speMCA_varsup(mca, Music[3:nrow(Music), 5:6]) explor(mca) ``` ## Exporting Plots `explor` provides two different ways to export the displayed plots. ### SVG export To save the displayed plot as an SVG file, click on the *Export to SVG* button in the bottom of the left sidebar, or choose *Export to SVG* in the gear menu. SVG is a vector graphics format, editable with softwares like [Inkscape](https://inkscape.org/). This SVG export may cause issues when used inside RStudio. As a workaround, you can open `explor` in a browser (with *Open in Browser* icon) before exporting. ### R code Another way is to get the R code which allows to generate the current plot. This code can then be used in a script or a *Rmarkdown* document. To do this, click on the *Get R code* button on the bottom of the left sidebar. A modal dialog should show up with the R code that you can then copy/paste. Please note that this R code keeps track of the current plot zooming, but not of any custom label positioning. If you want to keep those, you have to first save them in a CSV file with *Export labels positions* gear menu entry. Then, in your R script, read this file in an object with `read.csv` and pass this object to the `export_labels_positions` argument in the generated code : ```r labels <- read.csv("position_labels.csv") res <- explor::prepare_results(mca) explor::MCA_var_plot(res, xax = 1, yax = 2, var_sup = TRUE, , var_lab_min_contrib = 0, col_var = "Variable", symbol_var = "Type", size_var = NULL, size_range = c(10, 300), labels_size = 10, point_size = 56, transitions = TRUE, labels_positions = labels) ``` ## Feedback `explor` is quite a young package, so there certainly are bugs or problems. Thanks for reporting them by mail or by opening an [issue on GitHub](https://github.com/juba/explor/issues)