taddallas / metacom

R package for the analysis of metacommunity structure
GNU General Public License v3.0
10 stars 9 forks source link

Different results in Turnover #9

Closed littlemandy closed 7 years ago

littlemandy commented 7 years ago

I get qualitatively different results if I use the separate turnover() function than if I use the Metacommunity() function. The results for coherence and boundary clumping are the same. The only difference is with the turnover function, specifically the simulated mean. Which function is providing the correct answer? Thank you for your help! Example below:

Metacommunity(veg, scores = 1, method = "r1", sims = 1000,

  • order = TRUE, allowEmpty = TRUE, binary = TRUE, verbose = FALSE)

$Turnover is different using the separate function: it is significantly high turnover z pval "1,316,771" "-9.12140545737929" "7.41563066350554e-20" simulatedMean simulatedVariance method "298,240.212" "111663.79926418" "r1"

Turnover(veg, method = "r1", sims = 1000, scores = 1, order = TRUE,

  • allowEmpty = TRUE, binary = TRUE, verbose = FALSE) turnover z pval simulatedMean simulatedVariance method 1 1,254,166 11.5047 1.249268e-30 1,700,020 38754.11 r1 This is significantly low turnover.
littlemandy commented 7 years ago

Another piece of data: I tested both functions with an idealized data set. The function embedded withing Metacommunity() seems to be the incorrect version. Results:

metaHighT<-Metacommunity(High, scores = 1, method = "r1", sims = 1000,

  • order = TRUE, allowEmpty = TRUE, binary = TRUE, verbose = FALSE) There were 50 or more warnings (use warnings() to see the first 50) metaHighT $Comm a b c d e f g h i j k l m n o p q r s t 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

$Coherence embAbs z pval "0" "11.2343731839505" "2.76448874699434e-29" simulatedMean simulatedVariance method "175.086" "15.5848481382236" "r1"

$Turnover turnover z pval "3500" "1.32009958093428" "0.186801772678876" simulatedMean simulatedVariance method "4563.703" "805.774818326342" "r1"

$Boundary index P df 1 0.5142857 0 17

Turnover(High, method = "r1", sims = 1000, scores = 2, order = TRUE,

  • allowEmpty = TRUE, binary = TRUE, verbose = FALSE) turnover z pval simulatedMean simulatedVariance method 1 6775 -8.510826 1.726984e-17 5157.794 190.0175 r1
taddallas commented 7 years ago

Thank you for reporting this. I think there was a bug in the Metacommunity function that I may have fixed.

tests <- TestMatrices
mTurn <- plyr::ldply(tests, function(x){Metacommunity(x)$Turnover})
tTurn <- plyr::ldply(tests, Turnover)

This is the table from the Metacommunity results

.id turnover z pval simulatedMean simulatedVariance method
lei1a 1031 0.305496732981808 0.759987857825579 1103.633 237.753770035653 r1
lei1b 116 0.422251289577714 0.672841608274015 130.158 33.5297969466456 r1
lei2a 0 2.4986461558013 0.012466872222379 68.482 27.4076422709954 r1
lei2b 191 -2.3841118266189 0.0171203990500571 114.407 32.1264292827332 r1
lei3c 775 -2.91552413684608 0.00355091639218749 514.982 89.1839641160644 r1
pres3c 0 3.08811212870848 0.00201432452100491 624.234 202.140976098905 r1
pres4a 1960 0.835555902710837 0.403404785332221 2351.89 469.017092367573 r1

This is the table of the Turnover results. They still are a little off, but that may just be due to the randomization procedure.

.id turnover z pval simulatedMean simulatedVariance method
lei1a 1031 1.0365220 0.2999587 1143.742 108.76952 r1
lei1b 116 0.0668101 0.9467329 117.382 20.68550 r1
lei2a 0 3.7354949 0.0001873 84.777 22.69498 r1
lei2b 191 -1.1475440 0.2511569 157.631 29.07862 r1
lei3c 775 -9.0598635 0.0000000 500.560 30.29185 r1
pres3c 0 9.2323839 0.0000000 1484.674 160.81155 r1
pres4a 1960 -4.8881089 0.0000010 1560.889 81.64937 r1

