[R] I cannot get species scores to plot with site scores in MDS when I use a distance matrix as input. Problems with NA's?

1,408 views
Skip to first unread message

Edwin Lebrija Trejos

unread,
Nov 24, 2011, 7:16:07 AM11/24/11
to r-h...@r-project.org

Hi, First I should note I am relatively new to R so I would appreciate answers that take this into account.

I am trying to perform an MDS ordination using the function “metaMDS” of the “vegan” package. I want to ordinate species according to a set of functional traits. “Species” here refers to “sites” in traditional vegetation analyses while “traits” here correspond to “species” in such analyses.

My data looks like this:

Trait1 Trait2 Trait3 Trait4 Trait5 Trait…
Species1 228.44 16.56 1.66 13.22 1 short
Species2 150.55 28.07 0.41 0.60 1 mid
Species3 NA 25.89 NA 0.55 0 large
Species4 147.70 17.65 0.42 1.12 NA large
Species… 132.68 NA 1.28 2.75 0 short


Because the traits have different variable types, different measurement scales, and also missing values for some species, I have calculated the matrix of species distances using the Gower coefficient of similarity available in Package “FD” (which allows missing values).
My problem comes when I create a bi-plot of species and traits. As I have used a distance matrix in function “metaMDS” there are no species scores available. This is given as a warning in R:

