fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining


Description

This function can be used to visualize the quality of representation (cos2) of rows/columns from the results of Principal Component Analysis (PCA), Correspondence Analysis (CA) and Multiple Correspondence Analysis (MCA) functions.

The function fviz_cos2() [in factoextra package] is used.

Install and load factoextra

The package devtools is required for the installation as factoextra is hosted on github.

# install.packages("devtools")
devtools::install_github("kassambara/factoextra")

Load factoextra :

library("factoextra")

Usage

fviz_cos2(X, choice = c("row", "col", "var", "ind"), 
             axes = 1, fill = "steelblue", color = "steelblue",
             sort.val = c("desc", "asc", "none"), top = Inf)

Arguments

Argument Description
X an object of class PCA, CA and MCA [FactoMineR]; prcomp and princomp [stats]; dudi, pca, coa and acm [ade4]; ca [ca package].
choice allowed values are “row” and “col” for CA; “var” and “ind” for PCA or MCA.
axes a numeric vector specifying the dimension(s) of interest.
fill a fill color for the bar plot.
color an outline color for the bar plot.
sort.val a string specifying whether the value should be sorted. Allowed values are “none” (no sorting), “asc” (for ascending) or “desc” (for descending).
top a numeric value specifying the number of top elements to be shown.
not used.

Value

A ggplot2 plot

Examples

Principal component analysis

A principal component analysis (PCA) is performed using the built-in R function prcomp() and the decathlon2 [in factoextra] data

data(decathlon2)
decathlon2.active <- decathlon2[1:23, 1:10]
res.pca <- prcomp(decathlon2.active,  scale = TRUE)
# variable cos2 on axis 1
fviz_cos2(res.pca, choice="var", axes = 1 )

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# sorting
fviz_cos2(res.pca, choice="var", axes = 1,
           sort.val ="asc")

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# select the top 7 contributing variables
fviz_cos2(res.pca, choice="var", axes = 1, top = 7 )

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Change theme and color
fviz_cos2(res.pca, choice="var", axes = 1,
         fill = "lightgray", color = "black") +
         theme_minimal() +
         theme(axis.text.x = element_text(angle=45))

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Variable cos2 on axis 2
fviz_cos2(res.pca, choice="var", axes = 2)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Variable cos2 on axes 1 + 2
fviz_cos2(res.pca, choice="var", axes = 1:2)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# cos2 of individuals on axis 1
fviz_cos2(res.pca, choice="ind", axes = 1)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

Correspondence Analysis

The function CA() in FactoMineR package is used:

# Install and load FactoMineR to compute CA
# install.packages("FactoMineR")
library("FactoMineR")
data("housetasks")
res.ca <- CA(housetasks, graph = FALSE)
# Visualize row cos2 on axes 1
fviz_cos2(res.ca, choice ="row", axes = 1)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Visualize row cos2 on axes 1 + 2
fviz_cos2(res.ca, choice ="row", axes = 1:2)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Visualize column cos2 on axes 1
fviz_cos2(res.ca, choice ="col", axes = 1)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

Multiple Correspondence Analysis

The function MCA() in FactoMineR package is used:

library(FactoMineR)
data(poison)
res.mca <- MCA(poison, quanti.sup = 1:2,
              quali.sup = 3:4, graph=FALSE)
# Visualize individual cos2 on axes 1
fviz_cos2(res.mca, choice ="ind", axes = 1)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Select the top 20
fviz_cos2(res.mca, choice ="ind", axes = 1, top = 20)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

# Visualize variable categories cos2 on axes 1
fviz_cos2(res.mca, choice ="var", axes = 1)

fviz_cos2: Quick visualization of the quality of representation of rows/columns - R software and data mining

Infos

This analysis has been performed using R software (ver. 3.1.2) and factoextra (ver. 1.0.2)


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!






This page has been seen 2279 times