
Quick Example of Simulated Kinships with Partial Parentage
nprcgenekeepr: an R Package for the Genetic Management of Colonies
R. Mark Sharp, Ph.D.
2026-11-12
Source:vignettes/simulatedKValues.Rmd
simulatedKValues.RmdIntroduction
This vignette provides a quick example of how to estimate kinship coefficients using simulation. The same setup is run with 10, 100 and 500 simulations to illustrate the type of kinship coefficient estimate variance you can expect with a simple pedigree exhibiting partial parentage for some of the members.
Anticipation of further uses of this kinship estimation method
Retention of founder alleles is a primary driving force behind this package of utilities. This example is intentionally overly simplistic to clearly illustrate this one aspect of the methodology used with realistic pedigrees.
In this example, some of the potential parents are not members of the example pedigree and thus, by definition, have a kinship coefficient of 0.0 with all other pedigree members.
In practice most or all of the potential parents will be members of the same pedigree and will potentially have nonzero kinship coefficients with other pedigree members.
Identification of potential parents
This example writes its lists of potential parents by hand. getPotentialParents builds them from a pedigree: for each animal born at the center that has a birth date and a missing sire or dam, it lists the animals old enough, and present at the right time, to be that parent, from the birth and exit dates and the species’ breeding ages and gestation length. Each element of its result holds id, sires and dams, as the lists in allSimParents below do, so the result can be passed to createSimKinships as it is. The Potential Parents tab of the Shiny application shows the same lists.
A planned capability will also use timestamped transactional location data to identify potential parents for animals with a missing parent.
Creation of example pedigree1
The example uses the package’s small pedigree, smallPed, which is shown after the setup code below. All of its animals have identifiers that are single letters of the alphabet.
Each simParent_n list names one animal (id) and
the animals that could be its sire (sires) and its dam
(dams). In each simulated pedigree, a parent that is not
recorded is drawn at random from the animal’s list, and a recorded
parent is always kept. This example gives lists to B,
E, J, K and N, which have neither
parent recorded, and to A, which has no recorded dam.
A’s sire, Q, is recorded, so the Q in
simParent_1 changes nothing. O and Q have
no list and stay founders. Potential parents can come from the pedigree
itself or from outside it: the identifiers with an underscore, such as
s1_1 and d3_1, are not in
smallPed.
To give some potential parents more weight than others, list them more than once: each is drawn in proportion to the number of times it is listed.
knitr::opts_chunk$set(echo = TRUE)
library(knitr) # nolint: undesirable_function_linter
library(nprcgenekeepr) # nolint: undesirable_function_linter
library(stringi) # nolint: undesirable_function_linter
## kableExtra is optional: it styles the tables when it is installed.
kable_styling <- if (requireNamespace("kableExtra", quietly = TRUE)) { # nolint: object_name_linter
kableExtra::kable_styling
} else {
function(kable_input, ...) kable_input
}
ped <- nprcgenekeepr::smallPed
simParent_1 <- list( # nolint: object_name_linter
id = "A", # nolint: object_name_linter
sires = "Q",
dams = c("d1_1", "d1_2", "d1_3", "d1_4")
)
simParent_2 <- list( # nolint: object_name_linter
id = "B", # nolint: object_name_linter
sires = c("s1_1", "s1_2", "s1_3"),
dams = c("d1_1", "d1_2", "d1_3", "d1_4")
)
simParent_3 <- list( # nolint: object_name_linter
id = "E", # nolint: object_name_linter
sires = c("A", "C", "s1_1"),
dams = c("d3_1", "B")
)
simParent_4 <- list( # nolint: object_name_linter
id = "J", # nolint: object_name_linter
sires = c("A", "C", "s1_1"),
dams = c("d3_1", "B")
)
simParent_5 <- list( # nolint: object_name_linter
id = "K", # nolint: object_name_linter
sires = c("A", "C", "s1_1"),
dams = c("d3_1", "d1_2")
)
simParent_6 <- list( # nolint: object_name_linter
id = "N", # nolint: object_name_linter
sires = c("A", "C", "s1_2"),
dams = c("d3_1", "B")
)
allSimParents <- list(
simParent_1, simParent_2, simParent_3,
simParent_4, simParent_5, simParent_6
)
extractKValue <- function(kValue, id1, id2, simulation) {
row <- kValue$id_1 == id1 & kValue$id_2 == id2
vapply(paste0("sim_", simulation), function(column) kValue[[column]][row],
numeric(1L))
}
ped[, c("id", "sire", "dam", "sex")]## id sire dam sex
## 1 A Q <NA> M
## 2 B <NA> <NA> F
## 3 C A B M
## 4 D A B M
## 5 E <NA> <NA> F
## 6 F D E F
## 7 G D E F
## 8 H A B M
## 9 I A J F
## 10 J <NA> <NA> F
## 11 K <NA> <NA> F
## 12 L C K F
## 13 M A N M
## 14 N <NA> <NA> F
## 15 O <NA> <NA> F
## 16 P M O F
## 17 Q <NA> <NA> M
Small Example
This is the simulation. Only the pairs whose kinship varies between the simulated pedigrees are counted, and the first 10 of them are printed.
Before running these simulations, take time to look at the help pages of the package functions used here to see what they expect as arguments and what they return. (extractKValue is defined in the setup code above, not in the package.)
?createSimKinships
?kinshipMatricesToKValues
?countKinshipValues
?summarizeKinshipValues
# Only set this seed if you want to get the same simulation results each time.
set.seed(1L)
n <- 10L
simKinships <- createSimKinships(ped, allSimParents, pop = ped$id, n = n)
kValues <- kinshipMatricesToKValues(simKinships)
extractKValue(kValues, id1 = "A", id2 = "F", simulation = 1L:n)## sim_1 sim_2 sim_3 sim_4 sim_5 sim_6 sim_7 sim_8 sim_9 sim_10
## 0.125 0.125 0.125 0.125 0.250 0.125 0.125 0.250 0.250 0.125
counts <- countKinshipValues(kValues)
counts$kIds[1L:3L]## [[1]]
## [1] "A" "A"
##
## [[2]]
## [1] "A" "B"
##
## [[3]]
## [1] "A" "C"
counts$kValues[1L:3L]## [[1]]
## [1] 0.5
##
## [[2]]
## [1] 0
##
## [[3]]
## [1] 0.25
counts$kCounts[1L:3L]## [[1]]
## [1] 10
##
## [[2]]
## [1] 10
##
## [[3]]
## [1] 10
stats_10 <- summarizeKinshipValues(counts)
nrow(stats_10[stats_10$sd > 0.0, ])## [1] 78
kable(head(stats_10[stats_10$sd > 0.0, ], 10L), longtable = TRUE) |>
kable_styling(
latex_options = c("striped", "repeat_header"),
repeat_header_method = "replace",
repeat_header_text = "\\textit{(continued)}"
)| id_1 | id_2 | min | secondQuartile | mean | median | thirdQuartile | max | sd | |
|---|---|---|---|---|---|---|---|---|---|
| 5 | A | E | 0.000 | 0.000 | 0.0750 | 0.0000 | 0.250 | 0.250 | 0.1207615 |
| 6 | A | F | 0.125 | 0.125 | 0.1625 | 0.1250 | 0.250 | 0.250 | 0.0603807 |
| 7 | A | G | 0.125 | 0.125 | 0.1625 | 0.1250 | 0.250 | 0.250 | 0.0603807 |
| 9 | A | I | 0.250 | 0.250 | 0.3125 | 0.3125 | 0.375 | 0.375 | 0.0658808 |
| 10 | A | J | 0.000 | 0.000 | 0.1250 | 0.1250 | 0.250 | 0.250 | 0.1317616 |
| 14 | A | N | 0.000 | 0.000 | 0.0500 | 0.0000 | 0.000 | 0.250 | 0.1054093 |
| 21 | B | E | 0.000 | 0.000 | 0.1500 | 0.2500 | 0.250 | 0.250 | 0.1290994 |
| 22 | B | F | 0.125 | 0.125 | 0.2000 | 0.2500 | 0.250 | 0.250 | 0.0645497 |
| 23 | B | G | 0.125 | 0.125 | 0.2000 | 0.2500 | 0.250 | 0.250 | 0.0645497 |
| 25 | B | I | 0.000 | 0.000 | 0.0750 | 0.1250 | 0.125 | 0.125 | 0.0645497 |
A larger simulation
set.seed(1L)
n <- 100L
simKinships <- createSimKinships(ped, allSimParents, pop = ped$id, n = n)
kValues <- kinshipMatricesToKValues(simKinships)
extractKValue(kValues, id1 = "A", id2 = "F", simulation = 1L:10L)## sim_1 sim_2 sim_3 sim_4 sim_5 sim_6 sim_7 sim_8 sim_9 sim_10
## 0.125 0.125 0.125 0.125 0.250 0.125 0.125 0.250 0.250 0.125
counts <- countKinshipValues(kValues)
counts$kIds[1L:3L]## [[1]]
## [1] "A" "A"
##
## [[2]]
## [1] "A" "B"
##
## [[3]]
## [1] "A" "C"
counts$kValues[1L:3L]## [[1]]
## [1] 0.5
##
## [[2]]
## [1] 0
##
## [[3]]
## [1] 0.25
counts$kCounts[1L:3L]## [[1]]
## [1] 100
##
## [[2]]
## [1] 100
##
## [[3]]
## [1] 100
stats_100 <- summarizeKinshipValues(counts)
nrow(stats_100[stats_100$sd > 0.0, ])## [1] 94
kable(head(stats_100[stats_100$sd > 0.0, ], 10L), longtable = TRUE) |>
kable_styling(
latex_options = c("striped", "repeat_header"),
repeat_header_method = "replace",
repeat_header_text = "\\textit{(continued)}"
)| id_1 | id_2 | min | secondQuartile | mean | median | thirdQuartile | max | sd | |
|---|---|---|---|---|---|---|---|---|---|
| 5 | A | E | 0.000 | 0.000 | 0.08250 | 0.000 | 0.250 | 0.250 | 0.1181454 |
| 6 | A | F | 0.125 | 0.125 | 0.16625 | 0.125 | 0.250 | 0.250 | 0.0590727 |
| 7 | A | G | 0.125 | 0.125 | 0.16625 | 0.125 | 0.250 | 0.250 | 0.0590727 |
| 9 | A | I | 0.250 | 0.250 | 0.29875 | 0.250 | 0.375 | 0.375 | 0.0612759 |
| 10 | A | J | 0.000 | 0.000 | 0.09750 | 0.000 | 0.250 | 0.250 | 0.1225518 |
| 11 | A | K | 0.000 | 0.000 | 0.07000 | 0.000 | 0.250 | 0.250 | 0.1128152 |
| 12 | A | L | 0.125 | 0.125 | 0.16000 | 0.125 | 0.250 | 0.250 | 0.0564076 |
| 14 | A | N | 0.000 | 0.000 | 0.08000 | 0.000 | 0.250 | 0.250 | 0.1172065 |
| 21 | B | E | 0.000 | 0.000 | 0.13250 | 0.250 | 0.250 | 0.250 | 0.1254034 |
| 22 | B | F | 0.125 | 0.125 | 0.19125 | 0.250 | 0.250 | 0.250 | 0.0627017 |
A much larger simulation
set.seed(1L)
n <- 500L
simKinships <- createSimKinships(ped, allSimParents, pop = ped$id, n = n)
kValues <- kinshipMatricesToKValues(simKinships)
extractKValue(kValues, id1 = "A", id2 = "F", simulation = 1L:10L)## sim_1 sim_2 sim_3 sim_4 sim_5 sim_6 sim_7 sim_8 sim_9 sim_10
## 0.125 0.125 0.125 0.125 0.250 0.125 0.125 0.250 0.250 0.125
counts <- countKinshipValues(kValues)
counts$kIds[1L:3L]## [[1]]
## [1] "A" "A"
##
## [[2]]
## [1] "A" "B"
##
## [[3]]
## [1] "A" "C"
counts$kValues[1L:3L]## [[1]]
## [1] 0.5
##
## [[2]]
## [1] 0
##
## [[3]]
## [1] 0.25
counts$kCounts[1L:3L]## [[1]]
## [1] 500
##
## [[2]]
## [1] 500
##
## [[3]]
## [1] 500
stats_500 <- summarizeKinshipValues(counts)
nrow(stats_500[stats_500$sd > 0.0, ])## [1] 94
kable(head(stats_500[stats_500$sd > 0.0, ], 10L), longtable = TRUE) |>
kable_styling(
latex_options = c("striped", "repeat_header"),
repeat_header_method = "replace",
repeat_header_text = "\\textit{(continued)}"
)| id_1 | id_2 | min | secondQuartile | mean | median | thirdQuartile | max | sd | |
|---|---|---|---|---|---|---|---|---|---|
| 5 | A | E | 0.000 | 0.000 | 0.08650 | 0.000 | 0.250 | 0.250 | 0.1190424 |
| 6 | A | F | 0.125 | 0.125 | 0.16825 | 0.125 | 0.250 | 0.250 | 0.0595212 |
| 7 | A | G | 0.125 | 0.125 | 0.16825 | 0.125 | 0.250 | 0.250 | 0.0595212 |
| 9 | A | I | 0.250 | 0.250 | 0.29200 | 0.250 | 0.375 | 0.375 | 0.0591015 |
| 10 | A | J | 0.000 | 0.000 | 0.08400 | 0.000 | 0.250 | 0.250 | 0.1182030 |
| 11 | A | K | 0.000 | 0.000 | 0.08000 | 0.000 | 0.250 | 0.250 | 0.1167358 |
| 12 | A | L | 0.125 | 0.125 | 0.16500 | 0.125 | 0.250 | 0.250 | 0.0583679 |
| 14 | A | N | 0.000 | 0.000 | 0.08200 | 0.000 | 0.250 | 0.250 | 0.1174888 |
| 21 | B | E | 0.000 | 0.000 | 0.12550 | 0.250 | 0.250 | 0.250 | 0.1251242 |
| 22 | B | F | 0.125 | 0.125 | 0.18775 | 0.250 | 0.250 | 0.250 | 0.0625621 |
Comparing the values and variation found for the various kinship values:
stats_short <- stats_10[stats_10$sd > 0.0, ]
stats_long <- stats_500[stats_500$sd > 0.0, ]
## Align the short and long summaries by animal pair: the sd > 0 sets can
## differ in size between simulation lengths, so join on the ID pair rather
## than by row position.
merged <- merge(stats_short, stats_long, by = c("id_1", "id_2"),
suffixes = c("_short", "_long"))
comparison <- data.frame(
id_1 = merged$id_1,
id_2 = merged$id_2,
meanKin_short = merged$mean_short,
meanKin_long = merged$mean_long,
meanKinDelta = abs(merged$mean_short - merged$mean_long),
sdKin_short = merged$sd_short,
sdKin_long = merged$sd_long,
sdKinDelta = abs(merged$sd_short - merged$sd_long)
)
kable(comparison,
longtable = TRUE,
digits = c(0L, 0L, 4L, 4L, 4L, 4L, 4L, 4L),
caption = stri_c(
"Comparison of estimated kinships between simulations ",
"of 10 (short) and 500 (long)"
)
) |>
kable_styling(
latex_options = c("striped", "repeat_header"),
repeat_header_method = "replace",
repeat_header_text = "\\textit{(continued)}",
font_size = 10L
)| id_1 | id_2 | meanKin_short | meanKin_long | meanKinDelta | sdKin_short | sdKin_long | sdKinDelta |
|---|---|---|---|---|---|---|---|
| A | E | 0.0750 | 0.0865 | 0.0115 | 0.1208 | 0.1190 | 0.0017 |
| A | F | 0.1625 | 0.1683 | 0.0058 | 0.0604 | 0.0595 | 0.0009 |
| A | G | 0.1625 | 0.1683 | 0.0058 | 0.0604 | 0.0595 | 0.0009 |
| A | I | 0.3125 | 0.2920 | 0.0205 | 0.0659 | 0.0591 | 0.0068 |
| A | J | 0.1250 | 0.0840 | 0.0410 | 0.1318 | 0.1182 | 0.0136 |
| A | N | 0.0500 | 0.0820 | 0.0320 | 0.1054 | 0.1175 | 0.0121 |
| B | E | 0.1500 | 0.1255 | 0.0245 | 0.1291 | 0.1251 | 0.0040 |
| B | F | 0.2000 | 0.1878 | 0.0123 | 0.0645 | 0.0626 | 0.0020 |
| B | G | 0.2000 | 0.1878 | 0.0123 | 0.0645 | 0.0626 | 0.0020 |
| B | I | 0.0750 | 0.0583 | 0.0167 | 0.0645 | 0.0624 | 0.0021 |
| B | J | 0.1500 | 0.1165 | 0.0335 | 0.1291 | 0.1248 | 0.0043 |
| B | N | 0.0500 | 0.1205 | 0.0705 | 0.1054 | 0.1250 | 0.0196 |
| C | E | 0.1125 | 0.1060 | 0.0065 | 0.0922 | 0.0846 | 0.0077 |
| C | F | 0.1812 | 0.1780 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| C | G | 0.1812 | 0.1780 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| C | I | 0.1938 | 0.1751 | 0.0186 | 0.0355 | 0.0407 | 0.0052 |
| C | J | 0.1375 | 0.1003 | 0.0373 | 0.0710 | 0.0814 | 0.0104 |
| C | N | 0.0500 | 0.1013 | 0.0513 | 0.0874 | 0.0876 | 0.0002 |
| D | E | 0.1125 | 0.1060 | 0.0065 | 0.0922 | 0.0846 | 0.0077 |
| D | F | 0.3062 | 0.3030 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| D | G | 0.3062 | 0.3030 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| D | I | 0.1938 | 0.1751 | 0.0186 | 0.0355 | 0.0407 | 0.0052 |
| D | J | 0.1375 | 0.1003 | 0.0373 | 0.0710 | 0.0814 | 0.0104 |
| D | N | 0.0500 | 0.1013 | 0.0513 | 0.0874 | 0.0876 | 0.0002 |
| E | F | 0.3062 | 0.3030 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| E | G | 0.3062 | 0.3030 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| E | H | 0.1125 | 0.1060 | 0.0065 | 0.0922 | 0.0846 | 0.0077 |
| E | I | 0.0875 | 0.0740 | 0.0135 | 0.0791 | 0.0751 | 0.0039 |
| E | J | 0.1000 | 0.0615 | 0.0385 | 0.0791 | 0.0761 | 0.0030 |
| E | K | 0.0625 | 0.0290 | 0.0335 | 0.0659 | 0.0528 | 0.0131 |
| E | L | 0.0875 | 0.0675 | 0.0200 | 0.0323 | 0.0530 | 0.0207 |
| E | M | 0.0375 | 0.0432 | 0.0057 | 0.0604 | 0.0595 | 0.0009 |
| E | N | 0.0375 | 0.0562 | 0.0188 | 0.0604 | 0.0716 | 0.0112 |
| E | P | 0.0187 | 0.0216 | 0.0029 | 0.0302 | 0.0298 | 0.0004 |
| E | Q | 0.0375 | 0.0432 | 0.0057 | 0.0604 | 0.0595 | 0.0009 |
| F | F | 0.5562 | 0.5530 | 0.0032 | 0.0461 | 0.0423 | 0.0038 |
| F | G | 0.3062 | 0.3030 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| F | H | 0.1812 | 0.1780 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| F | I | 0.1406 | 0.1246 | 0.0161 | 0.0423 | 0.0473 | 0.0050 |
| F | J | 0.1187 | 0.0809 | 0.0379 | 0.0547 | 0.0655 | 0.0108 |
| F | K | 0.0312 | 0.0345 | 0.0033 | 0.0329 | 0.0444 | 0.0114 |
| F | L | 0.1062 | 0.1062 | 0.0000 | 0.0161 | 0.0322 | 0.0161 |
| F | M | 0.0813 | 0.0841 | 0.0029 | 0.0302 | 0.0298 | 0.0004 |
| F | N | 0.0437 | 0.0788 | 0.0350 | 0.0593 | 0.0675 | 0.0082 |
| F | P | 0.0406 | 0.0421 | 0.0014 | 0.0151 | 0.0149 | 0.0002 |
| F | Q | 0.0813 | 0.0841 | 0.0029 | 0.0302 | 0.0298 | 0.0004 |
| G | G | 0.5562 | 0.5530 | 0.0032 | 0.0461 | 0.0423 | 0.0038 |
| G | H | 0.1812 | 0.1780 | 0.0033 | 0.0461 | 0.0423 | 0.0038 |
| G | I | 0.1406 | 0.1246 | 0.0161 | 0.0423 | 0.0473 | 0.0050 |
| G | J | 0.1187 | 0.0809 | 0.0379 | 0.0547 | 0.0655 | 0.0108 |
| G | K | 0.0312 | 0.0345 | 0.0033 | 0.0329 | 0.0444 | 0.0114 |
| G | L | 0.1062 | 0.1062 | 0.0000 | 0.0161 | 0.0322 | 0.0161 |
| G | M | 0.0813 | 0.0841 | 0.0029 | 0.0302 | 0.0298 | 0.0004 |
| G | N | 0.0437 | 0.0788 | 0.0350 | 0.0593 | 0.0675 | 0.0082 |
| G | P | 0.0406 | 0.0421 | 0.0014 | 0.0151 | 0.0149 | 0.0002 |
| G | Q | 0.0813 | 0.0841 | 0.0029 | 0.0302 | 0.0298 | 0.0004 |
| H | I | 0.1938 | 0.1751 | 0.0186 | 0.0355 | 0.0407 | 0.0052 |
| H | J | 0.1375 | 0.1003 | 0.0373 | 0.0710 | 0.0814 | 0.0104 |
| H | N | 0.0500 | 0.1013 | 0.0513 | 0.0874 | 0.0876 | 0.0002 |
| I | I | 0.5625 | 0.5420 | 0.0205 | 0.0659 | 0.0591 | 0.0068 |
| I | J | 0.3125 | 0.2920 | 0.0205 | 0.0659 | 0.0591 | 0.0068 |
| I | K | 0.0063 | 0.0524 | 0.0461 | 0.0198 | 0.0671 | 0.0474 |
| I | L | 0.1000 | 0.1138 | 0.0137 | 0.0198 | 0.0394 | 0.0196 |
| I | M | 0.1562 | 0.1460 | 0.0103 | 0.0329 | 0.0296 | 0.0034 |
| I | N | 0.0562 | 0.0670 | 0.0108 | 0.0952 | 0.0735 | 0.0218 |
| I | P | 0.0781 | 0.0730 | 0.0051 | 0.0165 | 0.0148 | 0.0017 |
| I | Q | 0.1562 | 0.1460 | 0.0103 | 0.0329 | 0.0296 | 0.0034 |
| J | K | 0.0125 | 0.0248 | 0.0122 | 0.0395 | 0.0499 | 0.0103 |
| J | L | 0.0750 | 0.0625 | 0.0125 | 0.0395 | 0.0499 | 0.0104 |
| J | M | 0.0625 | 0.0420 | 0.0205 | 0.0659 | 0.0591 | 0.0068 |
| J | N | 0.0625 | 0.0520 | 0.0105 | 0.1062 | 0.0728 | 0.0334 |
| J | P | 0.0312 | 0.0210 | 0.0102 | 0.0329 | 0.0296 | 0.0034 |
| J | Q | 0.0625 | 0.0420 | 0.0205 | 0.0659 | 0.0591 | 0.0068 |
| K | N | 0.0250 | 0.0275 | 0.0025 | 0.0527 | 0.0518 | 0.0009 |
| L | N | 0.0375 | 0.0644 | 0.0269 | 0.0527 | 0.0540 | 0.0013 |
| M | N | 0.0250 | 0.0410 | 0.0160 | 0.0527 | 0.0587 | 0.0060 |
| N | P | 0.0125 | 0.0205 | 0.0080 | 0.0264 | 0.0294 | 0.0030 |
| N | Q | 0.0250 | 0.0410 | 0.0160 | 0.0527 | 0.0587 | 0.0060 |
stats_short <- stats_100[stats_100$sd > 0.0, ]
stats_long <- stats_500[stats_500$sd > 0.0, ]
## Align the short and long summaries by animal pair: the sd > 0 sets can
## differ in size between simulation lengths, so join on the ID pair rather
## than by row position.
merged <- merge(stats_short, stats_long, by = c("id_1", "id_2"),
suffixes = c("_short", "_long"))
comparison <- data.frame(
id_1 = merged$id_1,
id_2 = merged$id_2,
meanKin_short = merged$mean_short,
meanKin_long = merged$mean_long,
meanKinDelta = abs(merged$mean_short - merged$mean_long),
sdKin_short = merged$sd_short,
sdKin_long = merged$sd_long,
sdKinDelta = abs(merged$sd_short - merged$sd_long)
)
kable(comparison,
longtable = TRUE,
digits = c(0L, 0L, 4L, 4L, 4L, 4L, 4L, 4L),
caption = stri_c(
"Comparison of estimated kinships between simulations ",
"of 100 (short) and 500 (long)"
)
) |>
kable_styling(
latex_options = c("striped", "repeat_header"),
repeat_header_method = "replace",
repeat_header_text = "\\textit{(continued)}",
font_size = 10L
)| id_1 | id_2 | meanKin_short | meanKin_long | meanKinDelta | sdKin_short | sdKin_long | sdKinDelta |
|---|---|---|---|---|---|---|---|
| A | E | 0.0825 | 0.0865 | 0.0040 | 0.1181 | 0.1190 | 0.0009 |
| A | F | 0.1663 | 0.1683 | 0.0020 | 0.0591 | 0.0595 | 0.0004 |
| A | G | 0.1663 | 0.1683 | 0.0020 | 0.0591 | 0.0595 | 0.0004 |
| A | I | 0.2988 | 0.2920 | 0.0068 | 0.0613 | 0.0591 | 0.0022 |
| A | J | 0.0975 | 0.0840 | 0.0135 | 0.1226 | 0.1182 | 0.0043 |
| A | K | 0.0700 | 0.0800 | 0.0100 | 0.1128 | 0.1167 | 0.0039 |
| A | L | 0.1600 | 0.1650 | 0.0050 | 0.0564 | 0.0584 | 0.0020 |
| A | N | 0.0800 | 0.0820 | 0.0020 | 0.1172 | 0.1175 | 0.0003 |
| B | E | 0.1325 | 0.1255 | 0.0070 | 0.1254 | 0.1251 | 0.0003 |
| B | F | 0.1912 | 0.1878 | 0.0035 | 0.0627 | 0.0626 | 0.0001 |
| B | G | 0.1912 | 0.1878 | 0.0035 | 0.0627 | 0.0626 | 0.0001 |
| B | I | 0.0625 | 0.0583 | 0.0042 | 0.0628 | 0.0624 | 0.0004 |
| B | J | 0.1250 | 0.1165 | 0.0085 | 0.1256 | 0.1248 | 0.0008 |
| B | N | 0.1100 | 0.1205 | 0.0105 | 0.1247 | 0.1250 | 0.0003 |
| C | E | 0.1075 | 0.1060 | 0.0015 | 0.0871 | 0.0846 | 0.0025 |
| C | F | 0.1788 | 0.1780 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| C | G | 0.1788 | 0.1780 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| C | I | 0.1806 | 0.1751 | 0.0055 | 0.0386 | 0.0407 | 0.0021 |
| C | J | 0.1113 | 0.1003 | 0.0110 | 0.0772 | 0.0814 | 0.0041 |
| C | K | 0.0350 | 0.0400 | 0.0050 | 0.0564 | 0.0584 | 0.0020 |
| C | L | 0.2675 | 0.2700 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| C | N | 0.0950 | 0.1013 | 0.0063 | 0.0925 | 0.0876 | 0.0049 |
| D | E | 0.1075 | 0.1060 | 0.0015 | 0.0871 | 0.0846 | 0.0025 |
| D | F | 0.3038 | 0.3030 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| D | G | 0.3038 | 0.3030 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| D | I | 0.1806 | 0.1751 | 0.0055 | 0.0386 | 0.0407 | 0.0021 |
| D | J | 0.1113 | 0.1003 | 0.0110 | 0.0772 | 0.0814 | 0.0041 |
| D | K | 0.0350 | 0.0400 | 0.0050 | 0.0564 | 0.0584 | 0.0020 |
| D | L | 0.1425 | 0.1450 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| D | N | 0.0950 | 0.1013 | 0.0063 | 0.0925 | 0.0876 | 0.0049 |
| E | F | 0.3038 | 0.3030 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| E | G | 0.3038 | 0.3030 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| E | H | 0.1075 | 0.1060 | 0.0015 | 0.0871 | 0.0846 | 0.0025 |
| E | I | 0.0762 | 0.0740 | 0.0023 | 0.0730 | 0.0751 | 0.0021 |
| E | J | 0.0700 | 0.0615 | 0.0085 | 0.0801 | 0.0761 | 0.0040 |
| E | K | 0.0388 | 0.0290 | 0.0097 | 0.0581 | 0.0528 | 0.0053 |
| E | L | 0.0731 | 0.0675 | 0.0056 | 0.0533 | 0.0530 | 0.0003 |
| E | M | 0.0412 | 0.0432 | 0.0020 | 0.0591 | 0.0595 | 0.0004 |
| E | N | 0.0625 | 0.0562 | 0.0062 | 0.0743 | 0.0716 | 0.0027 |
| E | P | 0.0206 | 0.0216 | 0.0010 | 0.0295 | 0.0298 | 0.0002 |
| E | Q | 0.0412 | 0.0432 | 0.0020 | 0.0591 | 0.0595 | 0.0004 |
| F | F | 0.5538 | 0.5530 | 0.0007 | 0.0435 | 0.0423 | 0.0012 |
| F | G | 0.3038 | 0.3030 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| F | H | 0.1788 | 0.1780 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| F | I | 0.1284 | 0.1246 | 0.0039 | 0.0426 | 0.0473 | 0.0048 |
| F | J | 0.0906 | 0.0809 | 0.0097 | 0.0637 | 0.0655 | 0.0018 |
| F | K | 0.0369 | 0.0345 | 0.0024 | 0.0436 | 0.0444 | 0.0008 |
| F | L | 0.1078 | 0.1062 | 0.0016 | 0.0321 | 0.0322 | 0.0001 |
| F | M | 0.0831 | 0.0841 | 0.0010 | 0.0295 | 0.0298 | 0.0002 |
| F | N | 0.0788 | 0.0788 | 0.0000 | 0.0720 | 0.0675 | 0.0045 |
| F | P | 0.0416 | 0.0421 | 0.0005 | 0.0148 | 0.0149 | 0.0001 |
| F | Q | 0.0831 | 0.0841 | 0.0010 | 0.0295 | 0.0298 | 0.0002 |
| G | G | 0.5538 | 0.5530 | 0.0007 | 0.0435 | 0.0423 | 0.0012 |
| G | H | 0.1788 | 0.1780 | 0.0008 | 0.0435 | 0.0423 | 0.0012 |
| G | I | 0.1284 | 0.1246 | 0.0039 | 0.0426 | 0.0473 | 0.0048 |
| G | J | 0.0906 | 0.0809 | 0.0097 | 0.0637 | 0.0655 | 0.0018 |
| G | K | 0.0369 | 0.0345 | 0.0024 | 0.0436 | 0.0444 | 0.0008 |
| G | L | 0.1078 | 0.1062 | 0.0016 | 0.0321 | 0.0322 | 0.0001 |
| G | M | 0.0831 | 0.0841 | 0.0010 | 0.0295 | 0.0298 | 0.0002 |
| G | N | 0.0788 | 0.0788 | 0.0000 | 0.0720 | 0.0675 | 0.0045 |
| G | P | 0.0416 | 0.0421 | 0.0005 | 0.0148 | 0.0149 | 0.0001 |
| G | Q | 0.0831 | 0.0841 | 0.0010 | 0.0295 | 0.0298 | 0.0002 |
| H | I | 0.1806 | 0.1751 | 0.0055 | 0.0386 | 0.0407 | 0.0021 |
| H | J | 0.1113 | 0.1003 | 0.0110 | 0.0772 | 0.0814 | 0.0041 |
| H | K | 0.0350 | 0.0400 | 0.0050 | 0.0564 | 0.0584 | 0.0020 |
| H | L | 0.1425 | 0.1450 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| H | N | 0.0950 | 0.1013 | 0.0063 | 0.0925 | 0.0876 | 0.0049 |
| I | I | 0.5488 | 0.5420 | 0.0067 | 0.0613 | 0.0591 | 0.0022 |
| I | J | 0.2988 | 0.2920 | 0.0068 | 0.0613 | 0.0591 | 0.0022 |
| I | K | 0.0469 | 0.0524 | 0.0055 | 0.0649 | 0.0671 | 0.0022 |
| I | L | 0.1138 | 0.1138 | 0.0000 | 0.0360 | 0.0394 | 0.0034 |
| I | M | 0.1494 | 0.1460 | 0.0034 | 0.0306 | 0.0296 | 0.0011 |
| I | N | 0.0638 | 0.0670 | 0.0033 | 0.0794 | 0.0735 | 0.0060 |
| I | P | 0.0747 | 0.0730 | 0.0017 | 0.0153 | 0.0148 | 0.0005 |
| I | Q | 0.1494 | 0.1460 | 0.0034 | 0.0306 | 0.0296 | 0.0011 |
| J | K | 0.0238 | 0.0248 | 0.0010 | 0.0493 | 0.0499 | 0.0006 |
| J | L | 0.0675 | 0.0625 | 0.0050 | 0.0467 | 0.0499 | 0.0032 |
| J | M | 0.0488 | 0.0420 | 0.0067 | 0.0613 | 0.0591 | 0.0022 |
| J | N | 0.0475 | 0.0520 | 0.0045 | 0.0728 | 0.0728 | 0.0001 |
| J | P | 0.0244 | 0.0210 | 0.0034 | 0.0306 | 0.0296 | 0.0011 |
| J | Q | 0.0488 | 0.0420 | 0.0067 | 0.0613 | 0.0591 | 0.0022 |
| K | L | 0.2675 | 0.2700 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| K | M | 0.0350 | 0.0400 | 0.0050 | 0.0564 | 0.0584 | 0.0020 |
| K | N | 0.0288 | 0.0275 | 0.0013 | 0.0529 | 0.0518 | 0.0010 |
| K | P | 0.0175 | 0.0200 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| K | Q | 0.0350 | 0.0400 | 0.0050 | 0.0564 | 0.0584 | 0.0020 |
| L | L | 0.5175 | 0.5200 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| L | M | 0.0800 | 0.0825 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| L | N | 0.0619 | 0.0644 | 0.0025 | 0.0537 | 0.0540 | 0.0003 |
| L | P | 0.0400 | 0.0412 | 0.0013 | 0.0141 | 0.0146 | 0.0005 |
| L | Q | 0.0800 | 0.0825 | 0.0025 | 0.0282 | 0.0292 | 0.0010 |
| M | N | 0.0400 | 0.0410 | 0.0010 | 0.0586 | 0.0587 | 0.0001 |
| N | P | 0.0200 | 0.0205 | 0.0005 | 0.0293 | 0.0294 | 0.0001 |
| N | Q | 0.0400 | 0.0410 | 0.0010 | 0.0586 | 0.0587 | 0.0001 |
Other ways to run the simulation
cumulateSimKinships runs the same simulation but
returns only the mean, standard deviation, minimum and maximum of each
kinship coefficient across the simulations, not every simulated kinship
matrix. It and createSimKinships both take a
twinRelations table (see ?reportGV), so animals
declared identical (MZ) twins have their kinship corrected to genetic
identity in every simulated pedigree.