if (interactive() &&
requireNamespace("fda", quietly = TRUE) &&
requireNamespace("viridisLite", quietly = TRUE) &&
requireNamespace("vegan", quietly = TRUE)) {
require(ggplot2)
data("handwrit", package = "fda")
fives <- 5 * 0:280 + 1
hw <- handwrit[ , 1, ]
sd. <- .015
hh <- cbind(hw[fives,], rnorm(281, sd=sd.))
classical <- cmdscale(dist(hh), eig=TRUE)
ctmds <- mds.ct(dist(hh), argvals=handwritTime[fives], nbasis=100, recenter=TRUE,
weights="trap", norder=7, lambda=exp(2:40))
# a plot of GCV versus lambda is produced
pro.classical <- vegan::procrustes(hw[fives,], classical, scale=FALSE)
dat.classical <- as.data.frame(pro.classical$Yrot)
pro.ctmds <- vegan::procrustes(hw[fives,], fda::eval.fd(ctmds$argvals, ctmds$funcs),
scale=FALSE)
dat.ctmds <- as.data.frame(matrix(pro.ctmds$translation, length(handwritTime), 2, byrow=TRUE) +
fda::eval.fd(handwritTime, ctmds$funcs) %*% pro.ctmds$rotation)
names(dat.classical) <- names(dat.ctmds) <- c("x", "y")
dat.classical$time <- handwritTime[fives]
dat.ctmds$time <- handwritTime
# Plot of classical (dots) versus continuous-time (curve) MDS reconstructions
# of the handwritten "fda" (grey), corrupted by noise
g1 <- ggplot(hw, aes(x=X, y=Y)) + geom_point(color="darkgrey", size=.6) + coord_fixed() +
geom_point(data = dat.classical, aes(x, y, color = time), size=1) +
geom_point(data = dat.ctmds, aes(x, y, color = time), size=.6) +
scale_color_gradientn(colors=viridisLite::plasma(50)) +
labs(x="", y="", color="Time (ms)",
title=bquote(sigma == .(sd.) * ", "
~ .(round(100*ctmds$GOF[2,1], 1)) * "% explained"))
print(g1)
g2 <- plot(ctmds) # uses plot.mds.ct
print(g2)
g3 <- procrustes.plot.mds(ctmds, dat.classical[ , 1:2])
print(g3)
}
Run the code above in your browser using DataLab