################################################## ## Classical Multidimensional Scaling ################################################## setwd("/u/kalisch/teaching/12/ams/vorlesung/v1.1/") dat <- read.csv(file = "USairpollution.csv", header = TRUE, row.names = 1) str(dat) summary(dat) ## scale xs <- apply(dat, 2, function(x) (x - min(x))/(diff(range(x)))) summary(xs) ## compute distance matrix head(xs) poldist <- dist(xs) poldist ## reduce to 2 dimensions pol.mds <- cmdscale(poldist, k = 2, eig = TRUE) pol.mds ## reduce to 3 dimensions pol.mds3 <- cmdscale(poldist, k = 3, eig = TRUE) pol.mds3 ## 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)) ################################################## ## Non-metric MDS ################################################## ## Voting: 15 US congressman in Rep/Democrat party voted in 19 votes ## Recorded is the number of votes in which each pair of congressmen disagreed load("/u/kalisch/teaching/12/ams/vorlesung/v4.1/voting.rda") voting library(MASS) mdsRes <- isoMDS(voting, k = 2) str(mdsRes) mycol <- c(1,1,2,2,1,1,1,2,2,2,2,1,1,2,2) ## color code for Repub/Demo. plot(mdsRes$points, type = "n", xlim = c(-15,10)) text(mdsRes$points, labels = rownames(mdsRes$points), col = mycol)