knitr::opts_chunk$set(echo = FALSE) library(DiagrammeR) library(knitr) library(kableExtra) library(vegan) library(ape) library(FD) library(edcpR)
Goal: reduce dimensions
PCA
PCoA
NMDS
# First update package library(edcpR) data(varechem_s6, package = "edcpR")
PCA
data(varechem_s6, package = "edcpR") env <- varechem_s6 str(env[,10:17]) env$Managementtype <- as.factor(env$Managementtype) env$Use <- as.factor(env$Use)
# rda without conditions or constraints is PCA # We have nutrient contents (g/kg), soil depth (cm), pH (1-14)... # scale = TRUE for data on different scales!! PCA <- rda(env[,2:15], scale=TRUE)
# Bar plot of relative eigen values (= percentage variance explained by each axis) eig <- as.vector(PCA$CA$eig) barplot(eig/sum(eig), xlab = "Principal Component", ylab = "Explained variance", ylim = c(0, 0.5))
# Calculate the percent of variance explained by first two or three axes # First two axes explain 60% of the variance # First three axes explain 72% of the variance sum((eig/sum(eig))[1:2]) sum((eig/sum(eig))[1:3]) # Three dimensional representation is possible, but more difficult # We prefer to plot in two dimensions, but not possible here # Solution: plot PC1 Vs. PC2 & PC1 vs. PC3
# In a biplot of a PCA, environment's scores are drawn as arrows # These arrows point in direction of increasing value for that variable biplot(PCA, choices=c(1,2), type=c("text","points"))
# If we would like to include three dimensions: biplot of axis 1 vs 3 biplot(PCA, choices=c(1,3), type=c("text","points"))
PCoA
# First step: create a distance matrix # We have quantitative and categorical environmental variables # We thus use Gower distance for mixed variables dist <- gowdis(env[, 2:17])
# Perform PCoA & plot the eigenvalues PCoA <- pcoa(dist) barplot(PCoA$values$Relative_eig, xlab = "Principal Component", ylab = "Explained variance", ylim = c(0, 0.45))
# Some distance measures may result in negative eigenvalues --> add a correction PCoA <- pcoa(dist, correction = "cailliez") barplot(PCoA$values$Rel_corr_eig, xlab = "Principal Component", ylab = "Explained variance", ylim = c(0, 0.3))
# There are no variables plotted on this biplot (input = dissimilarity matrix) biplot.pcoa(PCoA)
# However, we could work around this problem biplot.pcoa(PCoA, env[6])
NMDS
data(varespec_s6, package = "edcpR") sp <- varespec_s6 # Determine the number of dimensions for the final NMDS # Run NMDS for 1 up to 10 dimensions # Bray-Curtis distance matrix for species matrix with cover data # If you don`t provide a dissimilarity matrix, metaMDS automatically applies Bray-Curtis A<-vector() B<-vector() for (i in 1:10) { NDMS <- metaMDS(sp[2:45], distance = "bray", k=i, autotransform = TRUE, trymax=100) solution <- NDMS$converged stress <- NDMS$stress A1 <- cbind(solution,stress) B <- rbind(B,i) A <- rbind(A,A1) }
#Combine results of all NMDS NMDSoutput<-data.frame(B,A) names(NMDSoutput) <- c("k","Solution?","Stress") NMDSoutput
plot(NMDSoutput$Stress, xlab = "# of Dimensions", ylab = "Stress", main = "NMDS stress plot")
plot(NMDSoutput$`Solution?`, xlab = "# of Dimensions", ylab = "Solution", main = "NMDS convergence plot")
# Final result depends on the initial random placement of the points # We need set a seed to make the results reproducible set.seed(999) NMDSfinal <- metaMDS(sp[2:45], distance = "bray", k=2, autotransform = TRUE, trymax=100 )
# We can again check the stress NMDSfinal$stress
# Let’s check the results of NMDSfinal with a stressplot stressplot(NMDSfinal)
#Plot results wit text for species plot(NMDSfinal, type = "t")
#Plot results wit points for species plot(NMDSfinal, type = "p")
# There are no environmental variables on the plot (same as PCoA) # We can again work around this ef <- envfit(NMDSfinal, env[2:10], permu = 999)
ef
# Plot the vectors of the significant correlations plot(NMDSfinal, type = "t", display = "sites") plot(ef, p.max = 0.05)
There is NO assignment for this week's session:
Remember to upload Assignment 4 (Relationships & Comparing Means) by next week: December 2nd, 12 pm (= at noon!)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.