Learn R Programming

CooccurrenceAffinity

Short summary

An R package for computing affinity metrics between co-occurring entities using presence/absence data. Key functions include:

  • affinity(data, row.or.col, ...): compute pairwise affinity statistics (MLE of alpha, expected co-occurrence, p-values, and optional square matrices) from presence/absence data
  • AlphInts(x, mA_mB_N, lev, pvalType, ...): calculate various confidence intervals for alpha, including conservative (Clopper–Pearson, Blaker) and mid-P methods
  • ML.Alpha(x, mA_mB_N, lev, ...): compute the maximum likelihood estimate of alpha and related likelihood-based metrics for co-occurrence counts
  • plotgg(data, variable, legendlimit, ...): create configurable ggplot2 heatmaps of affinity outputs with customizable legends, text size, and color scales

Quick install & start

The library can be installed from CRAN or GitHub.

# CRAN version
install.packages("CooccurrenceAffinity")
library(CooccurrenceAffinity)

# Load example data and compute affinity
data(finches)
res <- affinity(data = finches, row.or.col = "col")
CovrgPlot(marg = c(50,70,150), lev = 0.95)
plotgg(data = res, variable = "alpha_mle", legendlimit = "balanced")

Copy Link

Version

Install

install.packages('CooccurrenceAffinity')

Monthly Downloads

241

Version

1.0.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Kumar Mainali

Last Published

June 27th, 2025

Functions in CooccurrenceAffinity (1.0.2)

midP.EHyp

midP.EHyp computation
minmaxAlpha.pFNCH

integer-endpoint of range for which BiasedUrn::pFNCHHypergeo() works without error
MinX.Int

MinX.Int computation
affinity2by2

Maximum likelihood estimate and intervals of alpha, null expectation, p-value and traditional indices from a 2x2 table
affinity

Computes alpha, probability, expected co-occurence, median interval, various confidence intervals, other indices of affinity, etc.
dataprep

Occurrence matrix (e.g., species by site) data preparation for affinity() function
CovrgPlot

Coverage probabilities of the confidence intervals, calculated and plotted
Bisect

Bisections for finding a root of a function
AlphInts

Median interval, four confidence intervals, null expectation of cooccurrence count, and p-value
logLikExtHyp

log of Extended Hypergeometric Likelihiood at (X, mA,mB,N, alpha)
AcceptAffCI

Acceptability Interval
EHypQuInt

Interval of alpha values for which X is a specified q'th quantile
ML.Alpha

Maximum likelihood estimate and intervals of alpha, null expectation and p-value of a 2x2 table
Covrg

Coverage Probabilities for Confidence Intervals about alpha, for fixed true alpha
MaxX.Int

MaxX.Int computation
AcceptAffin

Calculates the "Acceptability Function" used in defining Blaker's (2000) Acceptability Interval and computing the latter in the function AcceptAffCI().
EHypMidP

Quantile of the Extended Hypergeometric distribution approximated by the midP distribution function
plotgg

Heatmap plot of affinity() output
finches

Darwin’s finches presence–absence data