draw_p_subpops: Draw allele frequencies for independent subpopulations

Description Usage Arguments Value See Also Examples

View source: R/draw_p_subpops.R

Description

The allele frequency matrix P for m_loci loci (rows) and k_subpops independent subpopulations (columns) are drawn from the Balding-Nichols distribution with ancestral allele frequencies p_anc and FST parameters inbr_subpops equivalent to P[ i, j ] <- rbeta( 1, nu_j * p_anc[i], nu_j * ( 1 - p_anc[i] ) ), where nu_j <- 1 / inbr_subpops[j] - 1. The actual function is more efficient than the above code.

Usage

1
draw_p_subpops(p_anc, inbr_subpops, m_loci = NA, k_subpops = NA)

Arguments

p_anc

The scalar or length-m_loci vector of ancestral allele frequencies per locus.

inbr_subpops

The scalar or length-k_subpops vector of subpopulation FST values.

m_loci

If p_anc is scalar, optionally provide the desired number of loci (lest only one locus be simulated). Stops if both length(p_anc) > 1 and m_loci is not NA and they disagree.

k_subpops

If inbr_subpops is a scalar, optionally provide the desired number of subpopulations (lest a single subpopulation be simulated). Stops if both length(inbr_subpops) > 1 and k_subpops is not NA and they disagree.

Value

The m_loci-by-k_subpops matrix of independent subpopulation allele frequencies. If p_anc is length-m_loci with names, these are copied to the row names of this output matrix. If inbr_subpops is length-k_subpops with names, these are copied to the column names of this output matrix.

See Also

draw_p_subpops_tree() for version for subpopulations related by a tree, which can therefore be non-independent.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# a typical, non-trivial example
# number of loci
m_loci <- 10
# random vector of ancestral allele frequencies
p_anc <- draw_p_anc(m_loci)
# FST values for two subpops
inbr_subpops <- c(0.1, 0.3)
# matrix of intermediate subpop allele freqs
p_subpops <- draw_p_subpops(p_anc, inbr_subpops)

# special case of scalar p_anc
p_subpops <- draw_p_subpops(p_anc = 0.5, inbr_subpops, m_loci = m_loci)
stopifnot ( nrow( p_subpops ) == m_loci )

# special case of scalar inbr_subpops
k_subpops <- 2
p_subpops <- draw_p_subpops(p_anc, inbr_subpops = 0.2, k_subpops = k_subpops)
stopifnot ( ncol( p_subpops ) == k_subpops )

# both main parameters scalars but return value still matrix
p_subpops <- draw_p_subpops(p_anc = 0.5, inbr_subpops = 0.2, m_loci = m_loci, k_subpops = k_subpops)
stopifnot ( nrow( p_subpops ) == m_loci )
stopifnot ( ncol( p_subpops ) == k_subpops )

# passing scalar parameters without setting dimensions separately results in a 1x1 matrix
p_subpops <- draw_p_subpops(p_anc = 0.5, inbr_subpops = 0.2)
stopifnot ( nrow( p_subpops ) == 1 )
stopifnot ( ncol( p_subpops ) == 1 )

bnpsd documentation built on Aug. 25, 2021, 5:07 p.m.