# Annotated code for performing nMDS and perMANOVA

Non-linear Multidimensional Scaling (nMDS)

We have seen what a Principal Component Analysis does, how it works, and how to implement it in R. Non-linear Multidimensional Scaling (nMDS) is another useful ordination method for reducing the number of dimensions that describe the data. Regardless of the initial number of dimensions, nMDS disposes the data points in a two- or three-dimensional space until it finds the configuration that produces the minimal amount of distortion. Here is how to produce and represent nMDS ordinations in R:

``````data(iris)
ii <- iris[,c(1:4)]
i.sp<-iris[,c(5)]
library(vegan)
i.dist <- vegdist(ii, method="euclidean")
i.mds <- metaMDS(i.dist, try=50)
plot(i.mds, display="sites",type="n",
xlab="Axis 1", ylab="Axis 2",
main="Iris species")
iris\$Species
iris\$col<-c(rep("red",50),rep("green",50),rep("blue",50))
points(i.mds, display="sites",
pch=21, cex=1,
bg=iris\$col
)
text(x=-2.5, y=3,paste("Stress=",round(i.mds\$stress,4)))
legend("topright",
legend=unique(iris\$Species),
pch=21,
pt.bg=unique(iris\$col)
)``````

The outcome is very similar to the one produced by PCA, and it suggests that the three Iris species under study are clearly discernible in the multidimensional space defined by sepal length, sepal width, petal length, and petal width. Let’s see how to test this pattern quantitatively.

perMANOVA

Permutation-based Multivariate Analysis of Variance, or PerMANOVA, is the multidimensional version of an Analysis of Variance. It answers the same question as an ANOVA does: does at least one study group differ significantly from the others? PerMANOVA can be performed in R with function adonis from package vegan.

``````i.obs <- ii
i.spp <- data.frame(species=iris[,5])
d.manova <- adonis(i.obs ~ species, method = "euclidean", data= i.spp)
d.manova

# Call:
# adonis(formula = i.obs ~ species, data = i.spp, method = "euclidean")

# Permutation: free
# Number of permutations: 999

# Terms added sequentially (first to last)

# Df SumsOfSqs MeanSqs F.Model      R2 Pr(>F)
# species     2    592.07 296.037  487.33 0.86894  0.001 ***
# Residuals 147     89.30   0.607         0.13106
# Total     149    681.37                 1.00000
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1``````

Post-hoc pairwise comparisons:

``````if (!require(pairwiseAdonis)) install_github("pmartinezarbizu/pairwiseAdonis/pairwiseAdonis")

factors= i.spp\$species,
sim.method='euclidean',