2024-02-04

Introduction

  • Dimensionality Reduction Method.
  • Transform a large set of variables into a smaller one retaining crucial information.
  • Steps
    • 1.Standardization
      • \(Normalization = \frac{{\text{{value}} - \text{{mean}}}}{\text{{standard deviation}}}\)
    • 2.Covariance Matrix Computation
      • \(S = \frac{1}{{n-1}} X^T X\)
    • 3.Eigenvalues and Eigenvectors
    • 4.Selection of Principal Components
    • 5.Data transformation in new dimensional space

Principal Components

  • \(\Phi_{11}\) is the loading vector
  • \(X^1, \ldots, X^p\) are normalized predictors.

1.First Principal Component (PC1)

  • Linear combination of original predictor variables that capture maximum variance in the data set

  • \(Z^1 = \Phi_{11} X^1 + \Phi_{21} X^2 + \Phi_{31} X^3 + \ldots + \Phi_{p1} X^p\)

2.Second Principal Component (PC2)

  • Linear combination of original predictors that captures the remaining variance

  • \(Z^2 = \Phi_{12} X^1 + \Phi_{22} X^2 + \Phi_{32} X^3 + \ldots + \Phi_{p2} X^p\)

Mathematical Concept

  • 1.Standardization

  • 2.Compute the covariance matrix of the whole dataset

    • \((X, Y) = \frac{1}{n} \sum_{i=1}^{n} (X_i - \bar{X}) (Y_i - \bar{Y})\)
  • 3.Compute Eigenvectors and corresponding Eigenvalues

    • \(A - \lambda I = 0\)
  • 4.Sort the eigenvectors by decreasing eigenvalues

    • Choose k eigenvectors with the largest eigenvalues to form \(d \times k\) dimensional matrix
  • 5.Transform samples onto the new subspace

    • \(y = W^\prime \times x\)

Loda Data

## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Data Processing

##      Sepal.Length Sepal.Width Petal.Length Petal.Width
## [1,]   -0.8976739  1.01560199    -1.335752   -1.311052
## [2,]   -1.1392005 -0.13153881    -1.335752   -1.311052
## [3,]   -1.3807271  0.32731751    -1.392399   -1.311052
## [4,]   -1.5014904  0.09788935    -1.279104   -1.311052
## [5,]   -1.0184372  1.24503015    -1.335752   -1.311052
## [6,]   -0.5353840  1.93331463    -1.165809   -1.048667
numerical.data = iris[1:4]
normalized.data = scale(numerical.data)
head(normalized.data)

Data Exploration

  • Correlation over 0.8 leads to multicollinearity problems when creating models. PCA helps tackling this issue.

Applying PCA

## Standard deviations (1, .., p=4):
## [1] 1.7083611 0.9560494 0.3830886 0.1439265
## 
## Rotation (n x k) = (4 x 4):
##                     PC1         PC2        PC3        PC4
## Sepal.Length  0.5210659 -0.37741762  0.7195664  0.2612863
## Sepal.Width  -0.2693474 -0.92329566 -0.2443818 -0.1235096
## Petal.Length  0.5804131 -0.02449161 -0.1421264 -0.8014492
## Petal.Width   0.5648565 -0.06694199 -0.6342727  0.5235971
## Importance of components:
##                           PC1    PC2     PC3     PC4
## Standard deviation     1.7084 0.9560 0.38309 0.14393
## Proportion of Variance 0.7296 0.2285 0.03669 0.00518
## Cumulative Proportion  0.7296 0.9581 0.99482 1.00000

Focus on Proportion of Variance

Orthogonality of PCA

Done on Normalized Data

PCA Biplot

Petal Length & Petal Width are very close meaning they are highly correlated. Sepal Length has high correlation with these two variables.

PCA Biplot Code

suppressPackageStartupMessages({library(ggplot2)})
suppressWarnings({
  pc_scores = as.data.frame(data.pc$x)
  loadings = as.data.frame(data.pc$rotation)
  
biplot = ggplot(data = pc_scores, aes(x = PC1, y = PC2)) +
  geom_point() + geom_segment(data = loadings, 
  aes(x = 0, y = 0, xend = PC1, yend = PC2), 
  arrow = arrow(length = unit(0.2, "inches")), 
  color = "red") + geom_text(data = loadings, 
  aes(x = PC1, y = PC2, label = row.names(loadings)), 
  hjust = -0.2, vjust = -0.2, size = 3, color = "red") +
  labs(x = "Principal Component 1 (~73% variance)",
  y = "Principal Component 2 (~23% variance)",
  title = "PCA Biplot") + theme_minimal()
  
  print(biplot)
})

Scree Plot

Screeplot Code

library(ggplot2)
variance.explained = (data.pc$sdev^2) / sum(data.pc$sdev^2)

scree.data = data.frame(Component = paste0("PC", 
      1:length(variance.explained)),
      VarianceExplained = variance.explained)

scree.plot = ggplot(scree.data, 
      aes(x = Component, y = VarianceExplained)) +
      geom_bar(stat = "identity", fill = "skyblue", width = 0.5) +
      labs(x = "Principal Component", 
      y = "Proportion of Variance Explained",
      title = "Scree Plot") +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(scree.plot) 

3D PCA

Cluster of versicolor and virginica means they are very similar in terms of their observations.

3D PCA (Part 1)

prin_comp = prcomp(normalized.data, rank. = 3)

components = prin_comp[["x"]]
components = data.frame(components)
components$PC2 = -components$PC2
components$PC3 = -components$PC3
components = cbind(components, iris$Species)

tot_explained_variance_ratio = 
summary(prin_comp)[["importance"]]['Proportion of Variance',]
tot_explained_variance_ratio = 100 * sum(tot_explained_variance_ratio)

3D PCA (Part 2)

fig = plot_ly(components, x = ~PC1, y = ~PC2, z = ~PC3, color = ~iris$Species, colors = c('purple','coral','green') ) %>%
  add_markers(size = 12)

fig = fig %>%
  layout(title = tit,
    scene = list(bgcolor = "white"))