Collaborated with a group of classmates on Slack. We helped one another with debugging and resources. A classmate provided a link to RPubs

Loading the Images

pic_list <- list.files("jpg", pattern = "\\.jpg")[1:17]

Plotting all 17 Images

plot_shoes <- function(path, add = FALSE){
  pic <- readJPEG(path, native = T)
  dims <- dim(pic)[2:1]
  if (!add)
    plot(1, 1, xlim = c(1,dims[1]), ylim = c(1,dims[2]), asp = 1,  type = 'n', xaxs = 'i', yaxs = 'i', xaxt = 'n', yaxt = 'n', xlab = '',ylab = '',bty = 'n')
    rasterImage(pic,1,1,dims[1],dims[2])
}


height <- 1200
width <- 2500
scale <- 20

pic_array = array(rep(0, length(pic_list) * height / scale * width / scale * 3), dim = c(length(pic_list), height/scale, width/scale,3))

for (i in 1:length(pic_list)){
  temp = resize(readJPEG(paste0("jpg/", pic_list[i])), height/scale, width/scale)
  pic_array[i,,,] = array(temp, dim = c(1, height/scale, width/scale,3))}


vec <- matrix(0, length(pic_list), prod(dim(pic_array)))

for (i in 1:length(pic_list)){
  pic_new <- readJPEG(paste0("jpg/", pic_list[i]))
  red = as.vector(pic_array[i,,,1])
  green = as.vector(pic_array[i,,,2])
  blue = as.vector(pic_array[i,,,3])
  vec[i,] <- t(c(red,green,blue))
}


par(mfrow = c(4,5))
par(mai = c(.3,.3,.3,.3))
for (i in 1:length(pic_list)){
  plot_shoes(writeJPEG(pic_array[i,,,]))
}

Eigenvalues

shoes_df <- as.data.frame(t(vec))
scale_shoe <- scale(shoes_df, center = TRUE, scale = TRUE)
shoe_mean <- attr(scale_shoe, "scaled:center")
shoe_sd <- attr(scale_shoe, "scaled:scale")

cor_scale <- cor(scale_shoe)

scale_eigen <- eigen(cor_scale)

eigval <- scale_eigen$values
eigvec <- scale_eigen$vectors
pvar <- eigval / sum(eigval)
cvar <- cumsum(eigval) /sum(eigval)

goal <- min(which(cvar > 0.8))

fullscale <- diag((eigval[1:goal])^(-1/2)) / sqrt(nrow(scale_shoe) - 1)

eig_image <- scale_shoe %*% eigvec[,1:goal] %*% fullscale

Principal Components and Visualization

height <- 1200
width <- 2500
scale <- 20
data <- pic_array
dim(data) <- c(length(pic_list), height*width*3/scale^2)
pcomp <- princomp(t(as.matrix(data)), scores=TRUE, cor=TRUE)

pcomp_all <- t(pcomp$scores)
dim(pcomp_all) <- c(length(pic_list), height/scale, width/scale, 3)
par(mfrow=c(5,4))
par(mai=c(.001,.001,.001,.001))
for (i in 1:17){
  plot_shoes(writeJPEG(pcomp_all[i,,,]))
}

precision <- (pcomp$sdev[1:17]^2)/sum(pcomp$sdev^2)

cumsum(precision)
##    Comp.1    Comp.2    Comp.3    Comp.4    Comp.5    Comp.6    Comp.7    Comp.8 
## 0.6928202 0.7940449 0.8451073 0.8723847 0.8913841 0.9076338 0.9216282 0.9336889 
##    Comp.9   Comp.10   Comp.11   Comp.12   Comp.13   Comp.14   Comp.15   Comp.16 
## 0.9433872 0.9524455 0.9609037 0.9688907 0.9765235 0.9832209 0.9894033 0.9953587 
##   Comp.17 
## 1.0000000

```