I am trying to run a permanova on insect community composition across host plant species. No matter what we have tried, it does not work and I cannot figure out why not. I know permanova can handle zeros, but we do have a lot, could that be the issue?
We receive this error: Error in if (any(lhs < -TOL)) stop("dissimilarities must be non-negative") : missing value where TRUE/FALSE needed
Here is what we tried:
library(vegan)
#insect community composition on 3 different species of milkweed
lilly.data<-read.csv("https://raw.githubusercontent.com/lmgermeroth/Garden-sampling-SU21/main/backyard2021_main_forR.4.22.22.csv")
lilly.data$SPECIES<-as.factor(lilly.data$SPECIES)
# creating a species data matrix so I can tell the model how to calculate the community composition, but first, make them numeric.
lilly.data[6:151] <- lapply(lilly.data[6:152], as.numeric)
#Create the matrix of insect species
lilly.species<-as.matrix(lilly.data[,8:152])
lilly.dist<-vegdist(lilly.species,"bray")
#get a warning.....but it runs
lilly.permanova<-adonis2(lilly.dist ~ SPECIES,permutations=999,method='bray',data=lilly.data)
# why doesn't this work?
Second, we are trying to create an ordination plot, but that also won't work and we aren't sure why. Might be related to the above issue?
library(ggplot2)
ord<-metaMDS(lilly.species)
#this is where we throw the error....
scores<-scores(ord,display='sites')
scores<-cbind(as.data.frame(scores), Treatment=lilly.data$SPECIES)
center<-aggregate(cbind(NMDS1,NMDS2)~Treatment,data=scores,FUN=mean)
seg<-merge(scores,setNames(center,c('Species','oNMDS1','oNMDS2')),by='Treatment',sort=FALSE)
ggplot(scores,aes(x=NMDS1,y=NMDS2,colour=Treatment))
geom_segment(data=seg,
mapping=aes(xend=oNMDS1,yend=oNMDS2))
geom_point(data=cent,size=8)
geom_point()
coord_fixed()
error here is: Square root transformation Wisconsin double standardization Error in cmdscale(dist, k = k) : NA values not allowed in 'd' In addition: Warning messages: 1: In distfun(comm, method = distance, ...) : you have empty rows: their dissimilarities may be meaningless in method “bray” 2: In distfun(comm, method = distance, ...) : missing values in results
Any advice would be greatly appreciated!
CodePudding user response:
You can't have sites where zero species were detected. After you've made your community data matrix you should remove any rows with zero species:
lilly.species <- lilly.species[rowSums(!lilly.species) < ncol(lilly.species), ]
Then all the remaining code should work fine.