Closed ngotelli closed 9 years ago
I don't actually detect an error in the plotting, I wonder if we need to re-evaluate the simulation metrics. I've looked at all the algorithms, and they all produce different plots. I've added an example image that highlights 2 differences from the following code:
finchMod <- cooc_null_model(dataWiFinches, algo="sim5",nReps=1000)
plot(finchMod,type="cooc")
Hi @emhart :
Thanks for looking at this. From my corner of the world (where it was -27 last night) the problem is in the plotting, not in the algorithms. I carefully checked sim1
, sim2
, and sim3
with small sample data sets and with West Indian finches.These algorithms are all working properly, and they do the following:
sim1
= reshuffle all matrix elements
sim2
= reshuffle within rows
sim3
= reshuffle within columns
Here is a little test matrix:
A <- c(1,1,1,1,1)
B <- c(1,0,0,0,0)
C <- c(1,1,0,0,0)
D <- c(1,0,0,0,1)
myMatrix <- rbind(A,B,C,D)
print(myMatrix)
[,1] [,2] [,3] [,4] [,5]
A 1 1 1 1 1
B 1 0 0 0 0
C 1 1 0 0 0
D 1 0 0 0 1
To avoid any complications with empty rows, let's run sim2
which reshuffles within each row:
myCheck <- sim2(myMatrix)
print(myCheck)
[,1] [,2] [,3] [,4] [,5]
A 1 1 1 1 1
B 0 0 0 1 0
C 1 0 0 0 1
D 1 0 1 0 0
These results for one random matrix look just as they should for sim2
. But now let's run this
as the null model and plot the results:
myModel <- cooc_null_model(myMatrix, algo="sim2")
summary(myModel)
Time Stamp: Tue Feb 24 09:46:20 2015
Reproducible: FALSE
Number of Replications:
Elapsed Time: 0.22 secs
Metric: c_score
Algorithm: sim2
Observed Index: 0.33333
Mean Of Simulated Index: 0.239
Variance Of Simulated Index: 0.022568
Lower 95% (1-tail): 0
Upper 95% (1-tail): 0.33333
Lower 95% (2-tail): 0
Upper 95% (2-tail): 0.33333
P(Obs <= null) = 1
P(Obs >= null) = 0.717
P(Obs = null) = 0.717
Standardized Effect Size (SES): 0.62794
plot(myModel,type="cooc")
This is definitely wrong for the observed, which now has an empty row and a missing column! The simulated looks the same, but with the empty row stripped out. This algorithm plotted matrices correctly when I originally coded it. To check it, I pulled out the following lines of code from the plot.coocnullmod
function and ran them from the prompt:
# reverse the matrix rows for plotting consistency
m <- MyMatrix # insert my test matrix here
m <- m[rev(1:nrow(m)),]
# setup plotting space
plot(m,xlim=c(0,ncol(m)),ylim=c(0,nrow(m)),type="n",
ann=FALSE,axes=FALSE)
mtext("Sites",side=1,font=2)
mtext("Species",side=2,font=2)
mtext("Observed",side=3,font=2,col="red3")
# define coordinate vectors
yrec <- rep(0:(nrow(m)-1),ncol(m))
xrec <- rep(0:(ncol(m)-1),each=nrow(m))
# Set up color labels
Plot.cols <- c("white","red3")
Color.Vector <- Plot.cols[as.integer(m)+1]
# Plot and fill rectangles
rect(xrec,yrec,xrec+1,yrec+1,col=Color.Vector)
So, when I pull this code outside of the function, it correctly plots the observed matrix. Thus something is happening when the algorithm and data are getting passed down to this function. I ran this check inside and outside of dev_tools
and got the same result. This does not solve the problem, but it at least isolates where it is occurring. If you could please check that code, I would be grateful. It appears that both the algorithm and the plotting function are doing what they should, but perhaps they are not receiving the correct data? Thanks!
Thanks for all the details @ngotelli, I see the problem now. I was just checking that it plotted 2 different matrices. I'll track down the bug tonight after work.
This fixes everything! The inequality for the fork was incorrect, so it was plotting sim9 for no matter what the algorithm. And I forgot about the row labels, so my test matrix was stripping a column and it looked funny. There is a secondary issue that I will explain in a separate post.
I will push a change now...
# if(is.na(nullmodObj$burn.in)){ # <- HERE IS THE ERROR
if(nullmodObj$Algorithm!="sim9"){ #<- CORRECTED CODE
Fun.Alg <- get(nullmodObj$Algorithm)
} else {
Fun.Alg <- sim9_single
}
One.Null.Matrix <- Fun.Alg(nullmodObj$Data)
Although the stats look correct for all of the different co-occurrence algorithms, this graph is not. It keeps showing the two data matrices as almost identical, no matter what algorithm is used. Note the contrast with the appearance of the simulated matrix in the Quick Start vignette.