### Install factoextra from GitHub Source: https://github.com/kassambara/factoextra/blob/master/README.md Install the latest version of factoextra directly from GitHub. Ensure the 'remotes' package is installed first. ```r if(!require(remotes)) install.packages("remotes") remotes::install_github("kassambara/factoextra") ``` -------------------------------- ### Install factoextra from CRAN Source: https://github.com/kassambara/factoextra/blob/master/README.md Install the factoextra package from CRAN using this command. ```r install.packages("factoextra") ``` -------------------------------- ### Install FactoMineR Package Source: https://github.com/kassambara/factoextra/blob/master/README.md Use this command to install the FactoMineR package from CRAN. ```r # Install install.packages("FactoMineR") ``` -------------------------------- ### Load factoextra Library Source: https://github.com/kassambara/factoextra/blob/master/README.md Load the installed factoextra package into your R session. ```r library("factoextra") ``` -------------------------------- ### Load factoextra and decathlon2 dataset Source: https://github.com/kassambara/factoextra/blob/master/README.md Loads the factoextra package and the decathlon2 dataset for PCA analysis. Ensure the factoextra package is installed. ```r library("factoextra") #> Loading required package: ggplot2 #> Welcome to factoextra! #> Want to learn more? See two factoextra-related books at https://www.datanovia.com/en/product/practical-guide-to-principal-component-methods-in-r/ data("decathlon2") df <- decathlon2[1:23, 1:10] ``` -------------------------------- ### Load FactoMineR Library Source: https://github.com/kassambara/factoextra/blob/master/README.md Load the installed FactoMineR package into your R session. ```r # Load library("FactoMineR") ``` -------------------------------- ### MCA with Supplementary Quantitative Variables Source: https://github.com/kassambara/factoextra/blob/master/README.md This example shows how to perform a Multiple Correspondence Analysis (MCA) using FactoMineR's MCA function, specifying quantitative supplementary variables. It then extracts these supplementary variables using get_mca. ```r # FactoMineR MCA quantitative supplementary variables are supported directly library(FactoMineR) data(poison) res.mca <- MCA(poison, quanti.sup = 1:2, graph = FALSE) get_mca(res.mca, "quanti.sup") ``` -------------------------------- ### Phylogenic Dendrogram Visualization Source: https://github.com/kassambara/factoextra/blob/master/README.md This snippet visualizes a hierarchical clustering result as a phylogenic tree using fviz_dend with a specified layout. It requires the igraph package to be installed. ```r if (requireNamespace("igraph", quietly = TRUE)) { # The default compatibility alias "layout.auto" still works, and # modern igraph layout names are accepted as well. fviz_dend(res, type = "phylogenic", phylo_layout = "layout_nicely", show_labels = FALSE) } ``` -------------------------------- ### Partitioning Clustering with K-means Source: https://github.com/kassambara/factoextra/blob/master/README.md This snippet demonstrates how to perform k-means clustering on scaled data and visualize the clusters using factoextra's fviz_cluster function. Ensure the factoextra package is loaded. ```r library("factoextra") # 1. Loading and preparing data data("USArrests") df <- scale(USArrests) # 2. Compute k-means set.seed(123) km.res <- kmeans(scale(USArrests), 4, nstart = 25) # 3. Visualize library("factoextra") fviz_cluster(km.res, data = df, palette = c("#00AFBB","#2E9FDF", "#E7B800", "#FC4E07"), ggtheme = theme_minimal(), main = "Partitioning Clustering Plot" ) ``` -------------------------------- ### Visualize PCA variables by contribution Source: https://github.com/kassambara/factoextra/blob/master/README.md Visualizes PCA variables, coloring them by their contribution to the principal axes using a gradient. Uses repel=TRUE to prevent text overlap. ```r # Control variable colors using their contributions fviz_pca_var(res.pca, col.var="contrib", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE # Avoid text overlapping ) ``` -------------------------------- ### Compute and Visualize Elbow Method for Clustering Source: https://github.com/kassambara/factoextra/blob/master/README.md This snippet demonstrates computing the Within-Sum of Squares (WSS) for different numbers of clusters (k) using the 'hcut' method on scaled iris data. It visualizes the results using fviz_nbclust, showing the k=1 baseline internally. ```r data(iris) iris.scaled <- scale(iris[, -5]) res.pca <- prcomp(iris[, -5], scale = TRUE) # WSS now computes the k = 1 baseline internally fviz_nbclust(iris.scaled, hcut, method = "wss", hc_method = "complete") ``` -------------------------------- ### Hierarchical Clustering Visualization Source: https://github.com/kassambara/factoextra/blob/master/README.md This snippet shows how to compute hierarchical clustering using hcut and visualize the dendrogram with factoextra's fviz_dend function. The data is scaled before clustering. ```r library("factoextra") # Compute hierarchical clustering and cut into 4 clusters res <- hcut(USArrests, k = 4, stand = TRUE) # Visualize fviz_dend(res, rect = TRUE, cex = 0.5, k_colors = c("#00AFBB","#2E9FDF", "#E7B800", "#FC4E07")) ``` -------------------------------- ### Generate PCA Biplot Source: https://github.com/kassambara/factoextra/blob/master/README.md Creates a biplot visualizing both individuals and variables on the principal component space. Use `repel = TRUE` to prevent text overlap. ```r # Biplot of individuals and variables fviz_pca_biplot(res.pca, repel = TRUE) ``` -------------------------------- ### Visualize Eigenvalues using Parallel Analysis Source: https://github.com/kassambara/factoextra/blob/master/README.md This code snippet visualizes the eigenvalues from a PCA result using fviz_eig. It explicitly enables and configures parallel analysis for validation, ensuring reproducibility with a specified seed. ```r # Parallel analysis validation is explicit and reproducible fviz_eig(res.pca, choice = "eigenvalue", parallel = TRUE, parallel.iter = 10, parallel.seed = 123) ``` -------------------------------- ### Generate Correspondence Analysis Biplot Source: https://github.com/kassambara/factoextra/blob/master/README.md Visualizes both row and column points and their relationships in the correspondence analysis space. `repel = TRUE` helps to avoid overlapping labels. ```r fviz_ca_biplot(res.ca, repel = TRUE) ``` -------------------------------- ### Extract and visualize eigenvalues/variances Source: https://github.com/kassambara/factoextra/blob/master/README.md Extracts eigenvalues and their percentage of variance from the PCA results using get_eig(). Visualizes these values using fviz_screeplot() to help determine the number of dimensions to retain. ```r # Extract eigenvalues/variances get_eig(res.pca) #> eigenvalue variance.percent cumulative.variance.percent #> Dim.1 4.1242133 41.242133 41.24213 #> Dim.2 1.8385309 18.385309 59.62744 #> Dim.3 1.2391403 12.391403 72.01885 #> Dim.4 0.8194402 8.194402 80.21325 #> Dim.5 0.7015528 7.015528 87.22878 #> Dim.6 0.4228828 4.228828 91.45760 #> Dim.7 0.3025817 3.025817 94.48342 #> Dim.8 0.2744700 2.744700 97.22812 #> Dim.9 0.1552169 1.552169 98.78029 #> Dim.10 0.1219710 1.219710 100.00000 # Visualize eigenvalues/variances fviz_screeplot(res.pca, addlabels = TRUE, ylim = c(0, 50)) ``` -------------------------------- ### Determine Optimal Clusters using Gap Statistic Source: https://github.com/kassambara/factoextra/blob/master/README.md This snippet uses the fviz_nbclust function from factoextra to determine the optimal number of clusters for k-means using the gap statistic method on scaled data. ```r # Optimal number of clusters for k-means library("factoextra") my_data <- scale(USArrests) fviz_nbclust(my_data, kmeans, method = "gap_stat") ``` -------------------------------- ### Extract and visualize PCA results for variables Source: https://github.com/kassambara/factoextra/blob/master/README.md Extracts variable-specific results like coordinates and contributions using get_pca_var(). Visualizes these using fviz_pca_var() for a default plot. ```r # Extract the results for variables var <- get_pca_var(res.pca) var #> Principal Component Analysis Results for variables #> =================================================== #> Name Description #> 1 "$coord" "Coordinates for the variables" #> 2 "$cor" "Correlations between variables and dimensions" #> 3 "$cos2" "Cos2 for the variables" #> 4 "$contrib" "contributions of the variables" # Coordinates of variables head(var$coord) #> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 #> X100m -0.8506257 -0.17939806 0.3015564 0.03357320 -0.1944440 #> Long.jump 0.7941806 0.28085695 -0.1905465 -0.11538956 0.2331567 #> Shot.put 0.7339127 0.08540412 0.5175978 0.12846837 -0.2488129 #> High.jump 0.6100840 -0.46521415 0.3300852 0.14455012 0.4027002 #> X400m -0.7016034 0.29017826 0.2835329 0.43082552 0.1039085 #> X110m.hurdle -0.7641252 -0.02474081 0.4488873 -0.01689589 0.2242200 # Contribution of variables head(var$contrib) #> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 #> X100m 17.544293 1.7505098 7.338659 0.13755240 5.389252 #> Long.jump 15.293168 4.2904162 2.930094 1.62485936 7.748815 #> Shot.put 13.060137 0.3967224 21.620432 2.01407269 8.824401 #> High.jump 9.024811 11.7715838 8.792888 2.54987951 23.115504 #> X400m 11.935544 4.5799296 6.487636 22.65090599 1.539012 #> X110m.hurdle 14.157544 0.0332933 16.261261 0.03483735 7.166193 # Graph of variables: default plot fviz_pca_var(res.pca, col.var = "black") ``` -------------------------------- ### Color Individuals by Groups in PCA Source: https://github.com/kassambara/factoextra/blob/master/README.md Visualizes PCA results with individuals colored according to their group membership. `habillage` specifies the grouping variable, and `addEllipses` draws concentration ellipses for each group. ```r # Compute PCA on the iris data set # The variable Species (index = 5) is removed # before PCA analysis iris.pca <- PCA(iris[,-5], graph = FALSE) # Visualize # Use habillage to specify groups for coloring fviz_pca_ind(iris.pca, label = "none", # hide individual labels habillage = iris$Species, # color by groups palette = c("#00AFBB", "#E7B800", "#FC4E07"), addEllipses = TRUE # Concentration ellipses ) ``` -------------------------------- ### Perform Principal Component Analysis Source: https://github.com/kassambara/factoextra/blob/master/README.md Computes the PCA on the prepared data frame using the FactoMineR::PCA function. Set graph = FALSE to prevent automatic plotting. ```r library("FactoMineR") res.pca <- PCA(df, graph = FALSE) ``` -------------------------------- ### Graph of Variable Categories in MCA Source: https://github.com/kassambara/factoextra/blob/master/README.md Generates a graph of variable categories from an MCA result. The `repel = TRUE` argument is used to avoid overplotting of category labels. ```r fviz_mca_var(res.mca, repel = TRUE) ``` -------------------------------- ### Extract and Visualize Individual Results Source: https://github.com/kassambara/factoextra/blob/master/README.md Extracts and displays coordinates, cos2 values, and contributions for individuals from PCA results. Useful for understanding individual positions and their quality on the factor map. ```r # Extract the results for individuals ind <- get_pca_ind(res.pca) ind #> Principal Component Analysis Results for individuals #> =================================================== #> Name Description #> 1 "$coord" "Coordinates for the individuals" #> 2 "$cos2" "Cos2 for the individuals" #> 3 "$contrib" "contributions of the individuals" # Coordinates of individuals head(ind$coord) #> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 #> SEBRLE 0.1955047 1.5890567 0.6424912 0.08389652 1.16829387 #> CLAY 0.8078795 2.4748137 -1.3873827 1.29838232 -0.82498206 #> BERNARD -1.3591340 1.6480950 0.2005584 -1.96409420 0.08419345 #> YURKOV -0.8889532 -0.4426067 2.5295843 0.71290837 0.40782264 #> ZSIVOCZKY -0.1081216 -2.0688377 -1.3342591 -0.10152796 -0.20145217 #> McMULLEN 0.1212195 -1.0139102 -0.8625170 1.34164291 1.62151286 # Graph of individuals # 1. Use repel = TRUE to avoid overplotting # 2. Control automatically the color of individuals using the cos2 # cos2 = the quality of the individuals on the factor map # Use points only # 3. Use gradient color fviz_pca_ind(res.pca, col.ind = "cos2", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE # Avoid text overlapping (slow if many points) ) ``` -------------------------------- ### Visualize Row or Column Points in CA Source: https://github.com/kassambara/factoextra/blob/master/README.md Generates separate plots for row points or column points in correspondence analysis. Use `fviz_ca_row` for rows and `fviz_ca_col` for columns. ```r # Graph of row points fviz_ca_row(res.ca, repel = TRUE) # Graph of column points fviz_ca_col(res.ca) ``` -------------------------------- ### Biplot of Individuals and Variables in MCA Source: https://github.com/kassambara/factoextra/blob/master/README.md Generates a biplot that displays both individuals and variables from an MCA result. The `repel = TRUE` argument helps in avoiding label overlaps. ```r fviz_mca_biplot(res.mca, repel = TRUE) ``` -------------------------------- ### Visualize Contributions of Variables and Individuals Source: https://github.com/kassambara/factoextra/blob/master/README.md Visualizes the contribution of variable categories and individuals to the principal axes of an MCA. Allows specifying the choice of elements ('var' or 'ind') and the axes to plot. ```r # Visualize variable categorie contributions on axes 1 fviz_contrib(res.mca, choice ="var", axes = 1) # Visualize individual contributions on axes 1 # select the top 20 fviz_contrib(res.mca, choice ="ind", axes = 1, top = 20) ``` -------------------------------- ### Compute Correspondence Analysis Source: https://github.com/kassambara/factoextra/blob/master/README.md Performs correspondence analysis on a contingency table. The `CA` function from FactoMineR is used, and results can be visualized with factoextra. ```r # Loading data data("housetasks") # Computing CA library("FactoMineR") res.ca <- CA(housetasks, graph = FALSE) ``` -------------------------------- ### Graph of Individuals in MCA Source: https://github.com/kassambara/factoextra/blob/master/README.md Generates a graph of individuals from an MCA result. This function can color individuals by groups, add concentration ellipses, and use text repulsion to avoid overplotting. ```r # Color by groups # Add concentration ellipses # Use repel = TRUE to avoid overplotting grp <- as.factor(poison[, "Vomiting"]) fviz_mca_ind(res.mca, habillage = grp, addEllipses = TRUE, repel = TRUE) ``` -------------------------------- ### Compute MCA using FactoMineR Source: https://github.com/kassambara/factoextra/blob/master/README.md Computes the Multiple Correspondence Analysis using the MCA function from the FactoMineR package. Requires the 'poison' dataset and specifies supplementary quantitative and qualitative variables. ```r library(FactoMineR) data(poison) res.mca <- MCA(poison, quanti.sup = 1:2, quali.sup = 3:4, graph=FALSE) ``` -------------------------------- ### Visualize CA Contributions Source: https://github.com/kassambara/factoextra/blob/master/README.md Plots the contributions of row or column variables to a specified axis in correspondence analysis. Use `choice = "row"` or `choice = "col"` to specify the variable type. ```r # Visualize row contributions on axes 1 fviz_contrib(res.ca, choice = "row", axes = 1) # Visualize column contributions on axes 1 fviz_contrib(res.ca, choice = "col", axes = 1) ``` -------------------------------- ### Extract Correspondence Analysis Results Source: https://github.com/kassambara/factoextra/blob/master/README.md Retrieves the results for row and column variables from a correspondence analysis object. Use `get_ca_row` for row data and `get_ca_col` for column data. ```r # Result for row variables get_ca_row(res.ca) # Result for column variables get_ca_col(res.ca) ``` -------------------------------- ### Visualize Variable Contributions to Principal Axes Source: https://github.com/kassambara/factoextra/blob/master/README.md Plots the top contributing variables to a specified principal axis. Use this to identify variables that most influence the principal components. ```r # Contributions of variables to PC1 fviz_contrib(res.pca, choice = "var", axes = 1, top = 10) # Contributions of variables to PC2 fviz_contrib(res.pca, choice = "var", axes = 2, top = 10) ``` -------------------------------- ### Extract MCA Results for Variables and Individuals Source: https://github.com/kassambara/factoextra/blob/master/README.md Extracts the results for variable categories and individuals from a computed MCA object. These functions are useful for further analysis and visualization. ```r # Extract the results for variable categories get_mca_var(res.mca) # Extract the results for individuals get_mca_ind(res.mca) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.