Skip to contents

Selects a subset of nPop populations from a MultiPop-class object based on a summary value calculated from the selection criterion use.

For nested MultiPop objects (containing one or more MultiPop objects), selection is performed at the specified level, where level=1 selects among direct children that are Pop-class objects, level=2 descends one level into nested MultiPops to select among their direct children, and so on.

Internally, selectPop executes the summary function FUN on the vector or matrix of use values for each population. The resulting vector of summary values is used to rank and select populations.

Usage

selectPop(
  x,
  nPop,
  level = 1,
  trait = 1,
  use = "pheno",
  selectTop = TRUE,
  simParam = NULL,
  ...,
  FUN = mean,
  FUN.ARGS = list()
)

Arguments

x

a MultiPop-class object

nPop

the number of populations to select

level

nesting depth at which selection is performed. level=1 selects among direct children, level=2 descends one level into nested MultiPops, etc.

trait

the trait for selection. Either a number indicating a single trait or a character for a trait name, or a function returning a vector of length nInd(pop) for each population at level. The function must work on a vector or matrix of use values as trait(pop@use, ...) - depending on what use is. See the examples and selIndex.

use

the selection criterion. Either a character (genetic values "gv", estimated breeding values "ebv", phenotypes "pheno", or random values "rand") or a function returning a vector of length nInd(pop) for each population at level. The function must work on each pop at level in x as use(pop, trait, ...) or as trait(pop@use, ...) depending on what trait is. See the examples.

selectTop

selects highest values if TRUE. Selects lowest values if FALSE.

simParam

an object of SimParam

...

additional arguments if using a function for trait or use

FUN

a summary function to be applied to the vector of selection criterion values per population. Default is mean.

FUN.ARGS

a list of additional arguments passed to FUN

Value

Returns a MultiPop-class object containing the selected populations. When x is a Pop-class, the original object is returned unchanged.

Details

The level argument controls at which nesting depth selection operates. At level=1, selection occurs among direct children of x (which must be Pop-class objects). At level=2, the function descends one level into nested MultiPops and selects among their direct children, and so on. If level exceeds the nesting depth of x, the function stops with an error.

If the number of eligible populations is less than nPop, the function returns all eligible populations and issues a warning.

Examples

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

# Set simulation parameters
SP = SimParam$new(founderPop)
SP$addTraitA(10)
SP$setVarE(h2=0.5)

# Create population
pop = newPop(founderPop, simParam=SP)

# Create multi-population with one level of nesting
mp1 = newMultiPop(pop[1:4], pop[5:8], pop[9:12], pop[13:16], pop[17:20])

# Select best 2 populations
selectPop(mp1, nPop=2, simParam=SP)
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 4 individual(s): 9, 10, ..., 12
#> `-- [[2]] An object of class "Pop" with 4 individual(s): 17, 18, ..., 20

# Select 2 most deviating populations from an optima
squaredDeviation = function(x, optima=0) (x - optima)^2
selectPop(mp1, nPop=2, trait=squaredDeviation, simParam=SP)
#> An object of class "MultiPop" with 2 item(s)
#>     Level 1:
#> |-- [[1]] An object of class "Pop" with 4 individual(s): 5, 6, ..., 8
#> `-- [[2]] An object of class "Pop" with 4 individual(s): 17, 18, ..., 20

# Create multi-population with two levels of nesting
pop@misc = list(smth=rnorm(20), smth2=rnorm(20))
by_df = data.frame(
  level1 = rep(LETTERS[1:5], each = 4),
  level2 = rep(c('X','Y'), length.out = nInd(pop)),
  row.names = pop@id
)
mp2 = splitPop(pop, by = by_df)

# Select the best population at level 2
selectPop(mp2, nPop=1, level=2, simParam=SP)
#> An object of class "MultiPop" with 5 item(s)
#>     Level 1:
#> |-- [[1]]  "A" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 2, 4
#> |-- [[2]]  "B" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 5, 7
#> |-- [[3]]  "C" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 10, 12
#> |-- [[4]]  "D" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 13, 15
#> `-- [[5]]  "E" - An object of class "MultiPop" with 1 item(s)
#>       |   Level 2:
#>       `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 17, 19

# Select the best population at level 2 based on
# miscellaneous info with `use` function
useFunc = function(pop, trait=NULL) pop@misc$smth + pop@misc$smth2
selectPop(mp2, nPop=1, level=2, simParam=SP, use=useFunc)
#> An object of class "MultiPop" with 5 item(s)
#>     Level 1:
#> |-- [[1]]  "A" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 1, 3
#> |-- [[2]]  "B" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 5, 7
#> |-- [[3]]  "C" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 10, 12
#> |-- [[4]]  "D" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 14, 16
#> `-- [[5]]  "E" - An object of class "MultiPop" with 1 item(s)
#>       |   Level 2:
#>       `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 18, 20

# ... equivalent result with the `use` & `trait` function
useFunc2 = function(pop, trait=NULL) cbind(pop@misc$smth, pop@misc$smth2)
trtFunc = function(x) rowSums(x)
selectPop(mp2, nPop=1, level=2, use=useFunc2, trait=trtFunc, simParam=SP)
#> An object of class "MultiPop" with 5 item(s)
#>     Level 1:
#> |-- [[1]]  "A" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 1, 3
#> |-- [[2]]  "B" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "X" - An object of class "Pop" with 2 individual(s): 5, 7
#> |-- [[3]]  "C" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 10, 12
#> |-- [[4]]  "D" - An object of class "MultiPop" with 1 item(s)
#> |     |   Level 2:
#> |     `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 14, 16
#> `-- [[5]]  "E" - An object of class "MultiPop" with 1 item(s)
#>       |   Level 2:
#>       `-- [[1]]  "Y" - An object of class "Pop" with 2 individual(s): 18, 20