# NOT RUN {
#-------------------------------------------------------------------
# Average Projection with 'iris' dataset
#-------------------------------------------------------------------
# For PCA, take half of data from 'iris' data and repeat it 10 times.
# We will compare naive PCA, intrinsic, and extrinsic mean.
data(iris)
label = iris$Species # label information
idata = as.matrix(iris[,1:4]) # numeric data
ndata = nrow(idata)
# define a function for extracting pca projection
pcaproj <- function(X, p){
return(eigen(stats::cov(X))$vectors[,1:p])
}
# extract embedding for random samples
proj10 = list()
for (i in 1:10){
# index for random subsample
rand.now = base::sample(1:ndata, round(ndata/2))
# PCA via personal tool
proj10[[i]] = pcaproj(idata[rand.now,], p=2)
}
# compute intrinsic and extrinsic mean of projection matrices
mean.int = st.mean(proj10, type='intrinsic')$mu
mean.ext = st.mean(proj10, type='extrinsic')$mu
# compute 2-dimensional embeddings
fproj = pcaproj(idata, p=2)
f2 = idata%*%fproj # PCA with full data
i2 = idata%*%mean.int # projection with intrinsic mean
e2 = idata%*%mean.ext # projection with extrinsic mean
# visualize
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3))
plot(f2, cex=0.5, pch=19, col=label, main='full PCA')
plot(i2, cex=0.5, pch=19, col=label, main='intrinsic projection')
plot(e2, cex=0.5, pch=19, col=label, main='extrinsic projection')
par(opar)
# }
Run the code above in your browser using DataLab