How to do it...

Perform the following steps to perform multidimensional scaling using the metric method:

  1. First, you can perform metric MDS with a maximum of two dimensions:
        > swiss.dist =dist(swiss)
        > swiss.mds = cmdscale(swiss.dist, k=2)
  1. You can then plot the swiss data in a two-dimension scatter plot:
        > plot(swiss.mds[,1], swiss.mds[,2], type = "n", main = "cmdscale        (stats)")
        > text(swiss.mds[,1], swiss.mds[,2], rownames(swiss), cex = 0.9,        xpd = TRUE)  
The two-dimension scatter plot from cmdscale object
  1. In addition, you can perform nonmetric MDS with isoMDS:
 > library(MASS) > swiss.nmmds = isoMDS(swiss.dist, k=2) initial value 2.979731 ...

Get Machine Learning with R Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.