Skip to contents

Apply a summary function to each Pop-class in a MultiPop-class and optionally simplify the output to a requested level of nesting.

Usage

calcPopValue(x, FUN, simplify = FALSE, level = 1, ...)

Arguments

x

A RawPop-class (including MapPop-class and Pop-class), HybridPop-class or MultiPop-class object. A MultiPop can only hold Pop and MultiPop objects, so a HybridPop or a MapPop are only accepted on their own.

FUN

Function to apply to each Pop. Must accept a Pop (or a RawPop or HybridPop, when x is one) as its first argument.

simplify

Logical. If TRUE, simplify the output by flattening the MultiPop in x to the requested level using flattenMultiPop. The corresponding output matrices from each Pop are combined with rbind, and a "source" attribute is added to indicate the origin of each row (see Details).

level

Integer scalar >= 1. Number of MultiPop levels to preserve when simplify=TRUE. Passed to flattenMultiPop. Ignored if simplify=FALSE.

...

Additional arguments passed to FUN.

Value

If x is a Pop, returns the value of FUN(x, ...). Otherwise returns a list (or a simplified matrix) of results, optionally with a "source" attribute.

Details

The level argument controls the depth of nesting retained when simplify=TRUE. If level exceeds the depth of x, the output structure is returned unchanged.

When the output is simplified (simplify=TRUE), a "source" attribute is attached to the resulting matrix. This attribute is a data frame with columns level1, level2, etc., that record the origin of each row. The values correspond to the name or index of the population from which the row was derived at each nesting level.

Examples

# Create founder haplotypes
founderPop = quickHaplo(nInd=6, nChr=1, segSites=10)

# Set simulation parameters
SP = SimParam$new(founderPop)
SP$addTraitA(10, mean = c(0, 0), var = c(1, 1))
SP$setVarE(h2 = c(0.6, 0.4))

# Create population
pop = newPop(founderPop, simParam=SP)
pop2 = randCross(pop, nCrosses = 3, nProgeny = 4)
#> Error in getSimParam(simParam): simParam was not supplied and there is no SimParam called SP in the global environment. Supply simParam or create SP with SimParam$new()

# Create a nested MultiPop
mp1 = splitPop(
  pop2,
  by = list(
    function(x) rep(LETTERS[1:2], length.out = length(x)),
    function(x) paste(x@mother, x@father, sep = "_")
  )
)
#> Error: object 'pop2' not found

# Calculate phenotypes at different levels
calcPopValue(mp1, FUN = pheno, simplify = TRUE, level = 2)
#> Error: object 'mp1' not found
calcPopValue(mp1, FUN = pheno, simplify = TRUE, level = 1)
#> Error: object 'mp1' not found

# Custom function returning a summary matrix
calcPopValue(mp1, FUN = function(x) colMeans(pheno(x)),
             simplify = TRUE, level = 1)
#> Error: object 'mp1' not found