Species scores not available as result of metaMDS - r

I am conducting an ordination analysis in R and I'm having trouble dealing with the results of the function metaMDS, from vegan. More specifically, I'm not able to get species scores from the metaMDS result. I found a similar question here How to get 'species score' for ordination with metaMDS()?, but the answer for it did not work for me.
My data is available here: https://drive.google.com/file/d/1btKzAWL_fmJ80GjcgMnwX5m_ls8h8vpY/view?usp=sharing
Here is the code I wrote so far:
library(vegan)
mydata <- read.table("nmdsdata.txt", h=T, row.names=1)
dist.f <- vegdist(mydata, method = "bray")
dist.f2 <- stepacross(dist.f, path = "extended")
results<-metaMDS(dist.f2, trymax=500)
I was told to use the function stepacross since the species composition of my sites is quite different and I was not getting converging results in metaMDS without using it. So far so good.
My problem arises when I try to plot the results:
plot(results, type="t")
When I run that line I receive the following message on my console: "species scores not available". I tried to follow the approach recommended on the link I cited earlier, by running the code:
NMDS<-metaMDS(dist.f2,k=2,trymax=500, distance = "bray")
envfit(NMDS, dist.f2)
However, it does not seem to work here. It returns me the sites scores and not the species scores as it does when using the data from the post I commented earlier. The only difference in my code is that I use "bray" and not "euclidean" distance. In addition, I get a warning after running envfit: "Warning message: In complete.cases(X) & complete.cases(env): longer object length is not a multiple of shorter object length".
I need both sites scores and species scores to plot the results. Am I missing something here? I'm new to R, so consider that, please,
Any help would be appreciated. Thanks in advance.

If you want to have species scores, you must supply information on species. Dissimilarities do not have any information on species, and therefore they won't work. You have two obvious (and documented) alternatives:
Supply a data matrix to metaMDS and it will calculate the dissimilarities, and if requested, also perform the stepacross:
## bray dissimilarities is the default, noshare triggers stepacross
NMDS <- metaMDS(mydata, noshare = TRUE, autotransform = FALSE, trymax = 500)
This also turns off automatic data transformation as you did not use it in your dissimilarities either.
Add species scores to the result after the analysis when you did not supply information on species:
NMDS <- metaMDS(dist.f2, trymax = 500)
sppscores(NMDS) <- mydata
If you transformed community data, you should use similar transformation for mydata in sppscores.

Related

Species Scores not appearing from metaMDS

I'm not really sure what is happening considering it works on my computer at work, but doesn't transfer over when I'm on my laptop at home :/. For background, I am working with microbial community data using unifrac distances and trying to see how my different sample types cluster together.
Anywho, first I create a square distance matrix using my unifrac data file and my meta data.
generate.square.dist.matrix(unifrac,metadata,1)
unifrac.matrix.all = dist.matrix1
unifrac.dist.all = square.dist.matrix
Then run my NMDS
nmds.all = metaMDS(unifrac.dist.all, k = 4, trymax = 500, distance = "bray")
but when I attempt to extract my scores...
nmds.all.scores = as.data.frame(scores(nmds.all))
I receive the following error: "Error in x$species[, choices, drop = FALSE] :
incorrect number of dimensions".
There is no reproducible example, but I can still reproduce this. There is a bug in scores function, and it fails when the metaMDS result object has no species scores. It will not have species scores if you supply dissimilarity matrix as input, because dissimilarities have no information of species. I'll fix this in vegan. Meanwhile you can circumvent the problem by asking only site scores so that the function will not try to extract the non-existent species scores:
scores(nmds.all, display="site")

PCA scores vs. Varimax-rotated PCA scores

