I am getting the following error when I try to plot a model I created with vegdist
in the vegan
package of R.
#Error in x$vectors[take, axes][ch, ] : incorrect number of dimensions
I am trying to see if the species assemblage of animals differs between days of the week at a single site. My code is listed below, complete with annotations as to what I am trying to do at each step.
#reshape my data (df1) into a new multivariate dataframe
dfnew<-dcast(df1, DayOfWeek + SessionID ~ SpeciesName, fill = NULL)
#find out how many species there are
x <- length(dfnew)
#remove the rows (if any) that do not contain any values at all (excluding the first 2 columns which are actually factors we will use in the next step)
dfnew <- dfnew[rowSums(is.na(dfnew)) != ncol(dfnew[,3:x]),]
#finally, convert all na's to zeros
dfnew[is.na(dfnew)] <- 0
#make two separate data frames to hold the data (dfspecies), and the factors (dfDoW)
dfDoW <- data.frame(dfnew$DayOfWeek)
dfspecies <- data.frame(dfnew[,-1:-2])
library("vegan", lib.loc="~/R/win-library/3.2")
#create a resemblance matrix based off bray-curtis distances (best for comparing species assemblages)
dis <- vegdist(dfspecies, method = "bray")
#create the factor we need to test
groups <- factor(dfDoW$dfnew.DayOfWeek)
#create a model of how the species assemblage varies between days of the week
mod <- betadisper(dis, groups)
#plot the model into a PCoA plot
plot(mod)
#test if species varies according to day of week
permutest(mod, pairwise = T)
Now, the part where the error occurs is the second last line "plot(mod)"
#Error in x$vectors[take, axes][ch, ] : incorrect number of dimensions
I have looked at the str()
of the data.frame
to make sure they match (they do).
The crazy part is that this exact code works perfectly for another factor that I am testing on the same data (time of day).
I think the model is actually running because the permute
line of code provides output.
I provide a screenshot of a small demo dataset below that I can extrapolate back to my code.
If anyone can please help that would be great, thanks!