weights_subpops: Get weights for individuals that balance subpopulations

View source: R/weights_subpops.R

weights_subpopsR Documentation

Get weights for individuals that balance subpopulations

Description

This function returns positive weights that sum to one for individuals using subpopulation labels, such that every subpopulation receives equal weight. In particular, if there are K subpopulations, then the sum of weights for every individuals of a given subpopulation will equal 1 / K. The weight of every individual is thus inversely proportional to the number of individuals in its subpopulation. If the optional sub-subpopulation labels are also provided, then each sub-subpopulation within a given subpopulation is also weighted equally.

Usage

weights_subpops(subpops, subsubpops = NULL)

Arguments

subpops

The length-n vector of subpopulation assignments for each individual.

subsubpops

The optional length-n vector of sub-subpopulation assignments for each individual. Each sub-subpopulation must belong to a single subpopulation (a nested hierarchy) or an error is produced.

Value

The length-n vector of weights for each individual.

Examples

# if every individual has a different subpopulation, weights are uniform:
subpops <- 1:10
weights <- weights_subpops( subpops )
stopifnot( all( weights == rep.int( 1/10, 10 ) ) )

# subpopulations can be strings too
subpops <- c('a', 'b', 'c')
weights <- weights_subpops( subpops )
stopifnot( all( weights == rep.int( 1/3, 3 ) ) )

# if there are two subpopulations
# and the first has twice as many individuals as the second
# then the individuals in this first subpopulation weight half as much 
# as the ones in the second subpopulation
subpops <- c(1, 1, 2)
weights <- weights_subpops( subpops )
stopifnot( all( weights == c( 1/4, 1/4, 1/2 ) ) )

# 2-level hierarchy example
subpops <- c(1, 1, 1, 2, 2)
subsubpops <- c('a', 'b', 'b', 'c', 'd')
weights <- weights_subpops( subpops, subsubpops )
stopifnot( all( weights == c( 1/4, 1/8, 1/8, 1/4, 1/4 ) ) )


popkin documentation built on Jan. 7, 2023, 1:26 a.m.