"> NMDSgowdis<- metaMDS(SpeciesGowdis)
> plot(NMDSgowdis, type = "t")
Warning message:In ordiplot(x, choices = choices, type = type, display = display, :Species scores not available”

I have read from internet resources that in principle I could obtain the trait ("species") scores to plot them in the ordination but my attempts have been unsuccessful. I have tried using the function “wascores” in package vegan and “add.spec.scores” in package BiodiversityR. For this purpuse I have created a new species x traits table where factor traits were coded into dummy variables and all integer variables (including binary) were coerced to numeric variables. Here are the codes used and the error messages I have got:

“> NMDSgowdis<- metaMDS(SpeciesGowdis)
> NMDSpoints<-postMDS(NMDSgowdis$points,SpeciesGowdis)
> NMDSwasc<-wascores(NMDSpoints,TraitsNMDSdummies)
Error in if (any(w < 0) || sum(w) == 0) stop("weights must be non-negative and not all zero") : missing value where TRUE/FALSE needed”

I imagine the problem is with the NA’s in the data.
Alternatively, I have used the “add.spec.scores” function, method=”cor.scores”, found in package BiodiversityR. This seemed to work, as I got no error message, but the species scores were not returned. Here the R code and results:
“> A<-add.spec.scores(ordi=NMDSgowdis,comm=TraitsNMDSdummies,method="cor.scores",multi=1, Rscale=F,scaling="1")
> plot(A)
Warning message:In ordiplot(x, choices = choices, type = type, display = display, :Species scores not available“

Can anyone guide me to get the trait (“species”) scores to plot together with my species (“site”) scores?
Thanks in advance,
Edwin


______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

B77S

unread,
Nov 24, 2011, 10:30:33 AM11/24/11
to r-h...@r-project.org

B77S

unread,
Nov 24, 2011, 10:57:57 AM11/24/11
to r-h...@r-project.org
Try the daisy() function from the package "cluster", it seems to be able to
handle NAs and non-dummy coded character variables

metaMDS(daisy(df, metric="gower"))


Edwin Lebrija Trejos wrote

> R-help@ mailing list


> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
View this message in context: http://r.789695.n4.nabble.com/I-cannot-get-species-scores-to-plot-with-site-scores-in-MDS-when-I-use-a-distance-matrix-as-input-Pr-tp4103699p4104406.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Edwin Lebrija Trejos

unread,
Nov 27, 2011, 9:16:29 AM11/27/11
to r-h...@r-project.org, bps...@auburn.edu

Hi,
I have used the daisy() function from the package "cluster" as suggested. Indeed it can handle Na's but this is not different from the gowdis() function in Package "FD". My problem is not calculating the similarity matrix for its use with the metaMDS() function. My problem is that because I am using a similarity matrix as input for the metaMDS() function, then there are no "species scores" (in my case plant traits) available to plot together with the "site scores" (in my case species). Using the daisy() function does not solve the problem (see warning message):

> NMDSdaysi<-metaMDS(daisy(TraitsNMDS, metric="gower",type = list(ordratio = 8:9,asymm = 7)))
> plot(NMDSdaysi)

Warning message:
In ordiplot(x, choices = choices, type = type, display = display, :
Species scores not available

In my original post I explain that I have tried obtaining species scores using the functions wascores() and add.spec.scores() of the packages vegan and BiodiversityR but wascores returns an error:

> TraitsNMDSdummies<-dummy.data.frame(TraitsNMDS, c("leaf.type","dispersal.rank"))

> NMDSwasc<-wascores(NMDSpoints,TraitsNMDSdummies)
Error in if (any(w < 0) || sum(w) == 0) stop("weights must be non-negative and not all zero") :
missing value where TRUE/FALSE needed

And function add.spec.scores() does not solves the problem as the warning message shows:
> A<-add.spec.scores(ordi=NMDSdaysi,comm=TraitsNMDSdummies,method="cor.scores",multi=1,Rscale=F,scaling="1")

> plot(A)
Warning message:
In ordiplot(x, choices = choices, type = type, display = display, :
Species scores not available

I still don' know why I get an error in wascores() or how can I get species scores to use in a biplot.



------------------------------ Message: 47Date: Thu, 24 Nov 2011 07:57:57 -0800 (PST)
From: B77S <bps...@auburn.edu>To: r-h...@r-project.orgSubject: Re: [R] I cannot get species scores to plot with site scoresin MDS when I use a distance matrix as input. Problems with NA's?Message-ID: <1322150277396...@n4.nabble.com>Content-Type:


Try the daisy() function from the package "cluster", it seems to be able to handle NAs and non-dummy coded character variables metaMDS(daisy(df, metric="gower"))

Edwin Lebrija Trejos wrote>
> Hi, First I should note I am relatively new to R so I would appreciate> answers that take this into account.

> I am trying to perform an MDS ordination using the function ?metaMDS? of the ?vegan? package. I want to ordinate species according to a set of> functional traits. ?Species? here refers to ?sites? in traditional vegetation analyses while ?traits? here correspond to ?species? in such> analyses.

> My data looks like this:

> Trait1 Trait2 Trait3 Trait4 Trait5 Trait?


> Species1 228.44 16.56 1.66 13.22 1 short
> Species2 150.55 28.07 0.41 0.60 1 mid
> Species3 NA 25.89 NA 0.55 0 large
> Species4 147.70 17.65 0.42 1.12 NA large

> Species? 132.68 NA 1.28 2.75 0 short


> Because the traits have different variable types, different measurement
> scales, and also missing values for some species, I have calculated the

> matrix of species distances using the Gower coefficient of similarity> available in Package ?FD? (which allows missing values).

> My problem comes when I create a bi-plot of species and traits. As I have

> used a distance matrix in function ?metaMDS? there are no species scores> available. This is given as a warning in R:

> NMDSgowdis<- metaMDS(SpeciesGowdis)>> plot(NMDSgowdis, type = "t")

> Warning message:In ordiplot(x, choices = choices, type = type, display => display, :Species scores not available?

> I have read from internet resources that in principle I could obtain the
> trait ("species") scores to plot them in the ordination but my attempts have been

> unsuccessful. I have tried using the function ?wascores? in package vegan and ?
> add.spec.scores? in package BiodiversityR. For this purpuse I have created a new species x

> traits table where factor traits> were coded into dummy variables and all integer variables
> (including> binary) were coerced to numeric variables. Here are the codes used and the
> error messages I have got:

> ?> NMDSgowdis<- metaMDS(SpeciesGowdis)


> NMDSpoints<-postMDS(NMDSgowdis$points,SpeciesGowdis)
> NMDSwasc<-wascores(NMDSpoints,TraitsNMDSdummies)
> Error in if (any(w < 0) || sum(w) == 0) stop("weights must be non-negative

> and not all zero") : missing value where TRUE/FALSE needed?
> I imagine the problem is with the NA?s in the data.
> Alternatively, I have used the ?add.spec.scores? function,> method=?cor.scores?, found in
> package BiodiversityR. This seemed to work,as I got no error message, but the species scores

> were not returned. Here the R code and results:
> A<-add.spec.scores(ordi=NMDSgowdis,comm=TraitsNMDSdummies,method="cor.scores",multi=1,
> Rscale=F,scaling="1")
> plot(A)

> Warning message:In ordiplot(x, choices = choices, type = type, display => display, :Species scores not available?
> Can anyone guide me to get the trait (?species?) scores to plot together
> with my species (?site?) scores?
> Thanks in advance,
> Edwin
______________________________________________> R-help@ mailing list> https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide> http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.> --View this message in context: http://r.789695.n4.nabble.com/I-cannot-get-species-scores-to-plot-with-site-scores-in-MDS-when-I-use-a-distance-matrix-as-input-Pr-tp4103699p4104406.htmlSent from the R help mailing list archive at Nabble.com.

Gavin Simpson

unread,
Nov 29, 2011, 4:33:17 PM11/29/11
to B77S, r-h...@r-project.org
On Thu, 2011-11-24 at 07:57 -0800, B77S wrote:
> Try the daisy() function from the package "cluster", it seems to be able to
> handle NAs and non-dummy coded character variables
>
> metaMDS(daisy(df, metric="gower"))

That won't help the OP as the species scores (the species data, i.e. the
traits in this case) can not be computed from a site x site
dissimilarity matrix. This is has the same problem as the OPs existing
problem.

G

--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

Gavin Simpson

unread,
Nov 29, 2011, 4:44:54 PM11/29/11
to Edwin Lebrija Trejos, r-h...@r-project.org
On Thu, 2011-11-24 at 12:16 +0000, Edwin Lebrija Trejos wrote:
> Hi, First I should note I am relatively new to R so I would appreciate
> answers that take this into account.
>
> I am trying to perform an MDS ordination using the function “metaMDS”
> of the “vegan” package. I want to ordinate species according to a set
> of functional traits. “Species” here refers to “sites” in traditional
> vegetation analyses while “traits” here correspond to “species” in
> such analyses.
>
> My data looks like this:
>
> Trait1 Trait2 Trait3 Trait4 Trait5 Trait…
> Species1 228.44 16.56 1.66 13.22 1 short
> Species2 150.55 28.07 0.41 0.60 1 mid
> Species3 NA 25.89 NA 0.55 0 large
> Species4 147.70 17.65 0.42 1.12 NA large
> Species… 132.68 NA 1.28 2.75 0 short
>
<snip />

> Can anyone guide me to get the trait (“species”) scores to plot
> together with my species (“site”) scores?
> Thanks in advance,
> Edwin

I think you should pass metaMDS the species trait matrix and then get it
to use FD to compute the dissimilarities. Note from ?metaMDS there is a
distfun argument for metaMDSdit. metaMDS passes the community data to
metaMDSdist to compute the dissimilarities.

The only snag is that gowdis doesn't accept a `method` argument so we
need a wrapper:

wrapper <- function(x, method, ...) {
gowdis(x, ...)
}

then do

metaMDS(SpeciesGowdis, distfun = wrapper, XXXXX)

where XXXX represents any other arguments you want to pass to gowdis via
our wrapper. Essentially the wrapper ignores the method argument, we
just need it as metaMDSdist wants to call the dissimilarity function
with a method argument.

This is not tested as there wasn't reproducible code, but hopefully
you'll be able to work it out from the above.

HTH

G


--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________

Edwin

unread,
Dec 1, 2011, 6:15:41 AM12/1/11
to r-h...@r-project.org
Dear Gavin,
Thanks a lot for your reply. I am still not sure if your suggestion can help
me or not because I have got different types of errors while trying it. I
have tried my best to troubleshoot them and made some progress, but I have
reached a dead end (I don’t know how to go on). Below I am copping my TinnR
script. It contains not only the R script but also the errors returned in
the R console. I apologize that the script is still not really reproducible
but I just do not know how to create a sample data frame for it. Thanks
again for your help. Edwin.
##Load required packages##
library(vegan)
library(MASS)
library (FD)
library(dummies)
##Load full table of species traits and taxonomic data##
AllTraits<-read.delim("SpeciesTraitsFullTbl.txt",header=T)
##Edit data##
row.names(AllTraits)<-AllTraits$species.code
TraitsNMDS<-AllTraits[,c(8:12,14:18,23,25,30)]
##Check variable class using function "allClass" from
http://gettinggeneticsdone.blogspot.com/2010
/08/quickly-find-class-of-dataframe-vectors.html##
allClass <- function(x) {unlist(lapply(unclass(x),class))}
allClass(TraitsNMDS)
##Change variables to fulfill requirements for gowdis() function##
#Define ordinal variables#
TraitsNMDS$leaf.type<-ordered(TraitsNMDS$leaf.type, levels =
c("simple", "pinnate", "bipinnate"))
TraitsNMDS$dispersal.rank<-ordered(TraitsNMDS$dispersal.rank, levels =
c("s_disp","m_disp","l_disp"))
#<coerce integer an binary variables to numeric#
TraitsNMDS$max.height<-as.numeric(TraitsNMDS$max.height)
TraitsNMDS$heterospory<-as.numeric(TraitsNMDS$heterospory)
##Test wrapper function##

wrapper<- function(x, method, ...) {gowdis(x, ...)}
WrapTest1<-metaMDS(TraitsNMDS, distfun = wrapper, x=TraitsNMDS, asym.bin =
"heterospory", ord = "podani")
##Returned error: 'Error in if (any(autotransform, noshare > 0, wascores) &&
any(comm < 0)) { : missing value where TRUE/FALSE needed'##
#Set autotransform, noshare and wascores to FALSE (even if TRUE is
desired) to see if problems is with metaMDS function#
WrapTest2<-metaMDS(TraitsNMDS, distfun = wrapper, x=TraitsNMDS, asym.bin =
"heterospory", ord= "podani", autotransform =FALSE, noshare = FALSE,
wascores = FALSE)
#Returned error: 'Error in FUN(X[[1L]], ...) : only defined on a data
frame with all numeric variables'#
#transform factors to dummies and numeric#
TraitsNMDSCompleteDumm<-dummy.data.frame(TraitsNMDSComplete,

c("leaf.type","dispersal.rank"))

TraitsNMDSCompleteDumm$leaf.typesimple<-as.numeric(TraitsNMDSCompleteDumm$leaf.typesimple)

TraitsNMDSCompleteDumm$leaf.typepinnate<-as.numeric(TraitsNMDSCompleteDumm$leaf.typepinnate)

TraitsNMDSCompleteDumm$leaf.typebipinnate<-as.numeric(TraitsNMDSCompleteDumm$leaf.typebipinnate)

TraitsNMDSCompleteDumm$dispersal.ranks_disp<-as.numeric(TraitsNMDSCompleteDumm$dispersal.ranks_disp)

TraitsNMDSCompleteDumm$dispersal.rankm_disp<-as.numeric(TraitsNMDSCompleteDumm$dispersal.rankm_disp)

TraitsNMDSCompleteDumm$dispersal.rankl_disp<-as.numeric(TraitsNMDSCompleteDumm$dispersal.rankl_disp)

##Re-test wrapping function##
WrapTest3<-metaMDS(TraitsNMDSCompleteDumm, distfun = wrapper,
x=TraitsNMDSCompleteDumm,asym.bin = "heterospory", ord = "podani")
#The function runs but returns an error related to the gowdis() function
after the transformation of the data is done:
#'Square root transformation Wisconsin double standardization. Error in
gowdis(x, ...) : w needs to be a numeric vector of length = number of
variables in x'
# But testing the wrapper alone does work!#
DistMatrixWrapper<-wrapper(x=TraitsNMDSCompleteDumm,asym.bin =
"heterospory", ord = "podani")
class(DistMatrixWrapper)
[1] "dist"
# I do not know why the error is produced


--
View this message in context: http://r.789695.n4.nabble.com/I-cannot-get-species-scores-to-plot-with-site-scores-in-MDS-when-I-use-a-distance-matrix-as-input-Pr-tp4103699p4127949.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Reply all
Reply to author
Forward
0 new messages