phylo_average: Phylogenetically-weighted averages

View source: R/weights.R

phylo_averageR Documentation

Phylogenetically-weighted averages

Description

Calculates phylogenetic weights according to the ACL method (see ACL) and BranchManager method (see BM), for one or more reference trees, and applies the resulting sets of weights to one or more numerical variables which characterize the languages in the trees, resulting in phylogenetically-sensitive (i.e., phylogenetically-weighted) averages. If the data is in the form of binary {0,1} values, this is equivalent to a phylogenetically-sensitive proportion.

Usage

phylo_average(phy, data, na.rm = FALSE)

Arguments

phy

A phylo or multiPhylo object, containing one or more trees, representing one or more hypotheses on the phylogenetic relatedness of a set of languages.

data

A dataframe, containing data on the languages. Must contain a column tip, whose contents match the tip labels in phy, and at least one column of numerical data.

na.rm

A logical. Whether to ignore languages with NA values in the data. If TRUE then for each column of data, tips with NA values are ignored and the weights of the remaining tips are re-scaled to sum to 1.

Details

The function returns a list, containing: phy, the input tree(s); data, the input dataframe; and dataframes ACL_weights, BM_weights, ACL_averages, BM_averages. Dataframe ACL_weights contains one column for each tree in phy. In those columns are the phylogenetic weights obtained using the ACL method. Additionally, ACL_weights contains all non-numeric columns of the input data dataframe. Dataframe BM_weights is similar but with phylogenetic weights obtained using the BM method. Dataframe ACL_averages has one row per tree and one column for each numerical column in data, and contains phylogenetically-sensitive averages obtained using the ACL method. Dataframe BM_averages is similar but with phylogenetically-sensitive averages obtained using the BM method.

Value

A list, containing phy, data, and dataframes: ACL_weights, BM_weights, ACL_averages, BM_averages

Examples


library(ape)

# A dataframe of language data, with columns: tip, is_SOV, n_consonants
dat <- data.frame(
  tip = c("A", "B", "C", "D"),
  is_SOV = c(1, 1, 1, 0),
  n_consonants = c(18, 20, 22, 40))
# Three trees, representing three genealogical hypotheses, that relate
# the same languages A,B,C,D:
trees <- c(
  read.tree(text = "(((A:1,B:1,C:1):1,D:2):0.3);"),
  read.tree(text = "(((A:0.2,B:0.2,C:0.2):1.8,D:2):0.3);"),
  read.tree(text = "(((A:1.8,B:1.8,C:1.8):0.2,D:2):0.3);"))
p_ave <- phylo_average(phy = trees, data = dat)
# Phylogenetic weights, calculated from the trees:
p_ave$ACL_weights
p_ave$BM_weights
# Phylogenetically-weighted averages, or, for binary data, phylogenetically-
# weighted proportions
p_ave$ACL_averages
p_ave$BM_averages

erichround/phyloWeights documentation built on Sept. 29, 2023, 9:49 a.m.