rclassificationpcafactoextra

Contributions of features in PCA based on group/class


I would like to get/plot the contribution of the features for each type of wine class(barolo, grignolino, barbera). With fviz_contrib I get the contribution over all classes, as shown in the MWE below. However I was wondering if and how it is possible to calculate/plot them individually filtered by classes/groups.

library(ggbiplot)
library(factoextra)

data(wine)

wine.pca <- prcomp(wine, scale. = TRUE)

# plot the PCA 
print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE))

# plot the contributions of the features for all wine classes
g.contr <- fviz_contrib(wine.pca, choice = "var", axes = 1:2, fill = "lightblue", color = "darkblue", top = 45)
print(g.contr)

Solution

  • If you calculate the pca for each class separatly:

    barolo.pca <- prcomp(wine[wine.class=="barolo", ], scale. = TRUE)
    grignolino.pca <- prcomp(wine[wine.class=="grignolino", ], scale. = TRUE)
    barbera.pca <- prcomp(wine[wine.class=="barbera", ], scale. = TRUE)
    # plot the contributions of the features for each wine classes
    
    g.contr <- fviz_contrib(barolo.pca, choice = "var", axes = 1:2, fill = "lightblue", color = "darkblue", top = 45)
    print(g.contr)
    
    g.contr <- fviz_contrib(grignolino.pca, choice = "var", axes = 1:2, fill = "lightblue", color = "darkblue", top = 45)
    print(g.contr)
    
    g.contr <- fviz_contrib(barbera.pca, choice = "var", axes = 1:2, fill = "lightblue", color = "darkblue", top = 45)
    print(g.contr)