Storing miscellaneous data in AlphaSimR populations
Gregor Gorjanc and Chris Gaynor
2026-12-01
Source:vignettes/articles/Misc.Rmd
Misc.RmdWhy use miscellaneous data slots?
AlphaSimR generates a lot of information about each
individual, such as genome, genetic and phenotypic values, sex, etc.
This information is stored in slots of the Pop object, and
can be accessed via the @ operator (say,
pop@gv) or some accessor functions (say,
gv(pop)).
Sometimes we may want to store additional information beyond these
standard Pop slots. For example, we may want to store
covariates, factor levels, or other metadata for the individuals in a
Pop object. We may want to also store some metadata about a
Pop object itself. While we can achieve all this by
creating other objects in the working environment, say in a matrix or
data frame, it is convenient to store the additional information in the
Pop object. This is what the misc and
miscPop slots are for.
We use the misc slot for individual
miscellaneous data. This slot is a list. Each list element must
have one item per individual, or one row of a matrix per individual,
both in the same order as individuals are stored in the Pop
object. We use the miscPop slot for population
miscellaneous data. This slot is also a list. Both slots start
as empty when a Pop object is created and users can add
data to them as shown below.
founders = quickHaplo(nInd=4, nChr=1, segSites=10)
SP = SimParam$new(founders)
pop = newPop(founders, simParam=SP)
pop@misc$cost = c(2, 4, 6, 8)
pop@misc$group = c("A", "A", "B", "B")
pop@misc$mat = matrix(rnorm(8), nrow=4, ncol=2)
pop@miscPop = list(totalCost=sum(pop@misc$cost),
groupCounts=table(pop@misc$group))
pop@misc
#> $cost
#> [1] 2 4 6 8
#>
#> $group
#> [1] "A" "A" "B" "B"
#>
#> $mat
#> [,1] [,2]
#> [1,] -0.2441996 2.0650249
#> [2,] -0.2827054 -1.6309894
#> [3,] -0.5536994 0.5124269
#> [4,] 0.6289820 -1.8630115
pop@miscPop
#> $totalCost
#> [1] 20
#>
#> $groupCounts
#>
#> A B
#> 2 2Because list is a flexible data structure, we can store
different types of objects in the list elements of misc and
miscPop. Which types of objects are appropriate depends on
the context, but also if we can define a sensible behavior for these
objects when we are subsetting and merging populations. This is
explained in the next section.
The above example is somewhat artificial, but it illustrates how we
can store additional information in a Pop object. A more
concrete example comes from a simulation of a dairy cattle breeding
program we have written, where we stored the farm ID for each cow, cow’s
lactation number, etc. in the misc slot. In addition to
this, we wrote a custom setPhenoCow() function that used
this additional information to generate phenotypes for the cows as a
function of their genetic values and environmental noise already
generated by setPheno(), but also adding the farm effect
and the lactation number effect. As the Pop objects
progressed through the simulation, we updated some of the information in
the misc slot, say, the lactation number of cows that were
kept for another year.
What happens when we subset or merge populations?
Subsetting a population keeps misc slot
aligned with the individuals. The subsetting operator handles any type
of vector-like object that can be subsetted with the [
method (say, pop@misc$obj[i]), matrices (say,
pop@misc$mat[i, , drop = FALSE]), or Pop
objects (say, pop@misc$otherPop[i], which behaves like a
vector-like object). However, subsetting a population clears
miscPop.
selected = pop[c(4, 1, 2)]
selected@misc
#> $cost
#> [1] 8 2 4
#>
#> $group
#> [1] "B" "A" "A"
#>
#> $mat
#> [,1] [,2]
#> [1,] 0.6289820 -1.863011
#> [2,] -0.2441996 2.065025
#> [3,] -0.2827054 -1.630989
selected@miscPop
#> list()Merging populations combines their corresponding misc
slots, using c() for vector-like objects and
rbind() for matrices. However, merging populations
clears miscPop.
combined = c(pop, pop)
combined@misc
#> $cost
#> [1] 2 4 6 8 2 4 6 8
#>
#> $group
#> [1] "A" "A" "B" "B" "A" "A" "B" "B"
#>
#> $mat
#> [,1] [,2]
#> [1,] -0.2441996 2.0650249
#> [2,] -0.2827054 -1.6309894
#> [3,] -0.5536994 0.5124269
#> [4,] 0.6289820 -1.8630115
#> [5,] -0.2441996 2.0650249
#> [6,] -0.2827054 -1.6309894
#> [7,] -0.5536994 0.5124269
#> [8,] 0.6289820 -1.8630115
combined@miscPop
#> list()If the number or names of misc elements differ between
populations, c() warns and clears the entire
misc slot. Use the same names in the same order to
avoid this.
other = pop
other@misc$group = NULL
combined = c(pop, other)
#> Warning in mergePops(c(list(x), args)): number of misc elements differs -
#> setting misc to an empty list!
combined@misc
#> list()Subsetting and merging operations clear miscPop because
AlphaSimR does not know how to recompute arbitrary population summaries.
You have to recompute them explicitly when needed:
selected@miscPop$totalCost = sum(selected@misc$cost)
selected@miscPop
#> $totalCost
#> [1] 14