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