# R program for Lecture 11 (VHM 802)
# it includes only code for the simulation-based ANOVA tests

sparrow <- read.csv("r:/sparrow.csv", header=TRUE)
sparrowstd <- scale(sparrow[,2:5])
set.seed(210325)

library(vegan)
#anosim
sparrow.simFuni.eucl <- anosim( sparrow[,2], sparrow$Survivorship, distance="euclidean") 
summary(sparrow.simFuni.eucl)
sparrow.simFuni.manh <- anosim( sparrow[,2], sparrow$Survivorship, distance="manhattan") 
summary(sparrow.simFuni.manh) # same

sparrow.simF.eucl <- anosim( sparrowstd, sparrow$Survivorship, distance="euclidean") 
summary(sparrow.simF.eucl)
sparrow.eucl <- vegdist(sparrowstd, method="euclidean")
sparrow.simF.eucl2 <- anosim( sparrow.eucl, sparrow$Survivorship) 
summary(sparrow.simF.eucl2) # same result with matrix
sparrow.simF.manh <- anosim( sparrowstd, sparrow$Survivorship, distance="manhattan") 
summary(sparrow.simF.manh)

#adonis
sparrow.permFuni.eucl <- adonis (sparrow[,2]~as.factor(Survivorship), data=sparrow, method="euclidean")
print(sparrow.permFuni.eucl)
anova(lm(sparrow[,2]~as.factor(Survivorship), data=sparrow)) # same

sparrow.permF.eucl <- adonis (sparrowstd~as.factor(Survivorship), data=sparrow, method="euclidean")
print(sparrow.permF.eucl)
sparrow.permF.manh <- adonis (sparrowstd~as.factor(Survivorship), data=sparrow, method="manhattan")
print(sparrow.permF.manh)
