avg_kinship_subpops: Calculate a kinship matrix between subpopulations by...

View source: R/avg_kinship_subpops.R

avg_kinship_subpopsR Documentation

Calculate a kinship matrix between subpopulations by averaging individual data

Description

This function calculates a kinship matrix between subpopulations, whose values are the average kinship values between all individual pairs where one individual is in the first subpopulation and the other individual is in the second subpopulation. To estimate coancestry instead of kinship, which is recommended to get more interpretable diagonal values, the input kinship matrix should be transformed using inbr_diag().

Usage

avg_kinship_subpops(kinship, subpops, subpop_order = unique(subpops))

Arguments

kinship

A symmetric n-by-n kinship matrix.

subpops

The length-n vector of subpopulation assignments for each individual.

subpop_order

The optional order of subpopulations in the output matrix. subpop_order must contain every unique subpopulation in subpops. Any additional subpopulations in subpop_order (missing in subpops) are ignored. By default, subpopulations are in the order of first appearance in subpops.

Value

The symmetric K-by-K kinship matrix between subpopulations, where K is the number of unique subpopulations in subpops, ordered as in subpop_order.

Examples

# a toy kinship matrix with 5 individuals belonging to 2 subpopulations
kinship <- matrix(
    c(
        0.7, 0.4, 0.4, 0.1, 0.0,
        0.4, 0.7, 0.4, 0.2, 0.1,
        0.4, 0.4, 0.7, 0.2, 0.0,
        0.1, 0.2, 0.2, 0.6, 0.1,
        0.0, 0.1, 0.0, 0.1, 0.6
    ),
    nrow = 5,
    ncol = 5
)
subpops <- c(1, 1, 1, 2, 2)

# calculate mean kinship between (and within) subpopulations
# a 2x2 matrix
avg_kinship_subpops( kinship, subpops )

# calculate coancestry estimate instead (difference is diagonal)
avg_kinship_subpops( inbr_diag( kinship ), subpops )


popkin documentation built on Jan. 7, 2023, 1:26 a.m.