With the attached data file, build and visualize eigenimagery that accounts for 80% of the variability. Provide full R code and discussion.
Code Referenced from Dr.Fulton: https://rpubs.com/R-Minator/eigenshoes
library(jpeg)
library(EBImage)
library(OpenImageR)
## 
## Attaching package: 'OpenImageR'
## The following objects are masked from 'package:EBImage':
## 
##     readImage, writeImage
List the path of the 17 jpeg files.
files <- list.files(path="/Users/catherinecho/Documents/DATA 605/jpg - Assignment_04",full.names = T) 
Create the correlation matrix.
#create multidimensional array for all 17 shoes
height=1200; width=2500;scale=20
im = array(rep(0,length(files)*height/scale*width/scale*3), dim=c(length(files), height/scale, width/scale,3))

# 0 correlation matrix matrix(constat, rows, cols)
mat=matrix(0, 17, prod(dim(im))) 

#Get the correlation. matrix, which will hold RGB for each shoe
#im vector will get rescaled 
for (i in 1:length(files)){
  #resize using EBImage package for each shoe to the same x,y dimemnsions and store in im array for each shoe.
  im[i,,,] = resize(readJPEG(files[i]),height/scale, width/scale)
  #get the RGB vectors for each shoe
  r=as.vector(im[i,,,1]); g=as.vector(im[i,,,2]);b=as.vector(im[i,,,3])
  mat[i,] <- t(c(r, g, b))
}
#shoes now contains rgb data in one combined column vector where each column is a different shoe. 
shoes=as.data.frame(t(mat))
par(mfrow=c(3,3))
par(mai=c(.2,.2,.2,.2))

for (i in 1:length(files)){
  image<-readJPEG(writeJPEG(im[i,,,])) #convert to JPEG format, then read to plot
  if (exists('rasterImage')) {
    plot(1,1,xlim=c(1,dim(image)[2]),ylim=c(1,dim(image)[1]), type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
    rasterImage(image,1,1,dim(image)[2],dim(image)[1])
  }
  }

#Center the columns of X by subtracting the mean of each column from the column itself, resulting in a new matrix Z
shoes_scaled=scale(shoes, center = TRUE, scale = TRUE)

#calculate sample covariance matrix
correlation <- cor(shoes_scaled) 
str(correlation)
##  num [1:17, 1:17] 1 0.787 0.678 0.654 0.727 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:17] "V1" "V2" "V3" "V4" ...
##   ..$ : chr [1:17] "V1" "V2" "V3" "V4" ...
eig <- eigen(correlation)

#cumulative proportion of variance explained by each component by dividing the cumulative sum of the eigenvalues up to that point by the total sum of the eigenvalues. This will show which principal components cumulatively capture 80% of the variance in the rgb data. By default, eigen() returns components in decreasing eigenvalue order.
cumsum(eig$values) / sum(eig$values)
##  [1] 0.6928202 0.7940449 0.8451073 0.8723847 0.8913841 0.9076338 0.9216282
##  [8] 0.9336889 0.9433872 0.9524455 0.9609037 0.9688907 0.9765235 0.9832209
## [15] 0.9894033 0.9953587 1.0000000
A scaling matrix is calculated next. This is reducing the dimensions of the shoes_scaled data to only the first two principal components while also scaling the resulting principal components to have unit variance.
#Extracts the first two eigenvalues that capture 80% variance explained by the first two principal components. 
scaling <- eig$values[1:2]

#Scale the principal components to have unit variance.
scaling <- scaling^(-1/2)

#create a diagonal matrix with the inverse square root of the first two eigenvalues on the diagonal.
scaling <- diag(scaling)

#This is used to scale the resulting principal components by the square root of the degrees of freedom of the data.
scaling <- scaling/(sqrt(nrow(shoes_scaled)-1))
A matrix multiplication is performed between shoes_scaled, the first two eigenvectors, and the scaling matrix calculated previously.The image capturing 80% of the variance in the 17 jpeg photos is shown below.
eigenshoes=shoes_scaled%*%eig$vectors[,1:2]%*%scaling
imageShow(array(eigenshoes[,1], c(60,125,3)))