I've included a new argument that allows the seeding of the null matrices, so results should be repeatable, but I am still a bit unsure why the small differences between the two functions. I'll look more into it. For now, you can play around with the new code by installing the package from Github (devtools::install_github('taddallas/metacom')). Let me know if you find any more bugs. 🐛

Cheers, Tad

littlemandy commented 7 years ago

Thank you very much for being so speedy!

taddallas commented 7 years ago

No worries. Let me know if you find any other issues. I'll close this issue for now, but will keep looking into the slight difference between the two functions.

MaruLacoretz commented 7 years ago

Hi, I am working with metacommunities and I am using the metacom package, although, I have had some troubles with it. I had inconsistent outcomes when i run “Metacommunity” and “Turnover” function with the same matrix (12 sites, 167 species). I hope you can guide me to the solution. These was what I have obtained:

ems.test12x167 <- Metacommunity(tabla.prueba7, method='r1', sims=100, scores=1) ems.test12x167 $Coherence embAbs z "479" "9.69942151349105" pval simulatedMean "3.03212861126114e-22" "821.88" simulatedVariance method "35.3505618374337" "r1"

$Turnover turnover z "32362" "-2.95682517632121" pval simulatedMean "0.00310824295169412" "23859.81" simulatedVariance method "2875.44561920233" "r1"

$Boundary index P df 1 7.54902 1.173583e-07 164

VS

turnover.12x167 <- Turnover(tabla.prueba7, method='r1', sims=100, scores=1) turnover.12x167 turnover z pval simulatedMean 1 32362 7.459905 8.658489e-14 54397.91 simulatedVariance method 1 2953.913 r1

The problem is that I have opposite outputs and I can not decide if i have nestedness or turnover (there is an important difference in the simulatedMean). I tried with more sims and with order FALSE but I obtained the same results. A slight different outcome is in the package example too. The simulatedMean is always higher with “turnover” function and here, test is significative at first, but then, no significative. intmat=TestMatrices[[4]] intmat

ems.test <- Metacommunity(intmat, method='r1', sims=100, scores=1) ems.test

$Coherence embAbs z "0" "4.64782364967428" pval simulatedMean "3.35455507360718e-06" "17.58" simulatedVariance method "3.78241545400114" "r1"

$Turnover turnover z "191" "-2.3854439043864" pval simulatedMean "0.0170585237238189" "113.71" simulatedVariance method "32.4006780699716" "r1"

turnover.intmat <- Turnover(intmat, method='r1', sims=100, scores=1, binary=TRUE) turnover.intmat turnover z pval simulatedMean 1 191 -1.165018 0.2440116 160.23 simulatedVariance method 1 26.4116 r1

I am sorry for the inconvenience, but I do not know If I am doing something wrong. If I have to chose a function (Metacommunity or Turnover), which one do you recommend that I follow? Thank you very much! Mariela

taddallas commented 7 years ago

Mariela,

Thank you for pointing this out! The Metacommunity function was incorrect, in that it "filled in" all the embedded absences in the null matrices, which it shouldn't be doing. I think was the result of a poor fix from this same bug. The Turnover function is correct. The Github repo is updated, and I'll record the bug and submit an update to CRAN soon.

intMat <- TestMatrices[[4]]

Metacommunity(intMat, method='tswap', sims=1000)
Turnover(intMat, method='tswap', sims=1000)

Metacommunity output

turnover z pval simulatedMean simulatedVariance method
191 -4.286965 1.811307e-05 145.185 10.68705 tswap

Turnover output

turnover z pval simulatedMean simulatedVariance method
191 -4.286965 1.81e-05 145.185 10.68705 tswap
MaruLacoretz commented 7 years ago

Thank you so much for your answer! I've just run it successfully!