Skip to contents

Calculates summaries for Pop-class objects and recursively aggregates them across a nested MultiPop-class object.

Usage

calcPopSummary(
  x,
  withinPopFUN,
  withinPopArgs = list(),
  acrossPopFUN,
  acrossPopArgs = list(),
  level = 0
)

Arguments

x

A Pop-class, HybridPop-class or MultiPop-class object. A MultiPop can only hold Pop and MultiPop objects, so a HybridPop is only accepted on its own.

withinPopFUN

a function used to calculate a summary for each Pop (or HybridPop, when x is one) object. The function must accept a Pop object as its first argument and return a numeric vector or one-row matrix.

withinPopArgs

a list of additional arguments passed to withinPopFUN

acrossPopFUN

a function used to aggregate summaries across the immediate children of each MultiPop object. The function must accept a numeric matrix as its first argument and return a numeric vector or one-row matrix with the same columns.

acrossPopArgs

a list of additional arguments passed to acrossPopFUN

level

an integer indicating the nesting level at which aggregation stops. See Details.

Value

If x is a Pop object, a numeric vector containing its summary.

If x is a MultiPop object and level = 0, a numeric vector containing the summary aggregated across the complete object.

If x is a MultiPop object and level > 0, a numeric matrix with one row per population unit at the requested level. The matrix has a "source" attribute describing the origin of each row in the population hierarchy.

Details

This function performs two operations. First, withinPopFUN is applied to every terminal Pop object. Second, the resulting summaries are recursively aggregated with acrossPopFUN through the nested MultiPop structure.

The rows of the matrix supplied to acrossPopFUN correspond to the immediate children of a MultiPop. Aggregation is therefore unweighted with respect to the number of individuals in each child population, unless weighting is explicitly implemented by the user-supplied function.

The level argument determines where recursive aggregation stops:
- level = 0 aggregates summaries across the complete object.
- level = 1 returns one summary for each immediate child of x.
- Larger values return summaries from progressively deeper levels of the hierarchy.

If a branch ends in a Pop object before the requested level is reached, the summary for that terminal Pop is returned. Consequently, levels greater than the maximum depth return one summary for every terminal Pop object.

The outputs of withinPopFUN must have consistent lengths and names. The output of acrossPopFUN must preserve the number and names of these summary values. Missing values are passed through unchanged, so how they propagate is decided by the supplied functions. For example, colMeans returns NA for a column containing a missing value and leaves the other columns unaffected.

Examples

founderPop = quickHaplo(nInd = 16, nChr = 1, segSites = 20)
SP = SimParam$new(founderPop)
SP$addTraitA(nQtlPerChr = 10)
SP$setVarE(h2 = 0.5)

pop = newPop(founderPop, simParam = SP)

# Create a nested MultiPop
multiPop = splitPop(
  pop,
  by = list(
    rep(c("A", "B"), each = 8),
    function(x) {
      rep(c("one", "two"), length.out = nInd(x))
    }
  )
)

# Calculate mean phenotypic values for each terminal Pop
popMeans = calcPopSummary(
  multiPop,
  withinPopFUN = function(x) colMeans(pheno(x)),
  acrossPopFUN = colMeans,
  level = 2
)
popMeans
#>           Trait1
#> [1,]  0.20165412
#> [2,] -0.79965650
#> [3,]  0.46864192
#> [4,]  0.02430383
#> attr(,"source")
#>   level1 level2
#> 1      A    one
#> 2      A    two
#> 3      B    one
#> 4      B    two
attr(popMeans, "source")
#>   level1 level2
#> 1      A    one
#> 2      A    two
#> 3      B    one
#> 4      B    two

# Aggregate terminal Pop means within each level-1 MultiPop
groupMeans = calcPopSummary(
  multiPop,
  withinPopFUN = function(x) colMeans(pheno(x)),
  acrossPopFUN = colMeans,
  level = 1
)
groupMeans
#>          Trait1
#> [1,] -0.2990012
#> [2,]  0.2464729
#> attr(,"source")
#>   level1
#> 1      A
#> 2      B

# Aggregate across the complete hierarchy
overallMean = calcPopSummary(
  multiPop,
  withinPopFUN = function(x) colMeans(pheno(x)),
  acrossPopFUN = colMeans,
  level = 0
)
overallMean
#>      Trait1 
#> -0.02626416