A new package called ‘backbone’ recently dropped on CRAN and I’m super stoked about it. Essentially, the package simplifies a co-occurrence matrix by extracting its ‘backbone.’ The package offers various methods for extracting a backbone, one of which involves the use of the hypergeometric distribution to assess whether the co-occurrence is significantly less than or greater than expected. Since the ‘cooccur‘ package does the same thing, I thought, “Hmmm…Which one’s faster?” And that, my dear friends, is how this battle of the CRAN packages began. Without further ado, let’s start this PACKAGE RACE!!!
Preparing the track
For convenience, we’ll use the finches data set from the ‘cooccur’ package to test each package’s respective function. To monitor speed, we’ll use ‘microbenchmark‘ which is more accurate than using system.time(). microbenchmark() will run each expression 100 times, randomizing which expression is being run each time.
On your marks, get set, GO!
library(cooccur)
library(backbone)
library(microbenchmark)
library(plotly)
# Load finches data set from 'cooccur'.
data(finches)
# Compare speed of 'cooccur' v 'backbone'.
set.seed(123)
speedTest <- microbenchmark(
co_cooccur <- cooccur(finches),
co_backbone <- hyperg(as.matrix(finches))
)

And the winner is…
# Rename factors.
levels(speedTest$expr) <- c('cooccur', 'backbone')
# Plot.
plot_ly(data = speedTest,
y = ~expr, x = ~(time/1000000),
color = ~expr, colors = c("#D3D3D3", "#FBCB23"),
type = "box") %>%
layout(showlegend = F,
title = "And the winner is...backbone!",
xaxis = list(range = c(0, 275), title = 'time(ms)'),
yaxis = list(title = ''))

The result: ‘backbone‘ wins by a landslide! If it’s speed you’re after, ‘backbone‘ is your package. If you’re using ‘cooccur‘ and haven’t noticed any issues with speed, then keep using it. Speed is not the only thing that defines a package. But, if speed is an issue, you might want to switch to ‘backbone.’ You should see noticeable improvements in run time.
Great race, packages!

References
Domagalski R, Neal Z, Sagan B (2020). backbone: Extracts the Backbone from Weighted Graphs. R package version 1.2.2, https://CRAN.R-project.org/package=backbone.
Griffith, Daniel M., Veech, Joseph A., & Marsh, Charles J. (2016).
cooccur: Probabilistic Species Co-Occurrence Analysis in R. Journal
of Statistical Software, 69(2), 1-17. doi:10.18637/jss.v069.c02
Leave a reply, but don't be rude!