| Title: | Predicting Species Interactions from Phylogenetic Distance |
|---|---|
| Description: | Tools to model and predict host-parasite (and other pairwise species) interactions from phylogenetic distance under presence-only data. Provides pairwise host-sharing logistic models with cluster-aware bootstrap and slope comparison between clades, presence-only performance metrics (a threshold efficiency metric and a phylogenetic partial receiver operating characteristic (ROC) adapted from Peterson, Papes and Soberon (2008) <doi:10.1016/j.ecolmodel.2007.11.008>), positive-unlabeled host prediction, and leave-hosts-out validation. Methods build on Robles-Fernandez and Lira-Noriega (2017) <doi:10.3389/fams.2017.00017>. |
| Authors: | Angel Robles [aut, cre] (ORCID: <https://orcid.org/0000-0002-4674-4270>) |
| Maintainer: | Angel Robles <a.l.robles.fernandez@gmail.com> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-14 19:04:45 UTC |
| Source: | https://github.com/cran/phylopred |
Validates both inputs and reorders the columns of incidence to match
the column order of phydist.
align_phylopred_inputs(incidence, phydist)align_phylopred_inputs(incidence, phydist)
incidence |
A binary incidence matrix (rows = parasites, cols = hosts). |
phydist |
A square phylogenetic distance matrix among hosts. |
A named list with:
The incidence matrix with columns reordered to match phydist.
The (unchanged) phylogenetic distance matrix.
incidence <- prepare_incidence_matrix(beetleTreeInteractions) aligned <- align_phylopred_inputs(incidence, phy_dist)incidence <- prepare_incidence_matrix(beetleTreeInteractions) aligned <- align_phylopred_inputs(incidence, phy_dist)
A dataset of bark beetle species and their host tree genera. Original information is available in https://www.barkbeetles.info/
beetleTreeInteractionsbeetleTreeInteractions
A data frame with 565 rows and 2 variables:
Species of bark beetles
Genus of host tree of bark beetles
We use this interaction tables to construct an incidence matrix. This is a table representation of a bipartite graph
Runs the phylopred logistic regression n times using random focal host
sampling, returning a distribution of coefficient estimates.
bootstrap_phylopred_model(incidence, phydist, n = 1000, seed = NULL)bootstrap_phylopred_model(incidence, phydist, n = 1000, seed = NULL)
incidence |
A binary incidence matrix (rows = parasites, cols = hosts). |
phydist |
A square, numeric phylogenetic distance matrix among hosts. |
n |
Integer. Number of bootstrap iterations. Default 1000. |
seed |
Integer or NULL. Random seed for reproducibility. Default NULL. |
An S3 object of class "phylopred_bootstrap", a list with:
The matched call.
Number of bootstrap iterations.
The seed used.
A matrix of n rows x 12 columns (coefficient info
from each iteration).
A data.frame with the column-wise means (same 12 columns).
List with n_parasites and n_hosts.
fit_phylopred_model, predict_phylopred_probability
incidence <- prepare_incidence_matrix(beetleTreeInteractions) aligned <- align_phylopred_inputs(incidence, phy_dist) bootstrap_phylopred_model(aligned$incidence, aligned$phydist, n = 5, seed = 42)incidence <- prepare_incidence_matrix(beetleTreeInteractions) aligned <- align_phylopred_inputs(incidence, phy_dist) bootstrap_phylopred_model(aligned$incidence, aligned$phydist, n = 5, seed = 42)
Nonparametric bootstrap that resamples whole parasites (clusters) with
replacement and refits the logistic regression on each replicate. This
respects the non-independence of rows that share a parasite in a
prepare_pair_data data set, unlike a naive row-level
bootstrap. When a group column is present, resampling is stratified
within groups and the model includes a group-by-distance interaction.
cluster_bootstrap_phylopred( pairs, n_boot = 1000, seed = NULL, conf_level = 0.95 )cluster_bootstrap_phylopred( pairs, n_boot = 1000, seed = NULL, conf_level = 0.95 )
pairs |
A data.frame from |
n_boot |
Integer. Number of bootstrap replicates. Default 1000. |
seed |
Integer or NULL. Random seed for reproducibility. Default NULL. |
conf_level |
Confidence level for percentile intervals. Default 0.95. |
An object of class "phylopred_cluster_bootstrap", a list with:
Matrix of bootstrap coefficient draws (replicates x terms).
Coefficients of the model fitted to the full data.
Percentile confidence intervals per term.
Bootstrap settings.
prepare_pair_data, compare_phylopred_slopes
# Three parasites keep the example fast top <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1:3] sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% top, ] pairs <- prepare_pair_data(sub, phy_dist) cluster_bootstrap_phylopred(pairs, n_boot = 5, seed = 42)# Three parasites keep the example fast top <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1:3] sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% top, ] pairs <- prepare_pair_data(sub, phy_dist) cluster_bootstrap_phylopred(pairs, n_boot = 5, seed = 42)
Fits a single logistic regression with a group-by-phylogenetic-distance
interaction to a pairwise data set built with prepare_pair_data
and tests whether the slopes of the two groups differ. A more negative
slope means the probability of sharing a parasite decays faster with
phylogenetic distance, i.e. the clade is more of a phylogenetic specialist.
Uncertainty for the slope difference comes from a parasite-level cluster
bootstrap.
compare_phylopred_slopes(pairs, n_boot = 1000, seed = NULL, conf_level = 0.95)compare_phylopred_slopes(pairs, n_boot = 1000, seed = NULL, conf_level = 0.95)
pairs |
A data.frame from |
n_boot |
Integer. Number of cluster bootstrap replicates. Default 1000. |
seed |
Integer or NULL. Random seed for reproducibility. Default NULL. |
conf_level |
Confidence level. Default 0.95. |
An object of class "phylopred_slope_comparison", a list with:
The two group labels (reference first).
Named vector with the slope of each group.
Slope of the second group minus the reference.
Wald p-value of the interaction term.
The cluster_bootstrap_phylopred object.
Bootstrap percentile CIs for each group's slope and for the difference.
The underlying glm object.
prepare_pair_data, cluster_bootstrap_phylopred
# Compare Xyleborus beetles against other genera (subset kept small # so the bootstrap example runs quickly) parasites <- unique(as.character(beetleTreeInteractions[[1]])) keep <- c(grep("^Xyleborus", parasites, value = TRUE)[1:3], parasites[!grepl("^Xyleborus", parasites)][1:3]) sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% keep, ] pairs <- prepare_pair_data( sub, phy_dist, group = function(x) ifelse(grepl("^Xyleborus", x), "Xyleborus", "other") ) compare_phylopred_slopes(pairs, n_boot = 5, seed = 42)# Compare Xyleborus beetles against other genera (subset kept small # so the bootstrap example runs quickly) parasites <- unique(as.character(beetleTreeInteractions[[1]])) keep <- c(grep("^Xyleborus", parasites, value = TRUE)[1:3], parasites[!grepl("^Xyleborus", parasites)][1:3]) sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% keep, ] pairs <- prepare_pair_data( sub, phy_dist, group = function(x) ifelse(grepl("^Xyleborus", x), "Xyleborus", "other") ) compare_phylopred_slopes(pairs, n_boot = 5, seed = 42)
Computes goodness-of-fit and discrimination metrics for a logistic
host-sharing model fitted to a prepare_pair_data data set:
McFadden and Tjur pseudo-R-squared, the area under the ROC curve (AUC),
and optionally a grouped cross-validated AUC where whole parasites are
held out (parasite-grouped k-fold), which measures the ability to predict
the host range of unseen parasites.
evaluate_phylopred_model(pairs, cv = FALSE, k = 5, seed = NULL)evaluate_phylopred_model(pairs, cv = FALSE, k = 5, seed = NULL)
pairs |
A data.frame from |
cv |
Logical. If TRUE, also compute the parasite-grouped cross-validated AUC. Default FALSE (refitting can be slow on large data sets). |
k |
Integer. Number of cross-validation folds. Default 5. |
seed |
Integer or NULL. Random seed for fold assignment. Default NULL. |
An object of class "phylopred_evaluation", a list with:
McFadden pseudo-R-squared, .
Tjur coefficient of discrimination (mean fitted probability for 1s minus mean for 0s).
In-sample area under the ROC curve.
Parasite-grouped k-fold cross-validated AUC (NA if
cv = FALSE).
The underlying glm object.
Data set size.
prepare_pair_data, cluster_bootstrap_phylopred
# Three parasites keep the example fast top <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1:3] sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% top, ] pairs <- prepare_pair_data(sub, phy_dist) evaluate_phylopred_model(pairs) evaluate_phylopred_model(pairs, cv = TRUE, k = 3, seed = 42)# Three parasites keep the example fast top <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1:3] sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% top, ] pairs <- prepare_pair_data(sub, phy_dist) evaluate_phylopred_model(pairs) evaluate_phylopred_model(pairs, cv = TRUE, k = 3, seed = 42)
Returns a named list of model coefficients and their associated statistics
from a phylopred_model or phylopred_bootstrap object.
extract_phylopred_coefficients(model)extract_phylopred_coefficients(model)
model |
A |
A named list with elements:
Intercept coefficient (mean for bootstrap).
Slope coefficient (mean for bootstrap).
Standard error of intercept.
Standard error of slope.
Two-element vector with 2.5% and 97.5% CI for intercept.
Two-element vector with 2.5% and 97.5% CI for slope.
inc <- prepare_incidence_matrix(beetleTreeInteractions) al <- align_phylopred_inputs(inc, phy_dist) model <- fit_phylopred_model(al$incidence, al$phydist, seed = 42) extract_phylopred_coefficients(model)inc <- prepare_incidence_matrix(beetleTreeInteractions) al <- align_phylopred_inputs(inc, phy_dist) model <- fit_phylopred_model(al$incidence, al$phydist, seed = 42) extract_phylopred_coefficients(model)
A phylogenetic tree (class phylo) of 625 marine fish species that
are recorded hosts of sea lice of the genera Caligus and
Lepeophtheirus in the cofid copepod-fish interaction
database. The tree is pruned from the Fish Tree of Life
actinopterygian phylogeny (Rabosky and colleagues 2018,
actinopt_12k_raxml).
Tip labels are binomial species names with spaces.
fish_treefish_tree
A phylo object with 625 tips.
https://fishtreeoflife.org/. The underlying data are released under the CC0 1.0 public domain dedication (Dryad, doi:10.5061/dryad.fc71cp4). Please cite Rabosky, D. L. and colleagues (2018) An inverse latitudinal gradient in speciation rate for marine fishes. Nature 559, 392-395. doi:10.1038/s41586-018-0273-1
The cofid package for the interaction records.
Fits a logistic regression model relating host susceptibility to phylogenetic distance.
fit_phylopred_model(incidence, phydist, seed = NULL)fit_phylopred_model(incidence, phydist, seed = NULL)
incidence |
A binary incidence matrix (rows = parasites/pathogens, cols = hosts). |
phydist |
A square, numeric phylogenetic distance matrix among hosts.
Row/col names must match the column names of |
seed |
Integer or NULL. Random seed for reproducibility. Default NULL. |
An S3 object of class "phylopred_model", a list with:
The matched call.
Named numeric vector c(intercept, slope).
Named numeric vector c(intercept, slope).
Matrix with rows c("intercept","slope") and
cols c("2.5%","97.5%").
The underlying glm object.
The data.frame used for model fitting.
Logical; whether the GLM converged.
List with n_parasites, n_hosts, and seed.
bootstrap_phylopred_model, predict_phylopred_probability
incidence <- prepare_incidence_matrix(beetleTreeInteractions) aligned <- align_phylopred_inputs(incidence, phy_dist) fit_phylopred_model(aligned$incidence, aligned$phydist, seed = 42)incidence <- prepare_incidence_matrix(beetleTreeInteractions) aligned <- align_phylopred_inputs(incidence, phy_dist) fit_phylopred_model(aligned$incidence, aligned$phydist, seed = 42)
Predicts the hosts of a parasite from the phylogeny using
positive-unlabeled (PU) learning, as a parallel alternative to the
pairwise logistic model. Known hosts are treated as positives and every
other species in the phylogeny as unlabeled (never as a true absence).
Each species is described by phylogenetic features and the PLUS
algorithm (xplus::xplus) iteratively re-labels the unlabeled
species while fitting a penalized logistic model.
fit_phylopred_pu( interactions, phydist, parasite, n_eigen = 10, seed = NULL, ... )fit_phylopred_pu( interactions, phydist, parasite, n_eigen = 10, seed = NULL, ... )
interactions |
A data.frame whose first two columns are the
parasite and host species names (as in
|
phydist |
Square numeric matrix of phylogenetic distances with species names as row and column names. |
parasite |
Character scalar: the parasite whose hosts are predicted. |
n_eigen |
Number of PCoA axes used as features. Default 10. |
seed |
Optional random seed passed to |
... |
Further arguments passed to |
The feature matrix contains, for every species in phydist:
the minimum and mean phylogenetic distance to the parasite's known hosts (excluding the species itself), and
the first n_eigen principal coordinates (PCoA) of the
phylogenetic distance matrix, describing the global position of the
species in the phylogeny (the analogue of environmental layers in
an SDM).
An object of class "phylopred_pu": a list with
data.frame with species, pred
(predicted host suitability) and is_host.
The underlying xplus object.
The parasite name.
The prediction columns can be passed directly to
host_threshold_metric and
phylo_partial_roc.
host_threshold_metric,
phylo_partial_roc, prepare_pair_data
pu <- fit_phylopred_pu(beetleTreeInteractions, phy_dist, parasite = beetleTreeInteractions[[1]][1], max_iter = 20, seed = 1) host_threshold_metric(pu$prediction$pred, pu$prediction$is_host)pu <- fit_phylopred_pu(beetleTreeInteractions, phy_dist, parasite = beetleTreeInteractions[[1]][1], max_iter = 20, seed = 1) host_threshold_metric(pu$prediction$pred, pu$prediction$is_host)
Applies poulin_std to every parasite in an interaction
table, following the same expansion logic as
prepare_pair_data. Useful for contrasting the Poulin and
Mouillot (2003) specificity index against phylopred's
model-based host-range metric b*
(host_threshold_metric) across many parasites.
host_specificity_index(interactions, phydist, group = NULL)host_specificity_index(interactions, phydist, group = NULL)
interactions |
A data.frame with at least two columns: the first the parasite (or symbiont) species, the second the host species. Extra columns are ignored. Duplicated rows are removed. |
phydist |
A square, numeric distance matrix among hosts (taxonomic steps or phylogenetic distances), with matching row and column names. |
group |
Optional. Either the name of a column in |
A data.frame of class "phylopred_specificity" with columns
parasite, n_hosts (known hosts present in phydist),
std (), var_std (), and
group (only if group is supplied).
Poulin, R. and Mouillot, D. (2003) Parasite specialization from a phylogenetic perspective: a new index of host specificity. Parasitology 126, 473-480. doi:10.1017/S0031182003002993
poulin_std, prepare_pair_data,
host_threshold_metric
std_by_parasite <- host_specificity_index(beetleTreeInteractions, phy_dist) head(std_by_parasite)std_by_parasite <- host_specificity_index(beetleTreeInteractions, phy_dist) head(std_by_parasite)
Computes the threshold-dependent performance metric
where, for a cutoff on the predicted host-suitability scores:
a(t)proportion of known hosts with prediction
(sensitivity / recall);
b(t)proportion of ALL species in the phylogeny with
prediction (predicted host breadth, the phylogenetic
analogue of predicted area in SDM threshold metrics);
c(t) = 1 - a(t)proportion of known hosts lost (omission).
The metric is designed for presence-only / positive-unlabeled settings:
it never uses absences, and gamma tunes the trade-off between
retaining known hosts and predicting a compact host set. The maximum of
gives an optimal cutoff .
host_threshold_metric(pred, is_host, gamma = 1)host_threshold_metric(pred, is_host, gamma = 1)
pred |
Numeric vector of predicted suitability scores, one per species in the phylogeny (any real values; typically probabilities). |
is_host |
Logical or 0/1 vector of the same length: TRUE/1 for species that are known hosts (presences). |
gamma |
Positive scalar weight on the predicted breadth term. Default 1. |
An object of class "phylopred_threshold_metric": a data.frame
with columns threshold, a, b, c, and
f_gamma, evaluated at every distinct prediction value. The
optimal cutoff is stored in attributes "optimal_threshold" and
"optimal_f", and gamma in attribute "gamma".
phylo_partial_roc, evaluate_phylopred_model
pairs <- prepare_pair_data(beetleTreeInteractions, phy_dist) fit <- stats::glm(suscept ~ phydist, data = pairs, family = stats::binomial()) # Suitability of every host species for one focal parasite one <- pairs[pairs$parasite == pairs$parasite[1], ] one <- one[!duplicated(one$target), ] pred <- stats::predict(fit, newdata = one, type = "response") m <- host_threshold_metric(pred, one$suscept, gamma = 1) plot(m)pairs <- prepare_pair_data(beetleTreeInteractions, phy_dist) fit <- stats::glm(suscept ~ phydist, data = pairs, family = stats::binomial()) # Suitability of every host species for one focal parasite one <- pairs[pairs$parasite == pairs$parasite[1], ] one <- one[!duplicated(one$target), ] pred <- stats::predict(fit, newdata = one, type = "response") m <- host_threshold_metric(pred, one$suscept, gamma = 1) plot(m)
Data of phylogenetic distance matrix for bark beetles host species
phy_distphy_dist
A matrix with 235 rows and 235 columns. Contain Phylogenetic distance matrix between bark beetle host genera
Partial ROC test for presence-only host-suitability predictions,
following Peterson, Papes & Soberon (2008, Ecological Modelling 213,
63-72). The ROC space is redefined for presence-only data: the y axis is
the sensitivity (proportion of known hosts predicted at
cutoff ) and the x axis is the proportion of ALL species in the
phylogeny predicted as hosts, (the analogue of proportional
predicted area). The curve is restricted to the low-omission region
for a user-chosen omission tolerance error_rate
, and performance is summarized as the ratio of the partial AUC of
the observed curve to the partial AUC of the null (random-ranking)
expectation, the diagonal . A ratio of 1 equals random
performance; ratios above 1 indicate predictive signal. Significance is
assessed by bootstrap resampling of the known hosts.
phylo_partial_roc( pred, is_host, error_rate = 0.05, n_boot = 500, boot_prop = 0.5, seed = NULL )phylo_partial_roc( pred, is_host, error_rate = 0.05, n_boot = 500, boot_prop = 0.5, seed = NULL )
pred |
Numeric vector of predicted suitability scores, one per species in the phylogeny. |
is_host |
Logical or 0/1 vector of the same length: TRUE/1 for known hosts. |
error_rate |
Allowed omission error |
n_boot |
Number of bootstrap replicates. Default 500. |
boot_prop |
Proportion of known hosts resampled (with replacement) in each bootstrap replicate, as in Peterson, Papes and Soberon (2008). Default 0.5. |
seed |
Integer or NULL. Random seed for the bootstrap. Default NULL. |
An object of class "phylopred_partial_roc", a list with:
Observed partial AUC ratio (observed / null).
Bootstrap proportion of replicates with ratio .
Vector of bootstrap AUC ratios.
data.frame with threshold, a (sensitivity)
and b (predicted breadth).
The configuration used.
pairs <- prepare_pair_data(beetleTreeInteractions, phy_dist) fit <- stats::glm(suscept ~ phydist, data = pairs, family = stats::binomial()) one <- pairs[pairs$parasite == pairs$parasite[1], ] one <- one[!duplicated(one$target), ] pred <- stats::predict(fit, newdata = one, type = "response") phylo_partial_roc(pred, one$suscept, n_boot = 50, seed = 42)pairs <- prepare_pair_data(beetleTreeInteractions, phy_dist) fit <- stats::glm(suscept ~ phydist, data = pairs, family = stats::binomial()) one <- pairs[pairs$parasite == pairs$parasite[1], ] one <- one[!duplicated(one$target), ] pred <- stats::predict(fit, newdata = one, type = "response") phylo_partial_roc(pred, one$suscept, n_boot = 50, seed = 42)
Computes the boundary of the joint confidence region ("ellipse") for a
pair of coefficients, either from a fitted glm (using the
asymptotic covariance matrix) or from a
cluster_bootstrap_phylopred object (using the empirical
covariance of the bootstrap draws, which respects parasite-level
clustering).
phylopred_confidence_ellipse( object, terms = c("(Intercept)", "phydist"), level = 0.95, n_points = 200 )phylopred_confidence_ellipse( object, terms = c("(Intercept)", "phydist"), level = 0.95, n_points = 200 )
object |
A |
terms |
Character vector of length 2 with the coefficient names.
Default |
level |
Confidence level. Default 0.95. |
n_points |
Number of points on the ellipse boundary. Default 200. |
A data.frame with n_points rows and two columns named after
terms, tracing the ellipse boundary. The center (point estimates)
is returned as the "center" attribute.
# Three parasites keep the example fast top <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1:3] sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% top, ] pairs <- prepare_pair_data(sub, phy_dist) # From a glm: asymptotic ellipse fit <- glm(suscept ~ phydist, data = pairs, family = binomial()) ell <- phylopred_confidence_ellipse(fit) plot(ell, type = "l") points(t(attr(ell, "center")), pch = 19) # From a cluster bootstrap boot <- cluster_bootstrap_phylopred(pairs, n_boot = 5, seed = 42) ell_b <- phylopred_confidence_ellipse(boot) lines(ell_b, lty = 2)# Three parasites keep the example fast top <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1:3] sub <- beetleTreeInteractions[ as.character(beetleTreeInteractions[[1]]) %in% top, ] pairs <- prepare_pair_data(sub, phy_dist) # From a glm: asymptotic ellipse fit <- glm(suscept ~ phydist, data = pairs, family = binomial()) ell <- phylopred_confidence_ellipse(fit) plot(ell, type = "l") points(t(attr(ell, "center")), pch = 19) # From a cluster bootstrap boot <- cluster_bootstrap_phylopred(pairs, n_boot = 5, seed = 42) ell_b <- phylopred_confidence_ellipse(boot) lines(ell_b, lty = 2)
Computes the average taxonomic (or phylogenetic) distinctness
of Poulin and Mouillot (2003) for a single parasite's known
host set:
where is the distance between hosts and
taken from phydist, and is the number of known
hosts. When phydist is a taxonomic step matrix (see
taxonomic_step_matrix), is the original
1-5 taxonomic index; when phydist is a matrix of phylogenetic
(patristic) distances, this is the continuous phylogenetic
generalization noted as possible by Poulin and Mouillot (2003, after
Clarke and Warwick, 2001), expressed directly in branch-length units.
The corresponding sampling variance
is also returned.
poulin_std(phydist, hosts)poulin_std(phydist, hosts)
phydist |
A square, numeric distance matrix among hosts with
matching row and column names (taxonomic steps from
|
hosts |
Character vector of known host names for one parasite.
Hosts not present in |
An object of class "phylopred_std": a list with elements
n_hosts, std (), and var_std
(, NA when n_hosts < 3).
is undefined (NA, with a warning) when n_hosts < 2, as
noted by Poulin and Mouillot (2003) for single-host parasites.
Poulin, R. and Mouillot, D. (2003) Parasite specialization from a phylogenetic perspective: a new index of host specificity. Parasitology 126, 473-480. doi:10.1017/S0031182003002993
Clarke, K.R. and Warwick, R.M. (2001) A further biodiversity index applicable to species lists: variation in taxonomic distinctness. Marine Ecology Progress Series 216, 265-278.
host_specificity_index,
taxonomic_step_matrix, host_threshold_metric
hosts <- unique(beetleTreeInteractions$genus[ beetleTreeInteractions$beetle == beetleTreeInteractions$beetle[1]]) poulin_std(phy_dist, hosts)hosts <- unique(beetleTreeInteractions$genus[ beetleTreeInteractions$beetle == beetleTreeInteractions$beetle[1]]) poulin_std(phy_dist, hosts)
Applies a logit function using intercept and slope coefficients to a vector
of phylogenetic distances. Accepts a numeric vector, a phylopred_model,
or a phylopred_bootstrap object.
predict_phylopred_probability(coef, dist)predict_phylopred_probability(coef, dist)
coef |
A named numeric vector with elements |
dist |
A numeric vector of phylogenetic distances. |
A named numeric vector of predicted probabilities of the same length
as dist.
inc <- prepare_incidence_matrix(beetleTreeInteractions) al <- align_phylopred_inputs(inc, phy_dist) model <- fit_phylopred_model(al$incidence, al$phydist, seed = 42) dist_vec <- seq(0, max(al$phydist), length.out = 50) predict_phylopred_probability(model, dist_vec)inc <- prepare_incidence_matrix(beetleTreeInteractions) al <- align_phylopred_inputs(inc, phy_dist) model <- fit_phylopred_model(al$incidence, al$phydist, seed = 42) dist_vec <- seq(0, max(al$phydist), length.out = 50) predict_phylopred_probability(model, dist_vec)
Builds an incidence (presence/absence) matrix from a two-column data.frame of species interactions.
prepare_incidence_matrix(db)prepare_incidence_matrix(db)
db |
A data.frame with exactly two columns (e.g., parasite and host). |
An incidence matrix (rows = first column unique values, cols = second column unique values) filled with 0/1.
prepare_incidence_matrix(beetleTreeInteractions)prepare_incidence_matrix(beetleTreeInteractions)
Expands an interaction table into all (focal host, target host) pairs per
parasite. For every parasite, each of its known hosts is used as a focal
host, and every other host in phydist is a target host whose
response is 1 if it is also a known host of that parasite and 0 otherwise.
prepare_pair_data(interactions, phydist, group = NULL)prepare_pair_data(interactions, phydist, group = NULL)
interactions |
A data.frame with at least two columns: the first the parasite (or symbiont) species, the second the host species. Extra columns are ignored. Duplicated rows are removed. |
phydist |
A square, numeric phylogenetic (or taxonomic) distance
matrix among hosts, with matching row and column names. Hosts in
|
group |
Optional. Either the name of a column in |
Unlike the legacy workflow (which samples a single focal host per parasite),
this uses every known host as a focal host, so no information is discarded
and results are deterministic. Rows belonging to the same parasite are not
independent; account for this with cluster_bootstrap_phylopred
when computing uncertainty.
A data.frame of class "phylopred_pairs" with columns:
Parasite species (cluster identifier).
Focal (known) host.
Target host.
Distance between focal and target host.
1 if the target host is a known host of the parasite.
Grouping factor (only if group is supplied).
fit_phylopred_model, cluster_bootstrap_phylopred,
compare_phylopred_slopes
pairs <- prepare_pair_data(beetleTreeInteractions, phy_dist) head(pairs)pairs <- prepare_pair_data(beetleTreeInteractions, phy_dist) head(pairs)
Constructs a symmetric taxonomic distance matrix among species from a
taxonomic hierarchy table, following the step scale of
Poulin and Mouillot (2003): 1 if two species share a genus, 2 if they
share a family but not a genus, 3 if they share an order but not a
family, 4 if they share a class but not an order, and 5 if they share
none of the above (different class / phylum). Any subset of
genus, family, order, class may be supplied;
missing ranks are skipped and the maximum step for that pair falls back
to length(ranks) + 1.
taxonomic_step_matrix(tax_table)taxonomic_step_matrix(tax_table)
tax_table |
A data.frame with one row per species and columns
|
A square numeric matrix with row/column names equal to
tax_table$species, diagonal 0, suitable as the phydist
argument of poulin_std or host_specificity_index.
Poulin, R. and Mouillot, D. (2003) Parasite specialization from a phylogenetic perspective: a new index of host specificity. Parasitology 126, 473-480. doi:10.1017/S0031182003002993
poulin_std, host_specificity_index
tax <- data.frame( species = c("Caligus elongatus", "Caligus curtus", "Lepeophtheirus salmonis", "Ergasilus sieboldi"), genus = c("Caligus", "Caligus", "Lepeophtheirus", "Ergasilus"), family = c("Caligidae", "Caligidae", "Caligidae", "Ergasilidae"), order = c("Siphonostomatoida", "Siphonostomatoida", "Siphonostomatoida", "Cyclopoida") ) taxonomic_step_matrix(tax)tax <- data.frame( species = c("Caligus elongatus", "Caligus curtus", "Lepeophtheirus salmonis", "Ergasilus sieboldi"), genus = c("Caligus", "Caligus", "Lepeophtheirus", "Ergasilus"), family = c("Caligidae", "Caligidae", "Caligidae", "Ergasilidae"), order = c("Siphonostomatoida", "Siphonostomatoida", "Siphonostomatoida", "Cyclopoida") ) taxonomic_step_matrix(tax)
Presence-only predictive validation: a fraction of the parasite's known hosts is hidden, the model is refitted on the remaining hosts, and the hidden hosts are scored against the unlabeled species. Because true absences do not exist, performance is measured as the AUC of the hidden (held-out) hosts versus the unlabeled species — the probability that a truly interacting species outranks a random unlabeled one. This measures actual predictive ability, not in-sample fit.
validate_host_prediction( interactions, phydist, parasite, method = c("glm", "pu"), prop_holdout = 0.25, n_rep = 10, seed = NULL, ... )validate_host_prediction( interactions, phydist, parasite, method = c("glm", "pu"), prop_holdout = 0.25, n_rep = 10, seed = NULL, ... )
interactions |
A data.frame whose first two columns are the parasite and host species names. |
phydist |
Square numeric matrix of phylogenetic distances with species names as row and column names. |
parasite |
Character scalar: the parasite whose hosts are predicted. |
method |
|
prop_holdout |
Proportion of known hosts hidden in each replicate. Default 0.25. |
n_rep |
Number of holdout replicates. Default 10. |
seed |
Optional random seed. |
... |
Further arguments passed to |
An object of class "phylopred_holdout": a list with
auc (vector of holdout AUCs, one per replicate),
mean_auc, method, parasite,
prop_holdout, and n_rep.
fit_phylopred_pu, host_threshold_metric,
phylo_partial_roc
parasite <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1] validate_host_prediction(beetleTreeInteractions, phy_dist, parasite, method = "glm", n_rep = 3, seed = 1)parasite <- names(sort(table(beetleTreeInteractions[[1]]), decreasing = TRUE))[1] validate_host_prediction(beetleTreeInteractions, phy_dist, parasite, method = "glm", n_rep = 3, seed = 1)