Generates a tibble of random demographic information for each individual in the simulation. The returned demography tibble can be modified post-hoc to use user-specified distributions and values.

generate_pop_demography(
  N,
  times,
  birth_times = NULL,
  age_min = 0,
  removal_min = min(times),
  removal_max = max(times),
  prob_removal,
  aux = NULL
)

Arguments

N

The number of individuals in the simulation

times

A vector of each time step in the simulation

birth_times

A vector of birth times for each individual; defaults to NULL; if birth_times is not specified then the function will simulate uniformly distributed birth times for each individual from the times vector

age_min

A number matching the time resolution of times giving the youngest age possible by the end of the simulation; defaults to 0 which means individuals can be born up until the penultimate time step

removal_min

The minimum age at which an individual can be removed from the population. Defaults to 0

removal_max

The maximum age at which an individual can be removed from the population. Defaults to max(times)

prob_removal

The probability that an individual will be removed from the population during the simulation, representing e.g., death or study attrition. If set to NA, then removal time will be max(times)+1

aux

An optional list of lists describing additional demographic factors. Each list must contain the variable name, a vector of possible factor levels, and their proportions to simulate from. Note that this is intended for categorical, uncorrelated variables. More complex demographic information should be added post-hoc. Defaults to NULL.

Value

A tibble of relevant demographic information for each individual in the simulation is returned; this output matches the required demography input for the runserosim function.

See also

Examples

## Example 1 -- default
generate_pop_demography(10, times=1:120, age_min=0, removal_min=0, 
removal_max=120, prob_removal=0.3)
#> removal_min is less than the first time step. Setting to min(times).
#> 
#> Joining with `by = join_by(i)`
#> # A tibble: 1,200 × 4
#>        i birth removal times
#>    <int> <int>   <dbl> <int>
#>  1     1   113     121     1
#>  2     1   113     121     2
#>  3     1   113     121     3
#>  4     1   113     121     4
#>  5     1   113     121     5
#>  6     1   113     121     6
#>  7     1   113     121     7
#>  8     1   113     121     8
#>  9     1   113     121     9
#> 10     1   113     121    10
#> # ℹ 1,190 more rows

## Example 2 -- specified birth times
birth_times <- rpois(100, 5)
generate_pop_demography(N=100, times=1:120, birth_times=birth_times, 
age_min=0, removal_min=0, removal_max=120, prob_removal=0.3)
#> removal_min is less than the first time step. Setting to min(times).
#> 
#> Joining with `by = join_by(i)`
#> # A tibble: 12,000 × 4
#>        i birth removal times
#>    <int> <int>   <dbl> <int>
#>  1     1     4     121     1
#>  2     1     4     121     2
#>  3     1     4     121     3
#>  4     1     4     121     4
#>  5     1     4     121     5
#>  6     1     4     121     6
#>  7     1     4     121     7
#>  8     1     4     121     8
#>  9     1     4     121     9
#> 10     1     4     121    10
#> # ℹ 11,990 more rows

## Example 3 -- using auxiliary variables
aux <- list("Sex"=list("name"="sex","options"=c("male", "female"), "proportion"=c(0.5,0.5)),
            "Group"=list("name"="group","options"=c(1, 2, 3, 4), 
            "proportion"=c(0.25,0.25,0.25,0.25)) )
generate_pop_demography(10, 1:120, age_min=0, removal_min=0, removal_max=120, 
prob_removal=0.3, aux=aux)
#> removal_min is less than the first time step. Setting to min(times).
#> 
#> Joining with `by = join_by(i)`
#> # A tibble: 1,200 × 6
#>        i birth removal times sex    group
#>    <int> <int>   <dbl> <int> <chr>  <dbl>
#>  1     1    86     121     1 female     4
#>  2     1    86     121     2 female     4
#>  3     1    86     121     3 female     4
#>  4     1    86     121     4 female     4
#>  5     1    86     121     5 female     4
#>  6     1    86     121     6 female     4
#>  7     1    86     121     7 female     4
#>  8     1    86     121     8 female     4
#>  9     1    86     121     9 female     4
#> 10     1    86     121    10 female     4
#> # ℹ 1,190 more rows