Principal Component Analysis (PCA) is a dimensionality reduction/ feature selection technique aimed at increasing the comprehensibility of a model with a goal of minimizing information loss in the process. Create a variable to perform Principal Component Analysis (PCA) and name it res.pca, assigning it to only the numerical columns (negating the last one). Ensure to normalize the data by scaling it.
res.pca <- prcomp(iris[, -5], scale = TRUE)
Each principal component carries with it a percentage of variance that it accounts for in the model. Create a new variable to measure variance explained and assign it to var_explained.
Note Keep in mind that this next line will be useful for subsequent renditions of PCA involving other datasets.
This step finally leverages gganimate() to add animation to the visualization. Add the fviz dataframe to the transition effect transition_reveal(), ensuring to correct the animation direction by making the transition a function of a sequence along the explained variance var_explained. Assign this to a new dataframe animated.