Imagine having a population where age of individuals is known. I would like to sample from this population individuals by age according to a parametric distribution (e.g. X ~ Poisson(1.82)). In other words, I would like to remove a known number of individuals from each class, where class sizes follows a Poisson distribution.
Is there a more elegant way?
ma <- 10 # max age
ss <- 50 # sample size
l <- 1.82 # lambda in Poisson distribution
set.seed(357)
# let's say we have a population of animals of ages
ages <- sample(x = 0:ma, size = 1000, replace = TRUE)
# we want to remove ss number of individuals according to
# X ~ Poisson(l)
cull.by.age.class <- table(rpois(n = ss, lambda = l))
before.removal <- table(ages)
# for each class, sample perscribed number of individuals
# and remove them from the population
for (i in seq_along(cull.by.age.class)) {
find.inds <- which(ages %in% as.numeric(names(cull.by.age.class[i])))
num.to.remove <- cull.by.age.class[i]
to.remove <- sample(
# find individuals of age i
find.inds,
# sample given number of individuals
num.to.remove
)
ages[to.remove] <- NA
}
after.removal <- table(ages)
removal <- cbind(before.removal, after.removal)
removal <- cbind(removal, n_removed = -apply(removal, MARGIN = 1, FUN = diff))
removal
before.removal after.removal n_removed
0 106 99 7
1 78 63 15
2 87 75 12
3 83 76 7
4 95 90 5
5 89 86 3
6 102 101 1
7 101 101 0
8 75 75 0
9 92 92 0
10 92 92 0