install.packages(c("FactoMineR", "factoextra)")
Principal Component Analysis
📝 Part I: Reanalysis of the socio-economic dataset
To retrive the data you can go here.
- Start by having a glimpse at the data and perform a short descriptive analysis.
- To perform PCA analysis, we will use the FactoMineR library (and factoextra for visualization purposes). Installation (only done once of course) is done using
you are now ready to load them (to be done each time you restart R)
library(FactoMineR)
library(factoextra)
- Read the documentation of the PCA function (you can skip the col.w and row.w arguments since they were not cover during the course). Next perform a PCA on the socio-economic dataset.
- Show the evolution of the explained percentage variance as the number of factorial axis increases. How many axis will you use? The function fviz_screeplot may be useful.
- Plot the first factorial plane, i.e., plane define from the 1st and 2nd factorial axis and interpret. The function fviz_pca_var will be useful. Comment.
- Plot the contribution of the variables to the first two axes. Does it match your previous statements? The function fviz_contrib will be useful.
- Learn how to retrieve the principal components, i.e., coordinates of the individuals onto factorial axis using the get_pca_ind function. Retrieving these coordinates might be useful for subsequent analysis.
- Plot the individuals on the first factorial plane. Now plot the five individuals the best represented on the first factorial plane. Do the same with those with the largest contributions. The function fviz_pca_ind might be useful.
- Create your own new individual as well as new quantitative and qualitative variables. Learn how to use the ind.sup, quali.sup, quanti.sup arguments.
- Use the habillage and addEllipses argument of the fviz_pca_ind function and try to make sense of it.
- Use the selectargument to plot only individuals whose \(\cos^2\) is larger than 0.9. Plot only the individuals that have the largest top 5 contribution to the first factorial axis.
- Reinventing the wheel… I wrote an implementation of the PCA (using SVD). Some parts of the code are missing though. Complete the code.
<- function(data){
mypca <- nrow(data)
n.obs <- ncol(data)
n.var <- scale(data)## centering and scaling the data
data <- svd(data)
decomp
<- decomp$u
U <- decomp$v
V <- diag(decomp$d)
D
## Compute the percentage of explained variance
<- 1## Fill in
explained.variance.prop
## Coordinates of individuals and variables onto factorial axis
<- 1## Fill in
ind.coord <- 1## Fill in
var.coord
## Some graphics
par(mfrow = c(1, 3))
## Evolution of the explained variance
barplot(100 * explained.variance.prop)
## Plot individuals onto the 1st factorial plane
<- paste("1st axis (", 100 * round(explained.variance.prop[1], 3), "%)", sep = "")
xlab <- paste("2nd axis (", 100 * round(explained.variance.prop[2], 3), "%)", sep = "")
ylab plot(ind.coord[,1:2], xlab = xlab, ylab = ylab, main = "Individuals")
abline(h = 0, lty = 2, col = "grey")
abline(v = 0, lty = 2, col = "grey")
## Plot the variable onto the 1st factorial plane
plot(0, xlim = c(-1, 1), ylim = c(-1, 1), xlab = xlab, ylab = ylab, main = "Variables",
type = "n")
abline(h = 0, lty = 2, col = "grey")
abline(v = 0, lty = 2, col = "grey")
## Draw the unit circle
<- seq(0, 2 * pi, length = 500)
angles lines(cos(angles), sin(angles))
arrows(rep(0, ncol(data)), rep(0, ncol(data)), var.coord[,1], var.coord[,2])
text(var.coord[,1], var.coord[,2], colnames(data))
return(list(ind.coord = ind.coord, var.coord = var.coord, explained.variance.prop = explained.variance.prop))
}
📝 Part II: The French premier league
Data can be retrieved from here. Perform a complete principal component analysis and give interpretation.
Good luck!
Want more? For those who already have finished the Lab, you can perform a PCA on ranks, i.e., rather than using the raw data, you compute the rank for each variable and apply PCA on these ranks. This prevents large contributions of outliers in the PCA.