Learn R Programming

popkin (version 1.2.2)

inbr_diag: Replace kinship diagonal with inbreeding coefficients

Description

The usual kinship matrix contains self-kinship values \(\phi_{jj}^T = \frac{1}{2}(1+f_j^T)\) where \(f_j^T\) are inbreeding coefficients. This function returns a modified kinship matrix with each \(\phi_{jj}^T\) replaced with \(f_j\) (off-diagonal \(j \ne k\) values stay the same). The resulting matrix is better for visualization, but is not appropriate for modeling (e.g. in mixed-effects models for association or heritability estimation).

Usage

inbr_diag(kinship)

Arguments

kinship

A kinship matrix with self-kinship values along the diagonal. Can pass multiple kinship matrices contained in a list.

Value

The modified kinship matrix, with inbreeding coefficients along the diagonal, preseving column and row names. If the input was a list of kinship matrices, the output is the corresponding list of transformed matrices.

See Also

The inverse function is given by coanc_to_kinship.

Examples

Run this code
# NOT RUN {
#########
# illustrate the main transformation on a 2x2 kinship matrix:
# same inbreeding values for both individuals
inbr <- 0.2
# corresponding self kinship (diagonal values) for both individuals
kinship_self <- (1 + inbr)/2
# kinship between the two individuals
kinship_between <- 0.1
# actual kinship matrix
kinship <- matrix(c(kinship_self, kinship_between, kinship_between, kinship_self), nrow=2)
# expected output of inbr_diag (replaces self kinship with inbreeding)
kinship_inbr_diag_exp <- matrix(c(inbr, kinship_between, kinship_between, inbr), nrow=2)
# actual output from this function
kinship_inbr_diag_obs <- inbr_diag(kinship)
# verify that they match (up to machine precision)
stopifnot( all( abs(kinship_inbr_diag_obs - kinship_inbr_diag_exp) < .Machine$double.eps ) )

# for a list of matrices, returns list of transformed matrices:
inbr_diag( list(kinship, kinship) )

#########
# Construct toy data (to more closely resemble real data analysis)
X <- matrix(c(0,1,2,1,0,1,1,0,2), nrow=3, byrow=TRUE) # genotype matrix
subpops <- c(1,1,2) # subpopulation assignments for individuals

# NOTE: for BED-formatted input, use BEDMatrix!
# "file" is path to BED file (excluding .bed extension)
## library(BEDMatrix)
## X <- BEDMatrix(file) # load genotype matrix object

# estimate the kinship matrix from the genotypes "X"!
kinship <- popkin(X, subpops) # calculate kinship from X and optional subpop labels

# lastly, replace diagonal of kinship matrix with inbreeding coefficients
kinship_inbr_diag <- inbr_diag(kinship)

# }

Run the code above in your browser using DataLab