library(MVA) ## Eurodist library(datasets) ?eurodist eurodist class(eurodist) eurocmd <- cmdscale(eurodist) plot(eurocmd, type = "n") text(eurocmd, rownames(eurocmd), cex = 0.7) plot(eurocmd[,1], -eurocmd[,2], type = "n", asp = 1) text(eurocmd[,1], -eurocmd[,2], rownames(eurocmd), cex = 0.7) ################################################## ## US cities pollution ################################################## setwd("C:/Users/kalischm/ETH/teaching/12/ams/vorlesung/v1.1/") dat <- read.csv(file = "USairpollution.csv", header = TRUE, row.names = 1) dim(dat) head(dat) summary(dat) ## scale xs <- apply(dat, 2, function(x) (x - min(x))/(diff(range(x)))) ## check eigenvalues: Is inner product matrix pos. semidef.? poldist <- dist(xs) pol.mds <- cmdscale(poldist, k = 40, eig = TRUE) ev <- pol.mds$eig ev ## choose number of dimensions cumsum(abs(ev)) / sum(abs(ev)) cumsum(ev^2) / sum(ev^2) ## two or three dimensions should be fine ## refit with chosen number of dimensions pol.mds <- cmdscale(poldist, k = 2, eig = TRUE) pol.mds ## plot x <- pol.mds$points plot(x[,1], x[,2], type = "n") text(x[,1], x[,2], labels = rownames(x), cex = 0.7) x11() stars(xs, draw.segments = TRUE, key.loc = c(15,2)) ## Voting library(MASS) load("C:/Users/kalischm/ETH/teaching/12/ams/vorlesung/v4.1/voting.rda") voting mdsRes <- isoMDS(voting) str(mdsRes) mycol <- c(1,1,2,2,1,1,1,2,2,2,2,1,1,2,2) plot(mdsRes$points, type = "n", xlim = c(-15,10)) text(mdsRes$points, labels = rownames(mdsRes$points), col = mycol)