Species Scores not appearing from metaMDS - r

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")

Related

Species scores not available as result of metaMDS

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.

Different results between fpc::dbscan and dbscan::dbscan

I want to implement DBSCAN in R on some GPS coordinates. I have a distance matrix (dist_matrix) that I fed into the following functions:
dbscan::dbscan(dis_matrix, eps=50, minPts = 5,borderPoints=TRUE)
fpc::dbscan(dis_matrix,eps = 50,MinPts = 5,method = "dist")
and Im getting very different results from both functions in terms of number of clusters and if a point is a noise point or belongs to a cluster. Basically, the results are inconsistent between two algorithms. I have no clue why they generate these very different results although here
http://www.sthda.com/english/wiki/wiki.php?id_contents=7940
we see for iris data, both functions did the same.
my distance matrix [is coming from a function (geosphere::distm) which calculates the spatial distance between more than 2000 coordinates.
Furthermore, I coded dbscan according to this psuedo-code
source: https://cse.buffalo.edu/~jing/cse601/fa13/materials/clustering_density.pdf
My results are equal to what I obtained from fpc package.
Can anyone notice why they are different. I already looked into both functions and haven't found anything.
The documentation of geosphere::distm says that it does not return a dist object but a matrix. dbscan::dbscan assumes that you have a data matrix and not distances. Convert your matrix into a dist object with as.dist first. THis should resolve the problem.

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!

What type of matrix should I be using for nmds

I tried to analyze my data using NMDS in R. My matrix has species names listed down the rows and the columns listed as individual sites. I have zeros under some sites. I am comparing the species abundance at the various sites. It only allows me to use the following code if I remove the species titles on the rows.
The code I am trying to use is this code:
BirdMatrix<-read.csv("BirdMatrix.csv")
install.packages("vegan")
library(vegan)
community_matrix<-as.matrix(BirdMatrix, ncol=8, nrow=62)
example_NMDS=monoMDS(community_matrix, # Our community-by-species matrix
k=2) # The number of reduced dimensions
But I continuously get an error:
Error in monoMDS(community_matrix, k = 10):
'dist' must be a distance object (class "dist") or a symmetric square matrix
Do I have to change the matrix somehow?
Probably you already figured it out, you need to use the vegdistfunction in the vegan package.
for example:dist.bird <- vegdist( Birdmatrix, method ="bray")

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.

Resources