library(vegan)
library(rgl)
library(cems)
###Swissroll data set example
#Create swissroll toy data set
data(swissroll)
d <- swissroll(500, nstd = 0.5)
plot3d(d$Xn)
#d$Xn contains noisy (orthogonal Normal distirbuted to the swissroll) of d$X
#Compute isomap
iso <- isomap(dist(d$Xn), k=15)
#Create and optimize CEM
ps <- cem(y = d$Xn, z = iso$points[, 1:2], knn=20, iter=5)
#Create test set
dtest <- swissroll(500, nstd = 0.25)
#parametrize manifold coordinates from test set
coords <- predict(ps, dtest$Xn)
#reconstruct points on manifold from coordinates
p <- predict(ps, coords)
#mean squared test error
mean( rowSums((dtest$X - p)^2) )
#optimize some more with samller step size
ps <- cem.optimize(ps, iter=5, step = 0.05)
#mean sqyuared error
coords <- predict(ps, dtest$Xn)
p <- predict(ps, coords)
mean( rowSums((dtest$X - p)^2) )
###Frey faces example
#Do not run
data("frey_faces")
#create isomap
iso <- isomap(dist(faces), k=25)
#Create conditional expectation surface (no optimization here, 20 iteration take
#roughly 2h on a 2Ghz laptop)
ps <- cem(y=faces, z = iso$points[, 1:3], knn=45, iter=0)
#parametrize manifold coordinates
coords <- predict(ps)
#reconstruct points on manifold from coordinates
p <- predict(ps, coords)
#mean squared test error
mean( rowSums((faces - p)^2) )
#plot images
split.screen(c(2,3))
ind <- c(1, 120, 333)
for(i in 1:length(ind)){
screen(i)
im <- matrix(faces[ind[i], 560:1], 20, 28)
image(1:nrow(im), 1:ncol(im), im, xlab="", ylab="")
screen(i+3)
im <- matrix(p[ind[i], 560:1], 20, 28)
image(1:nrow(im), 1:ncol(im), im, xlab="", ylab="")
}
Run the code above in your browser using DataLab