I have performed PCA using prcomp in R with my databases of 75-76 indicator variables and 7232 companies, including NAs. Before applying the function, I centred my data, but did not rescale them because they are all indicator variables. (Is my reasoning correct?)
After that I varimax-rotated the loadings of the 2 or 3 first principal components following the instructions by amoeba here.
Since I had centred, but not rescaled my data, I changed the code to:
Varimax_results <- varimax(rawLoadings,normalize = FALSE)
invLoadings <- t(pracma::pinv(VarimaxLoadings))
scores <- scale(DatosPCA, scale = FALSE) %*% invLoadings
Now I am trying to figure out why the scores given by "prcomp" and the scores obtained using the code above are not the same.
I am probably missing some theoretical background, so I would be grateful if someone could tell me if the scores are supposed to be the same and, in that case, what I am doing wrong in my code. If they are not supposed to be the same, which ones should I use?
Thank you very much!

R - Vegan package. metaMDS error

I would like to know why im getting this error running metaMDS:
'comm' has negative data: 'autotransform', 'noshare' and 'wascores' set to FALSE
I would like to do NMDS and dendogram graphs but can do so with the error above.
My data set is available for download if anyone wants to check DATASET. After importing the data, I transposed the column and rows. Afterwhich, I replaced the NA values with O before trying to run metaMDS.
abundance <- read.table("1_abundance.txt", header = TRUE)
abundance[is.na(abundance)] <- 0
abundance_trans <- t(abundance)
metaMDS(abundance_trans, distance = "bray", k = 2, trymax = 50)
It is not an error message but information: metaMDS tells you that you have negative data entries, and it will not make some tricks that it defaults to do with non-negative data.
Second issue is that you ask for Bray-Curtis dissimilarities that are only applicable with non-negative data.
You have two alternatives: either take care of negative values, or use a dissimilarity measure than can handle them. If you think that you do not have negative data, you are wrong: computer knows. You may have an error when reading in your data, and you may have columns or rows that you should not have. Check your data.

Creating a dendrogram with the results from the results the multipatt function in the indicspecies package

I am getting familiar with the multipatt function in indicspecies package. Thus far I only see summary being used to give a breakdown of the results. However I would like a dendrogram, ideally with the names of the species which are more 'indicative' of my given community location.
example from package file:
library(indicspecies)
library(stats)
data(wetland) ## Loads species data
wetkm = kmeans(wetland, centers=3) ## Creates three clusters using kmeans
## Runs the combination analysis using IndVal.g as statistic
wetpt = multipatt(wetland, wetkm$cluster, control = how(nperm=999))
## Lists those species with significant association to one combination
summary(wetpt)
wetpt gives the raw results but I am not sure how to proceed to get a cluster plot out of this result. Can anyone offer any pointers?

NMDS ordination interpretation from R output

I have conducted an NMDS analysis and have plotted the output too. However, I am unsure how to actually report the results from R. Which parts from the following output are of most importance? The graph that is produced also shows two clear groups, how are you supposed to describe these results?
MDS.out
Call:
metaMDS(comm = dgge2, distance = "bray")
global Multidimensional Scaling using monoMDS
Data: dgge2
Distance: bray
Dimensions: 2
Stress: 0
Stress type 1, weak ties
No convergent solutions - best solution after 20 tries
Scaling: centring, PC rotation, halfchange scaling
Species: expanded scores based on ‘dgge2’
The most important pieces of information are that stress=0 which means the fit is complete and there is still no convergence. This happens if you have six or fewer observations for two dimensions, or you have degenerate data. You should not use NMDS in these cases. Current versions of vegan will issue a warning with near zero stress. Perhaps you had an outdated version.
I think the best interpretation is just a plot of principal component. yOu can use plot and text provided by vegan package. Here I am creating a ggplot2 version( to get the legend gracefully):
library(vegan)
library(ggplot2)
data(dune)
ord = metaMDS(comm = dune)
ord_spec <- scores(ord, "spec")
ord_spec <- cbind.data.frame(ord_spec,label=rownames(ord_spec))
ord_sites <- scores(ord, "sites")
ord_sites <- cbind.data.frame(ord_sites,label=rownames(ord_sites))
ggplot(data=ord_spec,aes(x=NMDS1,y=NMDS2)) +
geom_text(aes(label=label,col='species')) +
geom_text(data=ord_sites,aes(label=label,col='sites'))

Resources